473,324 Members | 2,535 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,324 software developers and data experts.

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 2220
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.newshosting.co m...
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.

(Implementations 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
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'...
8
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...
13
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
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...
7
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...
1
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 {}; ...
5
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
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);...
5
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
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
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...
1
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.