virtual multiple inheritance

Hi

i want to use multiple inheritance. but i get the following compiler error:
error C2512: 'Object::Object' : Kein geeigneter Standardkonstruktor verfuegbar
error C2512: 'Object::Object' : No suitable StandardConstructor available

for this Source:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class Object
{
	public:
		Object(int c){
			printf("Object\n");
			a = 0;
		}

		~Object(){
			printf("~Object\n");
		}

		int a;
};


class Object1 : public virtual Object
{
	public:
		Object1(int c):Object(c){
			printf("Object1\n");
			a1 = 0;
			a = 1;
		}
		~Object1(){
			printf("~Object1\n");
		}

		int a1;
};


class Object2 : public virtual Object
{
	public:
		Object2(int c):Object(c){
			printf("Object2\n");
			a2= 0;
			a = 2;
		}
		~Object2(){
			printf("~Object2\n");
		}

		int a2;
};

class Object4 : public virtual Object1, public virtual Object2
{
	public:
		Object4(int c):Object1(c), Object2(c){
			printf("Object4\n");
			a4 = 0;
			a = 4;
		}
		~Object4(){
			printf("~Object4\n");
	}

		int a4;
};


i don't know where is my mistake.
When using virtual inheritance the same instance of Object is shared by both Object1 and Object2 when they are part of an instance of Object4. This means that if both Object1 and Object2 were allowed to initialise Object, the constructor would be run twice, which would not be correct.

The rule in C++ is, the initialisers for Object in Object1 and Object2s' constructors are ignored. (That is, they do not run) The virtual base must be initialised directly from Object4.

Thus the following works...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
class Object
{
public:
    Object(int c)
    {
        printf("Object\n");
        a = 0;
    }
    
    ~Object()
    {
        printf("~Object\n");
    }
    
    int a;
};


class Object1 : public virtual Object
{
public:
    // Call to Objects constructor is not called when instance is part of an Object4
    Object1(int c) : Object(c)
    {
        printf("Object1\n");
        a1 = 0;
        a = 1;
    }
    
    ~Object1()
    {
        printf("~Object1\n");
    }
    
    int a1;
};


class Object2 : public virtual Object
{
public:
    // Call to Objects constructor is not called when instance is part of an Object4
    Object2(int c) : Object(c)
    {
        printf("Object2\n");
        a2= 0;
        a = 2;
    }
    
    ~Object2(){
        printf("~Object2\n");
    }
    
    int a2;
};

class Object4 : public virtual Object1, public virtual Object2
{
public:
    // Note extra call to Object's constructor
    Object4(int c) : Object1(c), Object2(c), Object(c)
    {
        printf("Object4\n");
        a4 = 0;
        a = 4;
    }

    ~Object4()
    {
        printf("~Object4\n");
    }
    
    int a4;
};
Last edited on
Thanks very well
IIRC:
You don't need the base classes Object1 and Object2 of Object4 to use virtual inheritance, and the initializer for Object should be first in the Object4 constructor's init list.
ropex: The order of the initialisation list has no impact on the order of initialisation. Object will always be initialised first.

And yes, your right, in this case the virtual keywords on line 57 could be removed.

The keyword virtual basically says "only put one instance of this base in derived classes if possible" so on line 57 you are currently saying the classes derived from Object4 should treat Object1 and Object2 as virtual bases too.
Topic archived. No new replies allowed.