By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 898 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Calling member function in ctors and dtors.

P: n/a
Is it safe to call nonvirtual member functions from ctors and dtors?
What about virtual ones?

Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a

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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.