sowmya wrote:
> Hello,
> I wanted to know if calling a virtual function from another
> virtual function is valid or not.I was working on an assignmnet where
> the functions are pure in the base class and have definitions in the
> derived class.
> I used g++ to compile the code and I was getting a runtime error
> saying "pure virtual function called. aborted".
> The program executes fine when the declaration is removed in the
> base class.Can anyone please tell me the reason?
>
> I have included the brief structure of the code
> class base
> {
> public:
> virtual void abc();
> virtual void efg();
> };
>
> class derived: public base
> {
> public:
> virtual void abc(){ }
> virtual void efg(){ ...
> abc();
> ...
> }
> };
>
>
> Regards,
> Sowmya
The above code compiled without any warnings or errors? I'm surprised
the code even compiled without having definitions of abc() and efg() in
the base class. You are also writing base and derived classes
incorrectly. See Safe C++ Design Principles and read the chapter on
base classes and templates.
class base
{
public:
base() {}
virtual ~base() {}
virtual void abc() {}
virtual void efg() {}
};
class derived: public base
{
public:
derived() : base() {}
virtual ~derived() {}
virtual void abc() {}
virtual void efg() { abc(); }
};
Should work fine. I rarely use pure virtual functions. They cause too
many problems.
--
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197
*NEW* MyTaskFocus 1.1
Get on task. Stay on task.
http://www.CubicleSoft.com/MyTaskFocus/