![]() |
Initializer lists and multiple constructors
If class C inherits from class B, which inherits from class A, is the
order of the initializer list in the following constructor insignificant (C only inherits from A because it inherits from B)?: C::C() : A(), B() { } That is, would: C::C() : B(), A() { } work just the same? What if none of these were default constructors and some arguments to C's constructor where passed to the parent class constructors? Now, what if B's constructor was: B::B() : A() { } Upon invoking the constructor for C, would A's constructor be called twice? |
Re: Initializer lists and multiple constructors
Richard wrote:
> If class C inherits from class B, which inherits from class A, is the > order of the initializer list in the following constructor > insignificant (C only inherits from A because it inherits from B)?: > > C::C() : > A(), > B() > { > } > > That is, would: > > C::C() : > B(), > A() > { > } > Neither of these should compile. You can only call the constructor of a direct base class. -- Ian Collins. |
Re: Initializer lists and multiple constructors
On Feb 28, 5:42 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> Richard wrote: > > If class C inherits from class B, which inherits from class A, is the > > order of the initializer list in the following constructor > > insignificant (C only inherits from A because it inherits from B)?: > > > C::C() : > > A(), > > B() > > { > > } > > > That is, would: > > > C::C() : > > B(), > > A() > > { > > } > > Neither of these should compile. You can only call the constructor of a > direct base class. > > -- > Ian Collins.- Hide quoted text - > > - Show quoted text - It compiles for me. Something strange, here. |
Re: Initializer lists and multiple constructors
On Feb 28, 6:30 pm, "Richard" <rksa...@gmail.com> wrote:
> On Feb 28, 5:42 pm, Ian Collins <ian-n...@hotmail.com> wrote: > > > > > > > Richard wrote: > > > If class C inherits from class B, which inherits from class A, is the > > > order of the initializer list in the following constructor > > > insignificant (C only inherits from A because it inherits from B)?: > > > > C::C() : > > > A(), > > > B() > > > { > > > } > > > > That is, would: > > > > C::C() : > > > B(), > > > A() > > > { > > > } > > > Neither of these should compile. You can only call the constructor of a > > direct base class. > > > -- > > Ian Collins.- Hide quoted text - > > > - Show quoted text - > > It compiles for me. Something strange, here.- Hide quoted text - > > - Show quoted text - Ok, well something very close to this works in MULTI, but it doesn't seem to work in GCC. |
Re: Initializer lists and multiple constructors
Richard wrote:
> On Feb 28, 5:42 pm, Ian Collins <ian-n...@hotmail.com> wrote: > >>Richard wrote: >> >>>If class C inherits from class B, which inherits from class A, is the >>>order of the initializer list in the following constructor >>>insignificant (C only inherits from A because it inherits from B)?: >> >>>C::C() : >>> A(), >>> B() >>>{ >>>} >> >>>That is, would: >> >>>C::C() : >>> B(), >>> A() >>>{ >>>} >> >>Neither of these should compile. You can only call the constructor of a >>direct base class. >> >>-- >>Ian Collins.- Hide quoted text - >> >>- Show quoted text - > *Please trim the signature* that's the bit after the "-- ". > > It compiles for me. Something strange, here. > Well it shouldn't. Where did all that quoted text crap come from? I didn't write it. -- Ian Collins. |
Re: Initializer lists and multiple constructors
On Mar 1, 6:29 am, "Richard" <rksa...@gmail.com> wrote:
> If class C inherits from class B, which inherits from class A, is the > order of the initializer list in the following constructor > insignificant (C only inherits from A because it inherits from B)?: > > C::C() : > A(), > B() > { > > } > > That is, would: > > C::C() : > B(), > A() > { > > } > > work just the same? What if none of these were default constructors > and some arguments to C's constructor where passed to the parent class > constructors? > > Now, what if B's constructor was: > > B::B() : > A() > { > > } > > Upon invoking the constructor for C, would A's constructor be called > twice? I tried compiling your sample with g++ (3.3) and as expected it throws compilation error. Here is what it says: g++ const.cpp const.cpp: In constructor `C::C()': const.cpp:18: error: type `class A' is not a direct base of `C' Which compiler are you using? Cheers -Vallabha S7 Software Solutions http://s7solutions.com/ |
Re: Initializer lists and multiple constructors
Richard wrote:
> If class C inherits from class B, which inherits from class A, is the > order of the initializer list in the following constructor > insignificant (C only inherits from A because it inherits from B)?: You can only provide initializers for the direct base classes and for any virtual base classes. The specification of mem initializers has no bearing on construction order. They just provide the arguments for the constructor when it would have been called anyway which is: 1. From the most derived class, the virtual base classes as they appear in a depth-first, left-to-right, traversal. and then for each object recursively starting with the most derived are initialized: 2. The non-virtual direct base classes are initialized in left to right order of their listing in the class definition (not the mem-initializers) 3. The non-static members are initialized in order they appear in the class definition. 4. The constructor body is run. |
Re: Initializer lists and multiple constructors
It must be a compiler specific issue. I'm using the gbuild.exe
compiler bundled with MULTI 4.2.3. It actually complains if I remove what is the equivalent of A's constructor from C's initializer list. |
Re: Initializer lists and multiple constructors
Richard wrote:
> It must be a compiler specific issue. I'm using the gbuild.exe > compiler bundled with MULTI 4.2.3. It actually complains if I remove > what is the equivalent of A's constructor from C's initializer list. > File a bug. -- Ian Collins. |
| All times are GMT. The time now is 01:05 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.