473,508 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Constructor Initialization

Hi,

It seems I can influence how a base class is initialized beyond the
'normal' manner and I was wondering if someone can tell me why this.
Here's my example.

class A
{
public:
A(int a=1, int b=1, int c=1) : _a(a), _b(b), _c(c) {}
void print()
{ cout << _a << "," << _b << "," << _c << endl; }
private:
int _a, _b, _c;
};

class B : public virtual A
{
public:
B(int a=1, int b=1, int c=1) : A(a, b, c) {}
};

class C : public virtual B
{
public:
C(int a=1, int b=1, int c=1)
:
B(a, b, c),
A(2,2,2) // I can explicitly initialize A here and override
the default values
{}
};

main()
{
C c;
c.print();
}
thanks,
Mike

Jul 19 '05 #1
5 9439

"Michael McKnerney" <mi***************@baesystems.com> wrote in message news:3E***************@baesystems.com...
Hi,

It seems I can influence how a base class is initialized beyond the
'normal' manner and I was wondering if someone can tell me why this.
Because you have virtual base classes. The most derived class must initialize
all the virtual bases. The initialization of A specified in the B constructor has no
bearing on the creation of C objects.
main()
{


You must provide an int return here (even in C these days).
Jul 19 '05 #2
On Fri, 27 Jun 2003 17:39:03 +0000, Michael McKnerney wrote:
Hi,

It seems I can influence how a base class is initialized beyond the
'normal' manner and I was wondering if someone can tell me why this.
Here's my example.

[snip]......
For virtual bases, the rule is different. The most derived class is
responsible for constructing the virtual base class, as opposed to its
immediate descendant in case of non-virtual bases.

HTH,
-Dhruv.


Jul 19 '05 #3


Dhruv wrote:
On Fri, 27 Jun 2003 17:39:03 +0000, Michael McKnerney wrote:
Hi,

It seems I can influence how a base class is initialized beyond the
'normal' manner and I was wondering if someone can tell me why this.
Here's my example. [snip]......

For virtual bases, the rule is different. The most derived class is
responsible for constructing the virtual base class, as opposed to its
immediate descendant in case of non-virtual bases.


But wait a minute, if I remove the explicit initialization of A in C's
constructor list, A will get initialized by B.


HTH,
-Dhruv.


Jul 19 '05 #4

"Michael McKnerney" <mi***************@baesystems.com> wrote in message news:3E**************@baesystems.com...
But wait a minute, if I remove the explicit initialization of A in C's
constructor list, A will get initialized by B.


No it will not. If you remove the explicit initialization, it will invoke the
default constructor. If A didn't have a default constructor, then you'd
get an error.
Jul 19 '05 #5
Michael McKnerney wrote:

Dhruv wrote:

On Fri, 27 Jun 2003 17:39:03 +0000, Michael McKnerney wrote:

Hi,

It seems I can influence how a base class is initialized beyond the
'normal' manner and I was wondering if someone can tell me why this.
Here's my example.


[snip]......

For virtual bases, the rule is different. The most derived class is
responsible for constructing the virtual base class, as opposed to its
immediate descendant in case of non-virtual bases.


But wait a minute, if I remove the explicit initialization of A in C's
constructor list, A will get initialized by B.


Nope. If you remove the explicit initialization of A in C's initializer
list and then invoke the class C ctor,

C(int a=1, int b=1, int c=1)
: B(a,b,c)
{ }

the sequence of events (IIRC) is this:

1) Program invokes the C ctor
2) The C ctor implicitly invokes A's default ctor
3) The C ctor invokes 'B(a,b,c)' as specified in the initializer list of
the class C ctor. The B ctor does not invoke the A ctor.

So the class C ctor -- not the B ctor -- is still the entity that
invokes the class A ctor.
FWIW, if you rewrite your code a bit, this sequence of events will (IMO)
be easier to observe.

<example>
<code>

#include <iostream>
using namespace std;

class A {
public:
A(int a=1, int b=1, int c=1)
: a_(a), b_(b), c_(c)
{ cout << "A(" << a << ',' << b << ',' << c << ")\n"; }

void print()
{ cout << a_ << ',' << b_ << ',' << c_ << '\n'; }

private:
int a_, b_, c_;
};

class B : public virtual A {
public:
B(int a=2, int b=2, int c=2)
: A(a, b, c)
{ cout << "B(" << a << ',' << b << ',' << c << ")\n"; }
};

class C : public virtual B {
public:
C(int a=3, int b=3, int c=3)
// : A(4,4,4), B(a,b,c)
: B(a,b,c)
{ }
};

int main()
{
C c;
c.print();
}

</code>

<output>
A(1,1,1)
B(3,3,3)
1,1,1
</output>

</example>
Some side notes:

1) DO NOT use variable names that start with a leading underscore, e.g.,
'_a'. These names (and some others) are reserved for the
implementation's use -- e.g., the standard library, compiler-specific
language extensions, the OS's API, etc. So if you want to use
underscores to denote variable names, place the underscore at the end of
the name, 'a_', and not at the beginning.

2) In this code sample, class A's ctor is *always* invoked before class
B's ctor. This sequence holds even if you try (futilely) to specify the
base class ctor invocation sequence as B first, then A, in the class C
ctor initializer list:

C() : B(), A() { }

IOW, the compiler ignores the B,A sequence specified above, and
implicitly implements C's initializer list as A,B, i.e.,

C() : A(), B() { }

[n.b. Most compilers are nice enough to warn you when they implicitly
reorder the initializer list's arguments like this.]

--
Jim

To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link5809{at}now.here.com
Jul 19 '05 #6

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

Similar topics

2
4700
by: Grumble | last post by:
Hello all, What, if any, is the difference between string s("toto"); string s = "toto"; In the first case, I am using the constructor: basic_string(const value_type *ptr);
11
580
by: Kurt Krueckeberg | last post by:
Given a class X class X { public: X(int); X(const X&); //. . . }; Is this line X x1(1); the same thing as X x2 = 2;
6
2474
by: rona | last post by:
Hi everyone, Could someone tell us why the below code compiles and works? Is it legal in the constructor of class B? Cheers Ronny ---------------------------------
12
5790
by: NewToCPP | last post by:
does the default constructor initialize values? I have a class as defined below: class A { int i; char c; int * iPtr;
9
1970
by: toton | last post by:
Hi, Which one is the correct syntax for constructor for static memory allocation Object obj("param"); or Object obj = Object("param"); 1) For the first one, how compiler checks it as not a...
10
2012
by: imutate | last post by:
Some questions about ctors and class members Is v private in the following ? If it is why put the declaration at the top ? Is there any difference to putting it in the private section ? The...
74
15868
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
14
1887
by: raylopez99 | last post by:
Question: the "myList" below is the same (works the same) whether instantiated inside the (normal) constructor, as below, and as my standard practice, or outside? RL public class AClass {...
4
351
by: t | last post by:
Suppose class A has no programmer-written constructors and the compiler generates a default constructor for it. Suppose class B is exactly like class A except it has a programmer-written default...
3
2427
by: mhvaughn | last post by:
struct S1 { int i; }; struct S2 { S1 s; // version 1 S2() {} ; // version 2
0
7132
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
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7401
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...
0
5640
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,...
1
5059
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
4720
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
3211
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.