473,249 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,249 software developers and data experts.

Pure virtual methods, constructors and destructors

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.
Jul 23 '05 #1
5 2318
Ruben Campos wrote:
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
Jul 23 '05 #2


Ruben Campos wrote:
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() {};
};
Jul 23 '05 #3
Hang Dog wrote:
[...]
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
Jul 23 '05 #4
Ruben Campos wrote:
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.
Jul 23 '05 #5
Victor Bazarov wrote:
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.
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
37
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
7
by: vaividhya | last post by:
We can have virtual destructors.Why we can't have virtual constructors?
5
by: Alok | last post by:
hii Would somebody clear my doubts on following qustions . What are virtual constructors and virtual destructors ? Why can't we have virtual constructors ? What are demerits of inheritence in...
4
by: Eric | last post by:
I was wondering what people thought about the information found at: http://g.oswego.edu/dl/mood/C++AsIDL.html Specifically, I am interested in the following recommendation: ---- Since...
7
by: Markus Svilans | last post by:
Hello, My question involves virtual functions and inheritance. Suppose we have a class structure, that consists of "data" classes, and "processor" classes. The data classes are derived from...
8
by: Shraddha | last post by:
What is the use of "PURE vitual distructors"? And why we can not have vitual constructors?
2
by: katyusha | last post by:
why virtual destructors are possible but virtual constructors are not?
14
by: Jack | last post by:
Hi, I meet a question with it , I did not get clear the different betteen them, for example: #include <iostream>
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.