473,513 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Order of constructor execution?

Hi. I have base class Base and derived class Derived. My derived
class constructor is dependent upon attributes set in the base class
constructor. Is this okay? Does ANSI C++ guarantee that a base class
constructor will complete before the derived class contructor executes?

Thanks!

Ken

Mar 29 '06 #1
3 2742
kk_oop wrote:
Hi. I have base class Base and derived class Derived. My derived
class constructor is dependent upon attributes set in the base class
constructor. Is this okay? Does ANSI C++ guarantee that a base class
constructor will complete before the derived class contructor executes?


Yes. Just don't do it the other way around, where the base class depends on
derived class values.

This FAQ entry explains one way to do that which the language prevents:

http://www.parashift.com/c++-faq-lit....html#faq-23.5

There are others.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Mar 29 '06 #2
On 29 Mar 2006 06:40:54 -0800, kk****@yahoo.com wrote:
Does ANSI C++ guarantee that a base class
constructor will complete before the derived class contructor executes?


For regular inheritance (public, protected or private) the answer is
yes. For virtual inheritance, the rules are a little different, but in
most cases also yes. The base class constructor in a virtual
inheritance hierarchy is invoked by the most-derived class
constructor, whereas otherwise it is invoked by the constructor of the
next class in its hierarchy.

This should also be covered in the FAQ link that Phlip posted.

--
Bob Hairgrove
No**********@Home.com
Mar 29 '06 #3
On Wed, 29 Mar 2006 06:40:54 -0800, kk_oop wrote:
Hi. I have base class Base and derived class Derived. My derived class
constructor is dependent upon attributes set in the base class
constructor. Is this okay? Does ANSI C++ guarantee that a base class
constructor will complete before the derived class contructor executes?

This small test should explain all:

#include <iostream>

using namespace std;

class foo_base {
protected:
string test;

public:
foo_base() : test ("assigned") {};
~foo_base() {};
};

class bar_derived : public foo_base
{
public:
bar_derived() {};
~bar_derived() {};
string getTest() { return test; };
};

int main(void)
{
bar_derived *bar = new bar_derived();
cout << bar->getTest() << endl;
exit (EXIT_SUCCESS);
}

--
Hilsen/Regards
Michael Rasmussen
http://keyserver.veridis.com:11371/p...rch=0xE3E80917

Mar 29 '06 #4

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

Similar topics

11
2655
by: Bhushit Joshipura | last post by:
This post contains one question and one proposal. A. May I know why order of evaluation of arguments is not specified in C/C++? I asked a question in comp.lang.c++ for the following...
7
3638
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
10
2504
by: Marek | last post by:
Hi, I am analyzing Duwamish7 source code boundled with Visual Studio .NET 2003. Could anoybody explain why the Monitor.Enter and Monitor.Exit block is used inside a static constructor? The code...
13
2561
by: z. f. | last post by:
Hi, i have a class that is derived from System.Web.UI.Page, and this is the class i use in my application as PageBase. all other page classes are deriverd from my PageBase instead of the...
6
1435
by: Marek | last post by:
Hi, I am analyzing Duwamish7 source code boundled with Visual Studio .NET 2003. Could anoybody explain why the Monitor.Enter and Monitor.Exit block is used inside a static constructor? The code...
7
2130
by: Morgan Cheng | last post by:
In the book *Programming C#* 4th editionby Jesse Liberty, it reads "Actually, the CLR guarantees to start running the static constructor before anything else is done with your class. However, it...
7
3394
by: BeautifulMind | last post by:
In case of inheritence the order of execution of constructors is in the order of derivation and order of destructor execution is in reverse order of derivation. Is this case also true in case...
8
2424
by: Jess | last post by:
Hello, When I define default constructors, I tend to use constructor initializers for member data. However, I was told the order in which members are initialized is determined by the order of...
7
2687
by: eshneto | last post by:
Sorry if this may seem to be very obvious, but it is important and I decided to ask. I have the following classes: class MutexWrapper { public: MutexWrapper( void ); void lock( void );...
0
7158
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
7535
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...
1
7098
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...
0
7523
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...
1
5085
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...
0
4745
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...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.