* RE: Non-default ctors, virtual base classes, & multiple inheritance all at once!
@ 2000-09-06 10:13 Jan Reimers
2000-09-06 11:15 ` Non-default ctors, virtual base classes, & multiple inheritan ce " sidster
0 siblings, 1 reply; 8+ messages in thread
From: Jan Reimers @ 2000-09-06 10:13 UTC (permalink / raw)
To: 'sidster'
Cc: 'gcc@gcc.gnu.org', 'scherrey@switchco.com'
> -----Original Message-----
> From: sidster [ mailto:patrick@mail.boxsoft.com ]
> Sent: Wednesday, September 06, 2000 9:33 AM
> To: Alexandre Oliva
> Cc: scherrey@switchco.com; gcc@gcc.gnu.org
> Subject: Re: Non-default ctors, virtual base classes, & multiple
> inheritance all at once!
>
> If I understand what you are suggesting correctly then it is
> _impossible_ to get the desired behavior with the given code.
>
> Since template <typename X> class HoldX has no knowledge of what
> base classes X might have to even provide constructors to specify
> them in its "mem-initializer-list".
template <typename X> class HoldX : public virtual X
{
HoldX(int) : A(int),X(int) {};
};
Now the template parameter X is required to derive from A, restricting the
utility of HoldX. But most template code places requirements on the
template parameters, STL being an example. There is nothing earth
shattering about this. Make virtual base classes abstract with no data, and
everything will work out.
JR
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Non-default ctors, virtual base classes, & multiple inheritan ce all at once!
2000-09-06 10:13 Non-default ctors, virtual base classes, & multiple inheritance all at once! Jan Reimers
@ 2000-09-06 11:15 ` sidster
0 siblings, 0 replies; 8+ messages in thread
From: sidster @ 2000-09-06 11:15 UTC (permalink / raw)
To: Jan Reimers
Cc: 'sidster', 'gcc@gcc.gnu.org',
'scherrey@switchco.com'
Greetings again,
* Jan Reimers (janr@molienergy.bc.ca) [20000906 10:43]:
>
>
> > -----Original Message-----
> > From: sidster [ mailto:patrick@mail.boxsoft.com ]
> > Sent: Wednesday, September 06, 2000 9:33 AM
> > To: Alexandre Oliva
> > Cc: scherrey@switchco.com; gcc@gcc.gnu.org
> > Subject: Re: Non-default ctors, virtual base classes, & multiple
> > inheritance all at once!
> >
> > If I understand what you are suggesting correctly then it is
> > _impossible_ to get the desired behavior with the given code.
> >
> > Since template <typename X> class HoldX has no knowledge of what
> > base classes X might have to even provide constructors to specify
> > them in its "mem-initializer-list".
>
> template <typename X> class HoldX : public virtual X
> {
> HoldX(int) : A(int),X(int) {};
> };
>
> Now the template parameter X is required to derive from A, restricting the
> utility of HoldX. But most template code places requirements on the
> template parameters, STL being an example. There is nothing earth
> shattering about this. Make virtual base classes abstract with no data, and
> everything will work out.
You fail to consider the following situation:
I, working for the company HoldX.com, wrote this class.
You, working for the company CvpA.com, wrote the C class and its base
classes.
In situations like this your solution is bogus at best.
The best solution I can think of is what I mentioned at the end of my
post: To create a "wrapper" class to be the most derived class and to
specify the desired constructors for the virtual base classes (direct
and indirect).
An example of this would be:
http://www.ktb.net/~sidster/work/mtor.cc.html
But this is still ugly imo.
Sure I can staple my Picasso to the wall for public display. However,
one would wish for a more elegant solution.
patrick
p.s., STL is far from being elegant.
--
when i grow up i want to be a famous rock'n roll guitar player
-- steve vai
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Non-default ctors, virtual base classes, & multiple inheritance all atonce!
@ 2000-09-05 12:12 rlau
2000-09-05 16:35 ` Non-default ctors, virtual base classes, & multiple inheritance all at once! llewelly
0 siblings, 1 reply; 8+ messages in thread
From: rlau @ 2000-09-05 12:12 UTC (permalink / raw)
To: gcc
your template class do not have a corresponding constructor that accepts
(int) as argument.
Check with comp.prog.c++.moderate!
Good luck!
Benjamin Scherrey <scherrey@switchco.com>@gcc.gnu.org on 09/05/2000
12:58:20 PM
Please respond to scherrey@switchco.com
Sent by: gcc-owner@gcc.gnu.org
To: gcc@gcc.gnu.org
cc:
Subject: Non-default ctors, virtual base classes, & multiple inheritance
all at once!
I'm having some trouble getting non-default constructors called for
virtual baseclasses in certain instances.. I've attached some code that
demonstrates this problem. In some cases, I can pass a parameter up the
tree to the virtual base class, in others, the default ctor is always
called even though my code clearly specifies the ctor taking a parm. Is
this a compiler bug or a language design issue?
thanx & later,
Ben Scherrey
//
// vinherit.cpp - Tests resolution of virtual inheritance.
//
#include <iostream>
#include <string>
class A
{
public:
A( void ) : v(0) { cout << "A ctor @ " << this << ", v = " << v <<
endl; }
A( int a ) : v(a) { cout << "A int ctor @ " << this << ", v = " << v
<< endl; }
A( const A& a ) : v(a.v) { cout << "A copy ctor @ " << this << " from
" << &a << ", v = " << v << endl; }
virtual ~A( void ) { cout << "A dtor @ " << this << ", v = " << v <<
endl; }
virtual void f1( void ) { cout << "A::f1 v = " << v << endl; }
virtual void f2( void ) = 0;
int v;
};
class B : public A
{
public:
B( void ) : A() { cout << "B ctor @ " << this << endl; }
B( int i ) : A( i ) { cout << "B int ctor @ " << this << endl; }
B( const B& a ) : A(a) { cout << "B copy ctor @ " << this << " from "
<< &a << endl; }
virtual ~B( void ) { cout << "B dtor @ " << this << endl; }
virtual void f2( void ) { cout << "B::f2" << endl; }
};
class C : public virtual A
{
public:
C( void ) : A() { cout << "C ctor @ " << this << endl; }
C( int i ) : A(i) { cout << "C int ctor @ " << this << endl; }
C( const C& a ) : A(a) { cout << "C copy ctor @ " << this << " from "
<< &a << endl; }
virtual ~C( void ) { cout << "C dtor @ " << this << endl; }
virtual void f2( void ) { cout << "C::f2" << endl; }
};
template < typename X >
class HoldX : public virtual X
{
public:
template < typename T >
HoldX( T t ) : X( t ) { cout << "T HoldX ctor @ " << this << endl; }
HoldX( void ) : X() { cout << "HoldX ctor @ " << this << endl; }
HoldX( const HoldX& a ) : X(a) { cout << "HoldX copy ctor @ " << this
<< " from " << &a << endl; }
virtual ~HoldX( void ) { cout << "HoldX dtor @ " << this << endl; }
virtual void f2( void ) { cout << "HoldX::f2" << endl; }
};
template < typename Y, typename X >
class HoldY : public Y, public HoldX< X >
{
public:
template < typename T >
HoldY( T t ) : Y( t ), HoldX< X >( t ) { cout << "T HoldY ctor @ " <<
this << endl; }
HoldY( void ) : Y(), HoldX< X >() { cout << "HoldY ctor @ " << this
<< endl; }
HoldY( const HoldY& a ) : Y(a), HoldX< X >(a) { cout << "HoldY copy
ctor @ " << this << " from " << &a << endl; }
virtual ~HoldY( void ) { cout << "HoldY dtor @ " << this << endl; }
virtual void f2( void ) { cout << "HoldY::f2" << endl; }
};
template < typename Y, typename X >
class HoldZ : public Y, virtual public HoldX< X >
{
public:
template < typename T >
HoldZ( T t ) : Y(t), HoldX< X >(t) { cout << "\nT HoldZ ctor @ " <<
this << endl; }
HoldZ( void ) : Y(), HoldX< X >() { cout << "\nHoldZ ctor @ " << this
<< endl; }
HoldZ( const HoldZ& a ) : Y(a), HoldX< X >(a) { cout << "\nHoldZ copy
ctor @ " << this << " from " << &a << endl; }
virtual ~HoldZ( void ) { cout << "HoldZ dtor @ " << this << endl; }
virtual void f2( void ) { cout << "HoldZ::f2" << endl; }
};
int main( void )
{
cout << "Test case for virtual inheritance." << endl;
cout << "\nConstructing HoldX< A > X1;" << endl;
HoldX< A > X1; // OK
cout << "\nConstructing HoldX< A > X2(2);" << endl;
HoldX< A > X2(2); // OK
cout << "\nConstructing HoldX< B > X3;" << endl;
HoldX< B > X3; // OK
cout << "\nConstructing HoldX< B > X4(4);" << endl;
HoldX< B > X4(4); // OK
cout << "\nConstructing HoldX< C > X5;" << endl;
HoldX< C > X5; // OK
cout << "\nConstructing HoldX< C > X6(6);" << endl;
HoldX< C > X6(6); // Huh? Why no A(int) call?
cout << "\nConstructing HoldY< B, A > Y1;" << endl;
HoldY< B, A > Y1; // OK
cout << "\nConstructing HoldY< C, A > Y2;" << endl;
HoldY< C, A > Y2; // OK
cout << "\nConstructing HoldY< C, A > Y3(3);" << endl;
HoldY< C, A > Y3(3);// Huh? No A(int) again!
cout << "\nConstructing HoldZ< B, A > Z1;" << endl;
HoldZ< B, A > Z1; // OK
cout << "\nConstructing HoldZ< C, A > Z2;" << endl;
HoldZ< C, A > Z2; // OK
cout << "\nConstructing HoldZ< C, A > Z3(3);" << endl;
HoldZ< C, A > Z3(3);// Again no A(int)!
Z3.f1();
Z3.f2();
cout << "\nConstructing C C1(1);" << endl;
C C1(1); // OK
cout << "\nTest complete." << endl;
return 0;
}
//
// eof( vinherit.cpp )
//
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Non-default ctors, virtual base classes, & multiple inheritance all at once!
2000-09-05 12:12 Non-default ctors, virtual base classes, & multiple inheritance all atonce! rlau
@ 2000-09-05 16:35 ` llewelly
2000-09-05 22:59 ` Benjamin Scherrey
0 siblings, 1 reply; 8+ messages in thread
From: llewelly @ 2000-09-05 16:35 UTC (permalink / raw)
To: scherrey; +Cc: gcc
rlau@csc.com writes:
> Please respond to scherrey@switchco.com
>
> Sent by: gcc-owner@gcc.gnu.org
>
>
> To: gcc@gcc.gnu.org
> cc:
> Subject: Non-default ctors, virtual base classes, & multiple inheritance
> all at once!
>
>
> I'm having some trouble getting non-default constructors called for
> virtual baseclasses in certain instances.. I've attached some code that
> demonstrates this problem. In some cases, I can pass a parameter up the
> tree to the virtual base class, in others, the default ctor is always
> called even though my code clearly specifies the ctor taking a parm. Is
> this a compiler bug or a language design issue?
Language design.
Constructors of virtual base classes are called from the constructor
of the most derived class.
The reason is that any data members in a virtual base class must
appear only once in the final object, and must therefor only be
initialized once. Calling the constructor from the constructor of
the final type is how the langauge ensures this.
For a full explanation, ask comp.lang.c++.moderated
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Non-default ctors, virtual base classes, & multiple inheritance all at once!
2000-09-05 16:35 ` Non-default ctors, virtual base classes, & multiple inheritance all at once! llewelly
@ 2000-09-05 22:59 ` Benjamin Scherrey
0 siblings, 0 replies; 8+ messages in thread
From: Benjamin Scherrey @ 2000-09-05 22:59 UTC (permalink / raw)
To: llewelly; +Cc: gcc, scherrey
Bless you my son! I have been going nuts with this thing. I could not find
this in any of my references. I'll probably run across it about two months
from now.... Thanx!
llewelly@edevnull.com wrote:
> rlau@csc.com writes:
>
> > Please respond to scherrey@switchco.com
> > I'm having some trouble getting non-default constructors called for
> > virtual baseclasses in certain instances.. I've attached some code that
> > demonstrates this problem. In some cases, I can pass a parameter up the
> > tree to the virtual base class, in others, the default ctor is always
> > called even though my code clearly specifies the ctor taking a parm. Is
> > this a compiler bug or a language design issue?
>
> Language design.
>
> Constructors of virtual base classes are called from the constructor
> of the most derived class.
>
> The reason is that any data members in a virtual base class must
> appear only once in the final object, and must therefor only be
> initialized once. Calling the constructor from the constructor of
> the final type is how the langauge ensures this.
>
> For a full explanation, ask comp.lang.c++.moderated
^ permalink raw reply [flat|nested] 8+ messages in thread
* Non-default ctors, virtual base classes, & multiple inheritance all at once!
@ 2000-09-05 11:15 Benjamin Scherrey
2000-09-06 3:27 ` Alexandre Oliva
0 siblings, 1 reply; 8+ messages in thread
From: Benjamin Scherrey @ 2000-09-05 11:15 UTC (permalink / raw)
To: gcc
I'm having some trouble getting non-default constructors called for
virtual baseclasses in certain instances.. I've attached some code that
demonstrates this problem. In some cases, I can pass a parameter up the
tree to the virtual base class, in others, the default ctor is always
called even though my code clearly specifies the ctor taking a parm. Is
this a compiler bug or a language design issue?
thanx & later,
Ben Scherrey
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Non-default ctors, virtual base classes, & multiple inheritance all at once!
2000-09-05 11:15 Benjamin Scherrey
@ 2000-09-06 3:27 ` Alexandre Oliva
2000-09-06 8:39 ` Joe Buck
2000-09-06 9:37 ` sidster
0 siblings, 2 replies; 8+ messages in thread
From: Alexandre Oliva @ 2000-09-06 3:27 UTC (permalink / raw)
To: scherrey; +Cc: gcc
On Sep 5, 2000, Benjamin Scherrey <scherrey@switchco.com> wrote:
> I can pass a parameter up the tree to the virtual base class, in
> others, the default ctor is always called even though my code
> clearly specifies the ctor taking a parm. Is this a compiler bug or
> a language design issue?
The latter. It is the most derived class that must construct all
virtual base classes, even indirect ones. If it doesn't, default
constructors are used, even if other base classes specify constructor
arguments for virtual bases.
--
Alexandre Oliva Enjoy Guarana', see http://www.ic.unicamp.br/~oliva/
Red Hat GCC Developer aoliva@{cygnus.com, redhat.com}
CS PhD student at IC-Unicamp oliva@{lsd.ic.unicamp.br, gnu.org}
Free Software Evangelist *Please* write to mailing lists, not to me
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Non-default ctors, virtual base classes, & multiple inheritance all at once!
2000-09-06 3:27 ` Alexandre Oliva
@ 2000-09-06 8:39 ` Joe Buck
2000-09-06 9:37 ` sidster
1 sibling, 0 replies; 8+ messages in thread
From: Joe Buck @ 2000-09-06 8:39 UTC (permalink / raw)
To: Alexandre Oliva; +Cc: scherrey, gcc
>
> On Sep 5, 2000, Benjamin Scherrey <scherrey@switchco.com> wrote:
>
> > I can pass a parameter up the tree to the virtual base class, in
> > others, the default ctor is always called even though my code
> > clearly specifies the ctor taking a parm. Is this a compiler bug or
> > a language design issue?
>
> The latter. It is the most derived class that must construct all
> virtual base classes, even indirect ones. If it doesn't, default
> constructors are used, even if other base classes specify constructor
> arguments for virtual bases.
This is one of the major botches in C++. The only way around it that will
scale is to have a policy that, whenever virtual base classes are used,
the virtual base must have a default constructor and this constructor must
be the one that is used in the constructor for any derived object that
uses the virtual base. Otherwise you have a major violation of
encapsulation and all derived classes have to worry about initializing the
virtual base correctly, and if anyone makes a derived class that doesn't
do this, things don't work right.
The compiler, of course, has to support people who violate this design
rule.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Non-default ctors, virtual base classes, & multiple inheritance all at once!
2000-09-06 3:27 ` Alexandre Oliva
2000-09-06 8:39 ` Joe Buck
@ 2000-09-06 9:37 ` sidster
1 sibling, 0 replies; 8+ messages in thread
From: sidster @ 2000-09-06 9:37 UTC (permalink / raw)
To: Alexandre Oliva; +Cc: scherrey, gcc
Greetings,
* Alexandre Oliva (aoliva@redhat.com) [20000906 07:45]:
> On Sep 5, 2000, Benjamin Scherrey <scherrey@switchco.com> wrote:
>
> > I can pass a parameter up the tree to the virtual base class, in
> > others, the default ctor is always called even though my code
> > clearly specifies the ctor taking a parm. Is this a compiler bug or
> > a language design issue?
>
> The latter. It is the most derived class that must construct all
> virtual base classes, even indirect ones. If it doesn't, default
> constructors are used, even if other base classes specify constructor
> arguments for virtual bases.
If I understand what you are suggesting correctly then it is
_impossible_ to get the desired behavior with the given code.
Since template <typename X> class HoldX has no knowledge of what
base classes X might have to even provide constructors to specify
them in its "mem-initializer-list".
In other words all template classes with template parameter classes
containing virtual bases classes (direct or indirect) are restricted
to using/initializing only the default constructors of these virtual
base classes. ???
I'm not sure about you but this strikes me as /quite odd/!
If this is true ... then there is a HUGE limitation in the C++
language!
I don't think this makes sense at all. Can you please provide us with
a reference to this behavior/limitation in the standard?
The only section I find is the following:
[12.6.2 Initializing bases and members]
//
// Initialization shall proceed in the following order:
//
// - First, and only for the constructor of the most derived class
// as described below, virtual base classes shall be initialized
// in the order they appear on a depth-first left-to-right
// traversal of the directed acyclic graph of base classes, where
// "left-to-right" is the order of appearance of the base class
// names in the derived class base-specifier-list.
Which seems to be supporting your point. Which, originally I wasn't too
sure if it actually meant *all virtual base classes* (including direct
and indirect).
But the more i read the above it is more clear that that is in-fact what
it means.
The only possible way of getting the intended behavior from the given
code would be to write a "wrapper" class to be the most derived class
and specify the desired constructors of the virtual base classes in its
"mem-initializer-list".
Eeeek! Talk about a hack, error prone, tedious and a maintenance
nightmare!
Please correct me if i'm wrong. Originally I was going to discard this
thread but now it's become more interesting :>
patrick
--
when i grow up i want to be a famous rock'n roll guitar player
-- steve vai
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2000-09-06 11:15 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-09-06 10:13 Non-default ctors, virtual base classes, & multiple inheritance all at once! Jan Reimers
2000-09-06 11:15 ` Non-default ctors, virtual base classes, & multiple inheritan ce " sidster
-- strict thread matches above, loose matches on Subject: below --
2000-09-05 12:12 Non-default ctors, virtual base classes, & multiple inheritance all atonce! rlau
2000-09-05 16:35 ` Non-default ctors, virtual base classes, & multiple inheritance all at once! llewelly
2000-09-05 22:59 ` Benjamin Scherrey
2000-09-05 11:15 Benjamin Scherrey
2000-09-06 3:27 ` Alexandre Oliva
2000-09-06 8:39 ` Joe Buck
2000-09-06 9:37 ` sidster
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).