AnsweredAssumed Answered

MPC5748G C++ inheritance

Question asked by Kfir Ben shimon on Jun 13, 2017
Latest reply on Jun 26, 2017 by Martin Kovar

Hi,

 

I have a problem calling a function that is virtual in abstract and implemented in child:

class ClassA
{
public:
virtual int sum() = 0;
};

The child class:

header:

class ClassB : public ClassA
{
public:
ClassB(int startValue);

virtual int sum();

private:
int fStartValue;
};

source:

ClassB::ClassB(int startValue) :
fStartValue(startValue)
{
}

int ClassB::sum()
{
int sum = fStartValue;
for (int i = 0; i < 4; ++i)
{
sum += i;
}
return sum;
}

 

The compilation goes without warnings nor errors, but when debugging I get an exception when calling sum():

main src:

ClassA* pObjectA;
ClassB objectB(10);

int main()
{

        pObjectA = &objectB;

        total += pObjectA->sum();

}

 

What can be the reason for that?

How can I solve this?

Outcomes