class A1
{
public:
int a;
A1(int s = 0):a(s){};
};
class B1:virtual public A1
{
public:
B1(int s = 0):A1(s){};
};
class B2:virtual public A1
{
public:
B2(int s = 0):A1(s){};
};
class C1:public B1,public B2
{
public:
C1(int b1 = 0,int b2 = 0):B1(b1),B2(b2){};
};
int _tmain(int argc, _TCHAR* argv[])
{
C1 c1(5,6);
std::cout<<c1.a<<std::endl<<c1.a;
getchar();
return 0;
}
Вопрос по виртуальному множественному наследованию
Код:
вышеприведенный код выдает 0 0. А почему? Разве он не должен выдать 6 6? Как мне проинициализировать базовый класс нужным значением?
http://www.csci.csusb.edu/dick/c++std/cd2/special.html#class.base.init
Код:
6 All sub-objects representing virtual base classes are initialized by
the constructor of the most derived class (_intro.object_). If the
constructor of the most derived class does not specify a mem-initial-
izer for a virtual base class V, then V's default constructor is
called to initialize the virtual base class subobject. If V does not
have an accessible default constructor, the initialization is ill-
formed. A mem-initializer naming a virtual base class shall be
ignored during execution of the constructor of any class that is not
the most derived class. [Example:
class V {
public:
V();
V(int);
// ...
};
class A : public virtual V {
public:
A();
A(int);
// ...
};
class B : public virtual V {
public:
B();
B(int);
// ...
};
class C : public A, public B, private virtual V {
public:
C();
C(int);
// ...
};
A::A(int i) : V(i) { /* ... */ }
B::B(int i) { /* ... */ }
C::C(int i) { /* ... */ }
V v(1); // use V(int)
A a(2); // use V(int)
B b(3); // use V()
C c(4); // use V()
--end example]
the constructor of the most derived class (_intro.object_). If the
constructor of the most derived class does not specify a mem-initial-
izer for a virtual base class V, then V's default constructor is
called to initialize the virtual base class subobject. If V does not
have an accessible default constructor, the initialization is ill-
formed. A mem-initializer naming a virtual base class shall be
ignored during execution of the constructor of any class that is not
the most derived class. [Example:
class V {
public:
V();
V(int);
// ...
};
class A : public virtual V {
public:
A();
A(int);
// ...
};
class B : public virtual V {
public:
B();
B(int);
// ...
};
class C : public A, public B, private virtual V {
public:
C();
C(int);
// ...
};
A::A(int i) : V(i) { /* ... */ }
B::B(int i) { /* ... */ }
C::C(int i) { /* ... */ }
V v(1); // use V(int)
A a(2); // use V(int)
B b(3); // use V()
C c(4); // use V()
--end example]
Т.е. в данном случае "most derived class" - это твой класс C1, а т.к. у него нет никаких указаний на счет A1, то вызывается его дефолтовый конструктор.
Цитата:
Originally posted by Green
Все описано в стандарте:
http://www.csci.csusb.edu/dick/c++std/cd2/special.html#class.base.init
Т.е. в данном случае "most derived class" - это твой класс C1, а т.к. у него нет никаких указаний на счет A1, то вызывается его дефолтовый конструктор.
Все описано в стандарте:
http://www.csci.csusb.edu/dick/c++std/cd2/special.html#class.base.init
Код:
6 All sub-objects representing virtual base classes are initialized by
the constructor of the most derived class (_intro.object_). If the
constructor of the most derived class does not specify a mem-initial-
izer for a virtual base class V, then V's default constructor is
called to initialize the virtual base class subobject. If V does not
have an accessible default constructor, the initialization is ill-
formed. A mem-initializer naming a virtual base class shall be
ignored during execution of the constructor of any class that is not
the most derived class. [Example:
class V {
public:
V();
V(int);
// ...
};
class A : public virtual V {
public:
A();
A(int);
// ...
};
class B : public virtual V {
public:
B();
B(int);
// ...
};
class C : public A, public B, private virtual V {
public:
C();
C(int);
// ...
};
A::A(int i) : V(i) { /* ... */ }
B::B(int i) { /* ... */ }
C::C(int i) { /* ... */ }
V v(1); // use V(int)
A a(2); // use V(int)
B b(3); // use V()
C c(4); // use V()
--end example]
the constructor of the most derived class (_intro.object_). If the
constructor of the most derived class does not specify a mem-initial-
izer for a virtual base class V, then V's default constructor is
called to initialize the virtual base class subobject. If V does not
have an accessible default constructor, the initialization is ill-
formed. A mem-initializer naming a virtual base class shall be
ignored during execution of the constructor of any class that is not
the most derived class. [Example:
class V {
public:
V();
V(int);
// ...
};
class A : public virtual V {
public:
A();
A(int);
// ...
};
class B : public virtual V {
public:
B();
B(int);
// ...
};
class C : public A, public B, private virtual V {
public:
C();
C(int);
// ...
};
A::A(int i) : V(i) { /* ... */ }
B::B(int i) { /* ... */ }
C::C(int i) { /* ... */ }
V v(1); // use V(int)
A a(2); // use V(int)
B b(3); // use V()
C c(4); // use V()
--end example]
Т.е. в данном случае "most derived class" - это твой класс C1, а т.к. у него нет никаких указаний на счет A1, то вызывается его дефолтовый конструктор.
спасибо
Цитата:
Originally posted by Green
Все описано в стандарте:
http://www.csci.csusb.edu/dick/c++std/cd2/special.html#class.base.init
Т.е. в данном случае "most derived class" - это твой класс C1, а т.к. у него нет никаких указаний на счет A1, то вызывается его дефолтовый конструктор.
Все описано в стандарте:
http://www.csci.csusb.edu/dick/c++std/cd2/special.html#class.base.init
Код:
6 All sub-objects representing virtual base classes are initialized by
the constructor of the most derived class (_intro.object_). If the
constructor of the most derived class does not specify a mem-initial-
izer for a virtual base class V, then V's default constructor is
called to initialize the virtual base class subobject. If V does not
have an accessible default constructor, the initialization is ill-
formed. A mem-initializer naming a virtual base class shall be
ignored during execution of the constructor of any class that is not
the most derived class. [Example:
class V {
public:
V();
V(int);
// ...
};
class A : public virtual V {
public:
A();
A(int);
// ...
};
class B : public virtual V {
public:
B();
B(int);
// ...
};
class C : public A, public B, private virtual V {
public:
C();
C(int);
// ...
};
A::A(int i) : V(i) { /* ... */ }
B::B(int i) { /* ... */ }
C::C(int i) { /* ... */ }
V v(1); // use V(int)
A a(2); // use V(int)
B b(3); // use V()
C c(4); // use V()
--end example]
the constructor of the most derived class (_intro.object_). If the
constructor of the most derived class does not specify a mem-initial-
izer for a virtual base class V, then V's default constructor is
called to initialize the virtual base class subobject. If V does not
have an accessible default constructor, the initialization is ill-
formed. A mem-initializer naming a virtual base class shall be
ignored during execution of the constructor of any class that is not
the most derived class. [Example:
class V {
public:
V();
V(int);
// ...
};
class A : public virtual V {
public:
A();
A(int);
// ...
};
class B : public virtual V {
public:
B();
B(int);
// ...
};
class C : public A, public B, private virtual V {
public:
C();
C(int);
// ...
};
A::A(int i) : V(i) { /* ... */ }
B::B(int i) { /* ... */ }
C::C(int i) { /* ... */ }
V v(1); // use V(int)
A a(2); // use V(int)
B b(3); // use V()
C c(4); // use V()
--end example]
Т.е. в данном случае "most derived class" - это твой класс C1, а т.к. у него нет никаких указаний на счет A1, то вызывается его дефолтовый конструктор.
спасибо, понял.