virtual public inheritance? Need help understanding code - c++

I have question about virtual inheritance.
I'm interested why this code prints 1000 (from class "two") and not 3 (from class "one")
here's the code:
#include <iostream>
using namespace std;
class A {
protected:
int number;
public:
A (int a=0) {number=a;}
};
class one:virtual public A {
public:
one (int a=3) {number=a;}
void print() {cout<<number<<endl;}
};
class two :virtual public A {
public:
two (int a=1000) {number =a;}
void print() { cout<<number<<endl; }
};
class B:public one,public two {
public:
void print() { cout<<number<<endl; }
};
int main () {
B A;
A.print();
}

The base classes are initialised in the order they are declared: one then two. The virtual inheritance means that they both share the same instance of A, so there is only one variable called number here.
Initialising one assigns 3 to number then initialising two assigns 1000 to it. So, after initialising the whole object, it ends up with the value 1000.

Related

Access methods from derived class using an object from base class

I need to solve this problem about classes in C++. The problem says I need to create a base class, which has to be derived into 3 other derived classes. On main, I created a vector that can save objects from the base class to save the 3 different derived classes. The thing is that I don't know how to access the methods of the derivative classes when I access the vectors. For example, class C has 2 attributes and methods that base and other classes don't have, I need to have some way to access those methods using the vector of the base class. I hope someone could help me.
#include <iostream>
#include <vector>
#include <ctime>
using namespace std;
class Base{
protected:
string placa;
int ano_de_registro;
public:
Base(){};
Base(string _placa, int _ano_de_registro):placa(_placa),ano_de_registro(_ano_de_registro){};
const string &getPlaca() const {
return placa;
}
int getAnoDeRegistro() const {
return ano_de_registro;
}
};
class A: public Base{
int cubicaje;
int caballos_de_fuerza;
int contaminacion;//0==minimo, 1==normal y 2==excesivo
public:
A(){};
A(string _placa, int _ano_de_registro, int _cubicaje, int _caballos_de_fuerza, int _contaminacion):Base(_placa,_ano_de_registro),cubicaje(_cubicaje),caballos_de_fuerza(_caballos_de_fuerza),contaminacion(_contaminacion){};
int getCubicaje() {
return cubicaje;
}
int getCaballosDeFuerza() {
return caballos_de_fuerza;
}
int getContaminacion() {
return contaminacion;
}
};
class B: public Base{
int cubicaje;
public:
B(){};
B(string _placa, int _ano_de_registro, int _cubicaje):Base(_placa,_ano_de_registro),cubicaje(_cubicaje){};
int getCubicaje() const {
return cubicaje;
}
};
class C: public Base{
int cantidad_de_ejes;
int sobregarga;
public:
C(){};
C(string _placa, int _ano_de_registro,int _cantidad_de_ejes, int _sobregarga):C(_placa,_ano_de_registro),cantidad_de_ejes(_cantidad_de_ejes),sobregarga(_sobregarga){};
int getCantidadDeEjes() {
return cantidad_de_ejes;
}
** int getSobregarga() const {
return sobregarga;
}**
};
int main() {
srand(time(NULL));
int a1,c1,c2,sc,cb,ct,cde, r1, vs;
vector<string> Placas={"A1R 112","F5U-597","A1A-004","D5B-193","EUA-123","A8D-457","FCD-784","F0X-694","SLA-249","EBD-608",};
vector<Base*> Vehiculos;
for (int i=0;i<10;i++){
r1=rand()%3;
vs=rand()%Placas.size();
a1=rand()%22+2000;
c1=rand()%10;
c2=rand()%10;
cb=rand()%10;
ct=rand()%3;
cde=rand()%4+3;
sc=rand()%10;
A* carro=new A(Placas.at(vs),a1,c1,cb,ct);
Vehiculos.push_back(carro);
B* moto=new B(Placas.at(vs),a1,c2);
Vehiculos.push_back(moto);
** C* camion=new C(Placas.at(vs),a1,cde,sc);
Vehiculos.push_back(camion);**
}
**Vehiculos.at(0)->getSobrecarga;**
return 0;
}
Bold parts are the vector trying to access that method, the method itself and how I create the object using dynamic objects
Note: it is usually better to expand the base class with virtual functions so that no one needs to know exactly what type is being referenced. If the children are so different that a common interface is not possible, inheritance is probably not the right solution.
If you're being forced to do this for a course, there are two changes you need to make. The base class needs to have virtual methods to be polymorphic. It just so happens it needs a virtual destructor in order for the derived classes to be destroyed correctly from a reference to Base.
class Base
{
protected:
string placa;
int ano_de_registro;
public:
Base()
{
}
Base(string _placa, int _ano_de_registro) :
placa(_placa), ano_de_registro(_ano_de_registro)
{
}
virtual ~Base() = default; //Addition
const string& getPlaca() const
{
return placa;
}
int getAnoDeRegistro() const
{
return ano_de_registro;
}
};
The second change is using dynamic_cast.
Vehiculos.at(0)->getSobrecarga;
becomes (I've also corrected a few omissions)
C* example = dynamic_cast<C*>(Vehiculos.at(2)); // get a C from the vector
if (example) // test for non-null to make sure we did indeed get a C
{
example->getSobregarga(); // do the deed.
}

reduce size of object (wasted) in Multi virtual inheritance

After profiling, I found that a large portion of memory of my program are wasted by multi-virtual-inheritance.
This is MCVE to demostrate the problem ( http://coliru.stacked-crooked.com/a/0509965bea19f8d9 )
#include<iostream>
class Base{
public: int id=0;
};
class B : public virtual Base{
public: int fieldB=0;
public: void bFunction(){
//do something about "fieldB"
}
};
class C : public virtual B{
public: int fieldC=0;
public: void cFunction(){
//do something about "fieldC"
}
};
class D : public virtual B{
public: int fieldD=0;
};
class E : public virtual C, public virtual D{};
int main (){
std::cout<<"Base="<<sizeof(Base)<<std::endl; //4
std::cout<<"B="<<sizeof(B)<<std::endl; //16
std::cout<<"C="<<sizeof(C)<<std::endl; //32
std::cout<<"D="<<sizeof(D)<<std::endl; //32
std::cout<<"E="<<sizeof(E)<<std::endl; //56
}
I hope sizeof(E) to be not much more than 16 bytes (id+fieldB+fieldC+fieldD).
From experiment, if it is non virtual inheritance, E's size will be 24 (MCVE).
How to reduce size of E (by C++ magic, change program architecture, or design pattern)?
Requirement:-
Base,B,C,D,E can't be template class. It will cause circular dependency for me.
I must be able to call base class's function from derived class (if any) e.g. e->bFunction() and e->cFunction(), as usual.
However, it is OK if I can't call e->bField anymore.
I still want the ease of declaration.
Currently, I can declare "E inherit from C and D" as class E : public virtual C, public virtual D easily.
I am thinking about CRTP e.g. class E: public SomeTool<E,C,D>{}, but not sure how to make it works.
To make things easier :
In my case, every class is used like it is monolith, i.e. I will never cast object between types like static_cast<C*>(E*) or vise versa.
Macro is allowed, but discouraged.
Pimpl idiom is allowed. Actually, below is what I daydream.
Perhaps, I may be able to remove all virtual-inheritance.
However, with all the requirements, I can't find a way to code it.
In pimpl, if I make E virtual inherit from C & D, etc, all above requirement will be met, but I will still waste a lot of memory. :-
I am using C++17.
Edit
Here is a more correct description of my real-life problem.
I create a game that has many components e.g. B C D E.
All of them are created via pool. Thus, it enables fast iterating.
Currently, if I query every E from a game engine, I will be able to call e->bFunction().
In my most severe case, I waste 104 bytes per object in E-like class. (real hierarchy is more complex)
Edit 3
Let me try again. Here is a more meaningful class diagram.
I have a central system to assign hpPtr,flyPtr,entityId,componentId,typeId automatically already.
i.e. Don't worry how they are initialized.
In real case, dread diamond happen in many classes, this is the most simple case.
Currently, I call like :-
auto hps = getAllComponent<HpOO>();
for(auto ele: hps){ ele->damage(); }
auto birds = getAllComponent<BirdOO>();
for(auto ele: birds ){
if(ele->someFunction()){
ele->suicidalFly();
//.... some heavy AI algorithm, etc
}
}
With this approach, I can enjoy cache coherence as in Entity Component System, and the cool ctrl+space intellisense of HpOO,FlyableOO and BirdOO like in Object-Oriented style.
Everything works fine - it just uses too much memory.
EDIT: based on latest update to the question and some chatting
Here's the most compact maintaining the virtual in all your classes.
#include <iostream>
#include <vector>
using namespace std;
struct BaseFields {
int entityId{};
int16_t componentId{};
int8_t typeId{};
int16_t hpIdx;
int16_t flyPowerIdx;
};
vector<int> hp; // this will contain all the hit points, dynamically resizable, logic up to you
vector<float> flyPower; // this will contain all the fly powers, dynamically resizable, logic up to you
class BaseComponent {
public: // or protected
BaseFields data;
};
class HpOO : public virtual BaseComponent {
public:
void damage() {
hp[data.hpIdx] -= 1;
}
};
class FlyableOO : public virtual BaseComponent {
public:
void addFlyPower(float power) {
flyPower[data.hpIdx] += power;
}
};
class BirdOO : public virtual HpOO, public virtual FlyableOO {
public:
void suicidalFly() {
damage();
addFlyPower(5);
}
};
int main (){
std::cout<<"Base="<<sizeof(BaseComponent)<<std::endl; // 12
std::cout<<"C="<<sizeof(HpOO)<<std::endl; // 24
std::cout<<"D="<<sizeof(FlyableOO)<<std::endl; // 24
std::cout<<"E="<<sizeof(BirdOO)<<std::endl; // 32
}
much smaller class size version dropping all the virtual class stuff:
#include <iostream>
#include <vector>
using namespace std;
struct BaseFields {
};
vector<int> hp; // this will contain all the hit points, dynamically resizable, logic up to you
vector<float> flyPower; // this will contain all the fly powers, dynamically resizable, logic up to you
class BaseComponent {
public: // or protected
int entityId{};
int16_t componentId{};
int8_t typeId{};
int16_t hpIdx;
int16_t flyPowerIdx;
protected:
void damage() {
hp[hpIdx] -= 1;
};
void addFlyPower(float power) {
flyPower[hpIdx] += power;
}
void suicidalFly() {
damage();
addFlyPower(5);
};
};
class HpOO : public BaseComponent {
public:
using BaseComponent::damage;
};
class FlyableOO : public BaseComponent {
public:
using BaseComponent::addFlyPower;
};
class BirdOO : public BaseComponent {
public:
using BaseComponent::damage;
using BaseComponent::addFlyPower;
using BaseComponent::suicidalFly;
};
int main (){
std::cout<<"Base="<<sizeof(BaseComponent)<<std::endl; // 12
std::cout<<"C="<<sizeof(HpOO)<<std::endl; // 12
std::cout<<"D="<<sizeof(FlyableOO)<<std::endl; // 12
std::cout<<"E="<<sizeof(BirdOO)<<std::endl; // 12
// accessing example
constexpr int8_t BirdTypeId = 5;
BaseComponent x;
if( x.typeId == BirdTypeId ) {
auto y = reinterpret_cast<BirdOO *>(&x);
y->suicidalFly();
}
}
this example assumes your derived classes do not have overlapping functionalities with diverging effects, if you have those you have to add virtual functions to your base class for an extra overhead of 12 bytes (or 8 if you pack the class).
and quite possibly the smallest version still maintaining the virtuals
#include <iostream>
#include <vector>
using namespace std;
struct BaseFields {
int entityId{};
int16_t componentId{};
int8_t typeId{};
int16_t hpIdx;
int16_t flyPowerIdx;
};
#define PACKED [[gnu::packed]]
vector<int> hp; // this will contain all the hit points, dynamically resizable, logic up to you
vector<float> flyPower; // this will contain all the fly powers, dynamically resizable, logic up to you
vector<BaseFields> baseFields;
class PACKED BaseComponent {
public: // or protected
int16_t baseFieldIdx{};
};
class PACKED HpOO : public virtual BaseComponent {
public:
void damage() {
hp[baseFields[baseFieldIdx].hpIdx] -= 1;
}
};
class PACKED FlyableOO : public virtual BaseComponent {
public:
void addFlyPower(float power) {
flyPower[baseFields[baseFieldIdx].hpIdx] += power;
}
};
class PACKED BirdOO : public virtual HpOO, public virtual FlyableOO {
public:
void suicidalFly() {
damage();
addFlyPower(5);
}
};
int main (){
std::cout<<"Base="<<sizeof(BaseComponent)<<std::endl; // 2
std::cout<<"C="<<sizeof(HpOO)<<std::endl; // 16 or 10
std::cout<<"D="<<sizeof(FlyableOO)<<std::endl; // 16 or 10
std::cout<<"E="<<sizeof(BirdOO)<<std::endl; // 24 or 18
}
the first number is for unpacked structure, second packed
You can also pack the hpIdx and flyPowerIdx into the entityId using the union trick:
union {
int32_t entityId{};
struct {
int16_t hpIdx;
int16_t flyPowerIdx;
};
};
in the above example if not using packing and moving the whole BaseFields structure into the BaseComponent class the sizes remain the same.
END EDIT
Virtual inheritance just adds one pointer size to the class, plus alignment of the pointer (if needed). You can't get around that if you actually need a virtual class.
The question you should be asking yourself is whether you actually need it. Depending on your access methods to this data that might not be the case.
Considering you need virtual inheritance but all common methods that need to be callable from all your classes you can have a virtual base class and use a bit less space than your original design in the following way:
class Base{
public: int id=0;
virtual ~Base();
// virtual void Function();
};
class B : public Base{
public: int fieldB=0;
// void Function() override;
};
class C : public B{
public: int fieldC=0;
};
class D : public B{
public: int fieldD=0;
};
class E : public C, public D{
};
int main (){
std::cout<<"Base="<<sizeof(Base)<<std::endl; //16
std::cout<<"B="<<sizeof(B)<<std::endl; // 16
std::cout<<"C="<<sizeof(C)<<std::endl; // 24
std::cout<<"D="<<sizeof(D)<<std::endl; // 24
std::cout<<"E="<<sizeof(E)<<std::endl; // 48
}
In the case that there are cache misses but the CPU still has power to process the results you can furter decrease the size by using compiler-specific instructions to make the data structure as small as possible (next example works in gcc):
#include<iostream>
class [[gnu::packed]] Base {
public:
int id=0;
virtual ~Base();
virtual void bFunction() { /* do nothing */ };
virtual void cFunction() { /* do nothing */ }
};
class [[gnu::packed]] B : public Base{
public: int fieldB=0;
void bFunction() override { /* implementation */ }
};
class [[gnu::packed]] C : public B{
public: int fieldC=0;
void cFunction() override { /* implementation */ }
};
class [[gnu::packed]] D : public B{
public: int fieldD=0;
};
class [[gnu::packed]] E : public C, public D{
};
int main (){
std::cout<<"Base="<<sizeof(Base)<<std::endl; // 12
std::cout<<"B="<<sizeof(B)<<std::endl; // 16
std::cout<<"C="<<sizeof(C)<<std::endl; // 20
std::cout<<"D="<<sizeof(D)<<std::endl; // 20
std::cout<<"E="<<sizeof(E)<<std::endl; //40
}
saving an additional 8 bytes at the price of possibly some CPU overhead (but if memory is the issue might help).
Additionally if there is really a single function you are calling for each of your classes you should only have that as a single function which you override whenever necessary.
#include<iostream>
class [[gnu::packed]] Base {
public:
virtual ~Base();
virtual void specificFunction() { /* implementation for Base class */ };
int id=0;
};
class [[gnu::packed]] B : public Base{
public:
void specificFunction() override { /* implementation for B class */ }
int fieldB=0;
};
class [[gnu::packed]] C : public B{
public:
void specificFunction() override { /* implementation for C class */ }
int fieldC=0;
};
class [[gnu::packed]] D : public B{
public:
void specificFunction() override { /* implementation for D class */ }
int fieldD=0;
};
class [[gnu::packed]] E : public C, public D{
void specificFunction() override {
// implementation for E class, example:
C::specificFunction();
D::specificFunction();
}
};
This would also allow you to avoid having to figure out what class which object is before calling the appropriate function.
Furthermore, assuming your original virtual class inheritance idea is what works best for your application you could restructure your data so that it's more easily accessible for caching purposes while also decreasing the size of your classes and having your functions accessible at the same time:
#include <iostream>
#include <array>
using namespace std;
struct BaseFields {
int id{0};
};
struct BFields {
int fieldB;
};
struct CFields {
int fieldB;
};
struct DFields {
int fieldB;
};
array<BaseFields, 1024> baseData;
array<BaseFields, 1024> bData;
array<BaseFields, 1024> cData;
array<BaseFields, 1024> dData;
struct indexes {
uint16_t baseIndex; // index where data for Base class is stored in baseData array
uint16_t bIndex; // index where data for B class is stored in bData array
uint16_t cIndex;
uint16_t dIndex;
};
class Base{
indexes data;
};
class B : public virtual Base{
public: void bFunction(){
//do something about "fieldB"
}
};
class C : public virtual B{
public: void cFunction(){
//do something about "fieldC"
}
};
class D : public virtual B{
};
class E : public virtual C, public virtual D{};
int main (){
std::cout<<"Base="<<sizeof(Base)<<std::endl; // 8
std::cout<<"B="<<sizeof(B)<<std::endl; // 16
std::cout<<"C="<<sizeof(C)<<std::endl; // 16
std::cout<<"D="<<sizeof(D)<<std::endl; // 16
std::cout<<"E="<<sizeof(E)<<std::endl; // 24
}
Obviously this is just an example and it assumes you don't have more than 1024 objects at a point, you can increase that number but above 65536 you'd have to use a bigger int to store them, also below 256 you can use uint8_t to store the indexes.
Furthermore if one of the structures above adds very little overhead to it's parent you could reduce the number of arrays you use to store the data, if there's very little difference in the size of objects you can just store all the data in a single structure and have more localized memory accesses. That all depends on your application so I can't give more advice here other than to benchmark what works best for your case.
Have fun and enjoy C++.
You can avoid virtual inheritance by using the following technique: make all classes except leaf classes fully abstract (no data members). All data access is through virtual getters.
class A {
virtual int & a() = 0; // private!
// methods that access a
};
class B : public A {
virtual int & c() = 0; // private!
// methods that access b
};
class C: public A {
virtual int & c() = 0; // private!
// methods that access c
};
class D: public B, public C {
int & a() override { return a_; }
int & b() override { return b_; }
int & c() override { return c_; }
int a_, b_, c_;
};
This way, you can non-vir inherit a class several times without duplicating any data members (because there are none in the first place).
In the example D has A twice, but this is not important since A is virtually empty.
With a typical implementation you should get a vptr per most derived class plus one vptr for each base class except the first at each level in your hierarchy.
Of course you now have a virtual call overhead for each member access, but nothing comes for free.
If this overhead is too much for you, and you still need polymorphism, you will probably need to implement it in a way that doesn't involve the C++ mechanism of virtual functions at all. There are quite a few ways of doing so but of course each comes with its own special drawbacks so it is hard to recommend one.

Changing base class value in c++

I'm facing a problem with a few inherited classes and their base class as well.
For example:
base class{
int x,y; // Doesnt really matter
int counter;
public:
class(int x, int y):x(x), y(y), counter(1){}
void add_counter(){counter++;}
//stuff
virtual ~base(){}
}
class1:public base{
public:
class1():base(1,2){}
}
Every of my inherited classes (which I've a few) they all pass the x,y differently from each other. And then I want this counter to increment when I call it.
The problem I'm facing is that the counter increases ONLY on that iteration. No object is being re-constructed (because I debugged). If I call the add_counter for the class1 it will increase from 1 to 2 but if I call it again it will be the same (1 to 2).
What am I missing here?
Thank you.
What am I missing here?
It seems to me that you want to keep track of the number of objects constructed whose types are derived from Base.
In that case, you need to make counter a static member variable, which will require you to make add_counter a static member function.
However, that will require you to:
Decrement the count in the destructor.
Add a copy constructor in Base to make sure that objects created using a copy constructor are also counted.
Here's a simplified version of base to do that:
class base
{
public:
base() { inrement_counter(); }
base(base const& copy) { inrement_counter(); }
virtual ~base(){ decrement_counter(); }
private:
static int counter;
static void inrement_counter() {++counter;}
static void decrement_counter() {--counter;}
}
int base::counter = 0;
If you want to keep track of the number of derived1 objects constructed, you'll need to add the bookkeeping code to derived1. You can create a class template to streamline that process.
Example:
template <typename T>
struct ObjectCounter
{
ObjectCounter() { inrement_counter(); }
ObjectCounter(ObjectCounter const& copy) { inrement_counter(); }
virtual ~ObjectCounter(){ decrement_counter(); }
static int counter;
static void inrement_counter(){++counter;}
static void decrement_counter(){--counter;}
};
template <typename T>
int ObjectCounter<T>::counter = 0;
class base
{
};
class derived1 : public base, public ObjectCounter<derived1>
{
};
class derived2 : public base, public ObjectCounter<derived2>
{
};
#include <iostream>
int main()
{
derived1 d1;
derived2 d2;
auto d3 = d2;
std::cout << d1.counter << std::endl;
std::cout << d2.counter << std::endl;
}
Output:
1
2

Polymorphism: How do you access derived class member functions?

Let's say we have a derived class from an abstract base class. A pointer to the abstract base class is declared in the main and allocated to the derived class through "new". How do you access the member functions of the derived class from a pointer to the base class (not from an object of the derived class)?
Example:
#include <iostream>
using namespace std;
class clsStudent
{
public:
virtual void display() = 0;// {cout<<"Student\n";}
};
class clsInternational : public clsStudent
{
public:
void display(){cout<<"International\n";}
void passportNo(){cout<<"Pass\n";}
};
class local : public clsStudent
{
public:
void display(){cout<<"International\n";}
void icNo(){cout<<"IC\n";}
};
int main()
{
clsStudent * s = new clsInternational;
clsStudent * s2 = new local;
s->display();
s->passportNo(); //This won't work
return 0;
}
Cheeky answer: don't. I mean, if you really need to, the answer to your technical question is the dynamic_cast operation in C++, in order to a conduct a "downcast" (cast from base to derived class).
But stepping back, this is a reasonable use case for a virtual function. Ask yourself, what is the common meaning I want to access?
In this case, we want all students to have an identifying number.
Working source code: http://ideone.com/5E9d5I
class clsStudent
{
public:
virtual void display() = 0;// {cout<<"Student\n";}
virtual void identifyingNumber() = 0;
};
class clsInternational : public clsStudent
{
public:
void display(){cout<<"International\n";}
void identifyingNumber(){cout<<"Pass\n";}
};
class local : public clsStudent
{
public:
void display(){cout<<"Local\n";}
void identifyingNumber(){cout<<"IC\n";}
};
int main()
{
clsStudent * s = new clsInternational;
clsStudent * s2 = new local;
s->display();
s->identifyingNumber();
s2->display();
s2->identifyingNumber();
return 0;
}

Access Individual classes of a derived class, which is derived from two same base class

I have a base class called Number. Class One and Two are derived from Number.
Now I define another class Three, where I need to access individual base classes from the multiple inheritance:
class Number{
protected:
int val;
public:
Number(){
val=0;
}
void Add(Number n){//Receives another Number class instance and add the value
val+=n.val;
}
};
//class One derived from Number
class One:public Number{
public:
One(){
cal=1;
}
};
//class two derived from Number
class Two:public Number{
public:
Two(){
val=2;
}
};
class Three:public One,public Two{
public:
Three(){
Two::Add(One);//--How can i pass instance of class One Here
}
};
I tried One::Number and Two::Number, but no use.
There are a few problems, first val is private and so that needs to be made protected. Next you have a diamond of death so you need to virtual public for One and Two. You also are trying to call Add using a type but you need an instance of each class:
class Number{
protected:
int val;
public:
Number(){
val=0;
}
void Add(Number& n){//Receives another Number class instance and add the value
val+=n.val;
}
};
//class One derived from Number
class One:virtual public Number{
public:
One(){
val=1;
}
};
//class two derived from Number
class Two:virtual public Number{
public:
Two(){
val=2;
}
};
class Three: public One, public Two{
public:
Three()
{
Two t1 ;
Add(t1 );//--How can i pass instance of class Two Here
}
};
It could be argued that using protected data is bad but it depends on your case but to be complete it is also a choice to keep val a private data member and use a protected constructor and that would look like this:
class Number{
private:
int val;
protected:
Number( int n ) : val(n) {}
public:
Number(){
val=0;
}
void Add(Number& n){//Receives another Number class instance and add the value
val+=n.val;
}
};
class One: virtual public Number{
public:
One() : Number( 1 ) {
}
};
class Two: virtual public Number{
public:
Two() : Number(2) {
}
};
This should work:
Two::Add(*(One*)this);
You need to create objects of the respective types.
class Three : public One, public Two
{
public:
Three()
{
Add(One());
Add(Two());
}
};
But I don't see why you need MI here. Inheriting from Number would be sufficient.
The problem is not just the syntax. But what exactly are you trying to do?
You can. But where is the instance of One declared? you have to declare it first. Remember Two::Add(One); is not a declaration rather a call statement.
What you are doing is equivalent to let's say
process_number(One);
Where process number is a function.