473,836 Members | 1,506 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling member function in ctors and dtors.

Is it safe to call nonvirtual member functions from ctors and dtors?
What about virtual ones?

Jul 23 '05 #1
6 2253
Bi****@abv.bg wrote:
Is it safe to call nonvirtual member functions from ctors and dtors?
What about virtual ones?


Yes, it's safe. Unlike Java, C++ has a safe rule for virtual functions:
when a constructor or destructor calls a virtual function it calls the
function defined for the type whose constructor or destructor is
currently being run, which isn't necessarily the most derived type.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #2
Pete Becker wrote:
Bi****@abv.bg wrote:
Is it safe to call nonvirtual member functions from ctors and dtors?
What about virtual ones?


Yes, it's safe. Unlike Java, C++ has a safe rule for virtual functions:
when a constructor or destructor calls a virtual function it calls the
function defined for the type whose constructor or destructor is
currently being run, which isn't necessarily the most derived type.

With the exception of virtual calls to pure virtual functions in the constructors
(these are undefined).
Jul 23 '05 #3

"Ron Natalie" <ro*@sensor.com > skrev i en meddelelse
news:42******** *************** @news.newshosti ng.com...
Pete Becker wrote:
Bi****@abv.bg wrote:
Is it safe to call nonvirtual member functions from ctors and dtors?
What about virtual ones?


Yes, it's safe. Unlike Java, C++ has a safe rule for virtual functions:
when a constructor or destructor calls a virtual function it calls the
function defined for the type whose constructor or destructor is
currently being run, which isn't necessarily the most derived type.

With the exception of virtual calls to pure virtual functions in the
constructors
(these are undefined).


Hi Ron

This is a slight surprise to me as i read it that pure virtual functions can
be called in destructors. Was that the intended meaning and if so - what is
the result?

/Peter
Jul 23 '05 #4

Ron Natalie wrote:
Pete Becker wrote:
Bi****@abv.bg wrote:
Is it safe to call nonvirtual member functions from ctors and dtors? What about virtual ones?

Yes, it's safe. Unlike Java, C++ has a safe rule for virtual functions: when a constructor or destructor calls a virtual function it calls the function defined for the type whose constructor or destructor is
currently being run, which isn't necessarily the most derived type.

With the exception of virtual calls to pure virtual functions in the

constructors (these are undefined).


And destructors, 10.4/6, and it also includes indirect calls.

The reason is that pure virtual functions don't have to appear in
vtables.
The vtable entries will be overwritten by the derived constructor, as
every pure virtual function must be defined later, in a derived class.
Non-virtual calls don't use vtables, and therefore are defined.

(Implementation s which don't use vtables are still bound by the same
rules. )

HTH,
Michiel Salters

Jul 23 '05 #5
Ron Natalie wrote:
Pete Becker wrote:
Bi****@abv.bg wrote:
Is it safe to call nonvirtual member functions from ctors and dtors?
What about virtual ones?


Yes, it's safe. Unlike Java, C++ has a safe rule for virtual
functions: when a constructor or destructor calls a virtual function
it calls the function defined for the type whose constructor or
destructor is currently being run, which isn't necessarily the most
derived type.

With the exception of virtual calls to pure virtual functions in the
constructors
(these are undefined).


As I said, it calls the function defined for the type whose contructor
or destructor is currently being run. In a class that has a pure virtual
function with no definition the function is not defined.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #6
Peter Koch Larsen wrote:
Hi Ron

This is a slight surprise to me as i read it that pure virtual functions can
be called in destructors. Was that the intended meaning and if so - what is
the result?

That is the intended meaning and it is what the standard says.
It is undefined behavior to make a virtual call to a pure virtual
function in a constructor or destructor (even if it has an implementation) .
Jul 23 '05 #7

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

Similar topics

8
1842
by: SJ | last post by:
Hi: I have a class which has a static member function. The function implements something common to all instances. How can the static member function know all of the (Get access to the instances' handles) instances? Thanks in advance for any help
8
1419
by: Sathyaish | last post by:
How does a constructor of one class call another of the same class? When would this mechanism make sense or be required? PS: Two instances I saw are: (a) While using the Singleton pattern (b) While using the Factory pattern
13
4119
by: RainBow | last post by:
Hi everyone, (Very Sorry, if this is the wrong group in which I am posting this query). Code snippet: //C library typedef int (*PFunc)(int* aArg); void call_c_foo(PFunc aPtrtoFunc) {
31
5201
by: Peter E. Granger | last post by:
I'm fairly new to C++ and VC++, but for the most part it seems to do most of the same things that can be done in Java, with just some syntactic and structural adjustments. However, one thing I haven't been able to figure out is how to call one constructor from another within a class. It's easy enough to call the base class's constructor from the derived class, but that's not what I'm trying to do. For example, in Java (or J#) it's easy...
7
15029
by: The|Godfather | last post by:
Hi everybody, I read Scotte Meyer's "Effective C++" book twice and I know that he mentioned something specific about constructors and destructors that was related to the following error/warning: "error: invalid use of nonstatic data member " However, he did NOT mention this error in the book explicitly.It happens always in the constructor when you try to initialize some data members in the constructor and try to accsess other data...
1
2527
by: Belebele | last post by:
I would like to have the compiler bind a Base class const reference to a temporary object of a Derived class when calling another class constructor. Please see the code below: class Base {}; class Derived: public Base {}; class Foo { public:
5
7425
by: 2b|!2b==? | last post by:
I have a class that contains C structs as member variables. By C structs, I mean they cannot have ctors/dtors because they have C linkage (extern "C"). For eg: MyClass { //Impl private:
2
1906
by: .rhavin grobert | last post by:
i have (do try to have?) the following... & = breakpoints in debugger // ---------------------------------------------------------------- // cx.h class CX { public: CX(CX* pcx = NULL); virtual ~CX();
5
1655
by: wiskey5alpha | last post by:
Hello all. I have a design question. assuming we have class Bar {...} // body omitted for clarity class Foo { // should Bar be implemented as a pointer or not here ?
0
9826
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10860
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10560
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10604
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10261
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9389
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6984
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5659
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.