473,387 Members | 1,785 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,387 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 2325
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.