Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

c++ - Is this the right approach to count the number of base class objects created when there is a partial virtual inheritance involved?

From what I have learnt about virtual inheritance, whenever we have a class A (the base class), classes B, C, D (all these three virtually inherit class A) then if there is a class derived which inherits from B, C and D classes is instantiated then only a single object of class A would be created.

I tried to reason about the virtual inheritance by taking different cases:

Case 1:

Assume the derived class inherits only from class B and C. Even then only a single object of class A gets instantiated, right?

I tried to verify this using this code and it certainly seems to be the case:

#include <iostream>
using namespace std;
class A
{
    public:
    int a;
};
class B: virtual public A
{
  public:
  int b;
};
class C: virtual public A
{
    public:
    int c;
};
class D: virtual public A
{
    public:
    int d;
};
class derived: public B, public C
{
    public:
    int y;
};
int main() 
{
    derived dObj;
    cout<<&(dObj.B::a)<<'
';
    cout<<&(dObj.C::a)<<'
';
    return 0;
}

Output:

0x7ffc8adac1c0
0x7ffc8adac1c0

Case 2:

Assume the derived class inherits from class B, C and D, but class D doesn't inherit class A virtually. In this case, 2 objects of class A get instantiated, right? - One for B, C and one for D.

I tried to verify this using this code and it certainly seems to be the case:

#include <iostream>
using namespace std;
class A
{
    public:
    int a;
};
class B: virtual public A
{
  public:
  int b;
};
class C: virtual public A
{
    public:
    int c;
};
class D: public A
{
    public:
    int d;
};
class derived: public B, public C, public D
{
    public:
    int y;
};
int main() 
{
    derived dObj;
    cout<<&(dObj.B::a)<<'
';
    cout<<&(dObj.C::a)<<'
';
    cout<<&(dObj.D::a)<<'
';
    return 0;
}

Output:

0x7ffd512429c8
0x7ffd512429c8
0x7ffd512429bc

Now here is my question:

Are my observations correct? That is, instead of just 3 classes - B, C, D, let's assume we have n classes that inherit class A, if out of them, m (1<=m<=n) classes are inherited by another class named derived, and if out of those m classes only k (0<=k<=m) classes inherit class A virtually then the number of objects of class A that get instantiated when instantiating the derived class would be = 1+m-k (if k!=0) and m (when k=0), right?

question from:https://stackoverflow.com/questions/65867634/is-this-the-right-approach-to-count-the-number-of-base-class-objects-created-whe

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From the content written in the cppreference website it is indeed the case:

See the first example under the virtual base classes section.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...