Pure virtual methods, constructors and destructors

R

Ruben Campos

I've recently noticed that it's not allowed to call a pure (non-implemented)
virtual method inside a constructor or a destructor, doesn't matter if this
method is declared in the considered class itself or in one of its base
classes. Such an attempt results in a linker undefined symbol error. Why? Is
it right, or is it a bad issue of my compiler/linker (I'm working with
Microsoft Visual C++ 7.1.3088)? Thank you in advance for your help.
 
V

Victor Bazarov

Ruben said:
I've recently noticed that it's not allowed to call a pure (non-implemented)
virtual method inside a constructor or a destructor, doesn't matter if this
method is declared in the considered class itself or in one of its base
classes. Such an attempt results in a linker undefined symbol error. Why? Is
it right, or is it a bad issue of my compiler/linker (I'm working with
Microsoft Visual C++ 7.1.3088)? Thank you in advance for your help.

Calling a virtual function in a c-tor or d-tor results in static linking
to that class' member (or the base if this doesn't have it), and not in
a polymorphic call. That's why calling a pure virtual function from the
c-tor or d-tor causes undefined behaviour.

You can provide a definition of the pure virtual function just for that
purpose:

struct ABC {
virtual ~ABC() = 0; // makes this class abstract
};

ABC::~ABC() {} // do nothing, but at least it is defined

struct D : ABC { }; // no problem destroying it -- the ABC's dtor is
// defined, although it is pure

V
 
H

Hang Dog

Ruben said:
I've recently noticed that it's not allowed to call a pure (non-implemented)
virtual method inside a constructor or a destructor, doesn't matter if this
method is declared in the considered class itself or in one of its base
classes. Such an attempt results in a linker undefined symbol error. Why? Is
it right, or is it a bad issue of my compiler/linker (I'm working with
Microsoft Visual C++ 7.1.3088)? Thank you in advance for your help.

What do you expect the complier to call instead of the non-implemented
function?

BTW a pure virtual function just says that derived classes have to
define the method. It doesn't mean that the pure virtual can't also be
defined, and in may cases they are.

class A {
public:
virtual ~A() {};
};
 
V

Victor Bazarov

Hang said:
[...]
BTW a pure virtual function just says that derived classes have to
define the method. It doesn't mean that the pure virtual can't also be
defined, and in may cases they are.

class A {
public:
virtual ~A() {};
};

There are two things wrong about this example. First, the topic is *pure*
virtual functions, and there are none in that example. Second, it has
a superfluous semicolon after the body of the d-tor. Yes, I do consider
it wrong although it's allowed by the syntax. It's a bad habit and
reduces readability.

Also, even if you did declare ~A() pure, you can't define it in the class:

class A {
public:
virtual ~A() = 0 {} // wrong as well
};

It would be a syntax error. The definition has to be put at the namespace
level.

V
 
R

Ron Natalie

Ruben said:
I've recently noticed that it's not allowed to call a pure (non-implemented)
virtual method inside a constructor or a destructor,

Doesn't matter if it's implemented or not either. Making a virtual call
to a pure virtual fucntion during construction/destruction is undefined
behavior. That's the way the standard reads....it's not required to
catch it.
 
R

Ron Natalie

Victor said:
Calling a virtual function in a c-tor or d-tor results in static linking
to that class' member (or the base if this doesn't have it), and not in
a polymorphic call

Incorrect. The call may still be polymorphic, but the type of the object
is that of the constructor that is currently running.

Consider the following:

class Base {
public:
virtual void vfunc();
void nvfunc() {
vfunc();
};
};

class Derived : public Base {
public:
void vfunc();
Derived() {
nvfunc();
}
};

class MoreDerived : public Derived {
void vfunc();
};

MoreDerived foo;

While the object is being created and the Derived function is being
invoked, the dynmaic type is set to Derived. Derived's constructor
calls Based::nvfunc which calls Derived::vfunc (not base::vfunc
or MoreDerived::vfunc).

If Base::vfunc was declared pure virtual, the behavior here is undefined.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top