niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » C++
Calling member function in ctors and dtors.
Post new topic   Reply to topic Page 1 of 1 [7 Posts] View previous topic :: View next topic
Author Message
BigMan
*nix forums addict


Joined: 23 Feb 2005
Posts: 69

PostPosted: Thu Feb 03, 2005 5:35 pm    Post subject: Calling member function in ctors and dtors. Reply with quote

Is it safe to call nonvirtual member functions from ctors and dtors?
What about virtual ones?
Back to top
Pete Becker
*nix forums Guru


Joined: 22 Feb 2005
Posts: 682

PostPosted: Thu Feb 03, 2005 5:43 pm    Post subject: Re: Calling member function in ctors and dtors. Reply with quote

BigMan@abv.bg wrote:
Quote:
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)
Back to top
Ron Natalie
*nix forums Guru


Joined: 21 Feb 2005
Posts: 461

PostPosted: Thu Feb 03, 2005 11:37 pm    Post subject: Re: Calling member function in ctors and dtors. Reply with quote

Pete Becker wrote:
Quote:
BigMan@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).
Back to top
Peter Koch Larsen
*nix forums Guru Wannabe


Joined: 20 Feb 2005
Posts: 141

PostPosted: Fri Feb 04, 2005 9:53 am    Post subject: Re: Calling member function in ctors and dtors. Reply with quote

"Ron Natalie" <ron@sensor.com> skrev i en meddelelse
news:4202c03c$0$11733$9a6e19ea@news.newshosting.com...
Quote:
Pete Becker wrote:
BigMan@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
Back to top
msalters
*nix forums Guru Wannabe


Joined: 02 Mar 2005
Posts: 181

PostPosted: Fri Feb 04, 2005 10:58 am    Post subject: Re: Calling member function in ctors and dtors. Reply with quote

Ron Natalie wrote:
Quote:
Pete Becker wrote:
BigMan@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
Back to top
Ron Natalie
*nix forums Guru


Joined: 21 Feb 2005
Posts: 461

PostPosted: Fri Feb 04, 2005 12:47 pm    Post subject: Re: Calling member function in ctors and dtors. Reply with quote

Peter Koch Larsen wrote:

Quote:
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).
Back to top
Pete Becker
*nix forums Guru


Joined: 22 Feb 2005
Posts: 682

PostPosted: Fri Feb 04, 2005 1:48 pm    Post subject: Re: Calling member function in ctors and dtors. Reply with quote

Ron Natalie wrote:
Quote:
Pete Becker wrote:

BigMan@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)
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [7 Posts] View previous topic :: View next topic
The time now is Fri Jan 09, 2009 9:56 am | All times are GMT
navigation Forum index » Programming » C++
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Function Pointer Sikandar C 3 Fri Jul 21, 2006 1:23 pm
No new posts Arbitrary function with parameter darknails@gmail.com C++ 2 Fri Jul 21, 2006 9:58 am
No new posts Is there C/C++ corresponding function in Linux for Java's... xiebopublic@gmail.com apps 4 Fri Jul 21, 2006 3:22 am
No new posts Is there C/C++ corresponding function in Linux for Java's... xiebopublic@gmail.com C++ 1 Fri Jul 21, 2006 2:44 am
No new posts can I call a internal function directly? minrobin@gmail.com shell 2 Fri Jul 21, 2006 2:17 am

Loans | Bad Credit Loans | Debt Consolidation | Mortgage Loans | Banruptcy
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1963s ][ Queries: 16 (0.1126s) ][ GZIP on - Debug on ]