473,587 Members | 2,505 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 2745
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.co m 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**********@Ho me.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
2666
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 possibility and because the languages do not specify the order of evaluation, doing so was an error. int B::f ( int i, int j = i + 1 ) { // j defaults to i...
7
3649
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. And i have things to offer, and to request. And a lot of ideas, but who needs them.... here's an example (from type_struct.py):
10
2511
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 can be found in Project SystemFramework within module ApplicationLog.cs. Here comes the sample. namespace Duwamish7.SystemFramework { (some...
13
2567
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 original System.Web.UI.Page in order to have common checks in the page base. i make securirty checks in the page base page_load event. if the security...
6
1437
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 can be found in Project SystemFramework within module ApplicationLog.cs. Here comes the sample. namespace Duwamish7.SystemFramework { (some...
7
2140
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 only guarantees to *start* running the static constructor; it doesn't actually guarantee to *finish* running it." Page 82, Chap 4 Classes and...
7
3399
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 class is derived as virtual? How does the order of construction/destruction is impacted if the base class is derived as virtual or non virtual? just...
8
2433
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 declaration in the class, instead of the order they appear in the constructor initializer. This would introduce interdependencies. Therefore, it...
7
2689
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 ); //Maybe const, whatever.
0
7923
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...
0
7852
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8349
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...
1
7974
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...
0
8221
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...
0
6629
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...
1
5719
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...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.