Overriding class methods

V

Vasileios Zografos

Hi, I am trying to override a class method from a child class like the
following:


class ParentClass
{
public:
ParentClass();
virtual ~ParentClass();
virtual double someMethod (int)=0;
}


ParentClass::parentClass()
{
someMethod(1);
}

class childClass::public ParentClass
{
public:
childClass(): ParentClass(){;}
double someMethod(int);
}

double childClass::someMethod(int num)
{
//do something meaningfull here
return 0;
}


int main()
{
childClass cld();
return 0;
}


now, this program will crash because someMethod is called from the
ParentClass constructor.

If someMethod is called outside, e.g.

ParentClass::parentClass()
{

}

and

int main()
{
childClass cld();
cld.someMethod();
return 0;
}

it will work fine.
However, I want to be able to call the overriden method someMethod()
from the constructor. Is there a way around this problem?

Thank you
Vasileios
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top