mardi 15 avril 2014

héritage - c ++ classe à dérivée de la classe de base même deux - Stack Overflow


Here are my classes:


class Base1
{
public:
Base1();
~Base1();

virtual void main_func()=0;
}

class D1 : public Base1
{
public:
D1();
~D1();

virtual void main_func(do_d1_main(););
}
class D2 : public Base1
{
public:
D2();
~D2();

virtual void main_func(){do_d2_main();};
}

class Base2
{
public:
Base2();
~Base2();

int m_needed_by_b1;
}
class D12B2: public Base2, public D1, public D2
{
public:
D12B2();
~D12B2();
}

Here's the problem:


Both do_main_func1() and do_main_func2() need access to member m_needed_by_b1 in Base2. How?


Could I define the virtual function main_func for each within class D12B2? That would give them access to all of D12B2, including Base2. How would I specify each separately?


I thought about adding a (protected) member in Base1, that is a pointer to a Base2 object. That would have to be set by D12B2. It seems like there might be a more elegant way to do this.


TIA


ken





It seems like there might be a more elegant way to do this.



More than likely, there is - don't use multiple inheritance.


However, if you are going to insist on using multiple inheritance, there are some things you must note: D1 and D2 should be inherited virtually in D12B2 to avoid the diamond problem. Assuming do_d1_main and do_d2_main actually means main_func, then you can implement a D12B2::main_func that will be able to access m_needed_by_b1.


But back to the original note:


It sounds like Base1 and Base2 should not exist, and D1 and D2 should be pure abstract classes. Which would leave you with


class D1
{
public:
virtual void d1_func() = 0;
};

class D2
{
public:
virtual void d2_func() = 0;
};

class D12B2 : public virtual D1, public virtual D2
{
public:
D12B2();
~D12B2();

virtual void d1_func()
{
// do something
}

virtual void d2_func()
{
// do something else
}

private:
int m_myData;
};


Here are my classes:


class Base1
{
public:
Base1();
~Base1();

virtual void main_func()=0;
}

class D1 : public Base1
{
public:
D1();
~D1();

virtual void main_func(do_d1_main(););
}
class D2 : public Base1
{
public:
D2();
~D2();

virtual void main_func(){do_d2_main();};
}

class Base2
{
public:
Base2();
~Base2();

int m_needed_by_b1;
}
class D12B2: public Base2, public D1, public D2
{
public:
D12B2();
~D12B2();
}

Here's the problem:


Both do_main_func1() and do_main_func2() need access to member m_needed_by_b1 in Base2. How?


Could I define the virtual function main_func for each within class D12B2? That would give them access to all of D12B2, including Base2. How would I specify each separately?


I thought about adding a (protected) member in Base1, that is a pointer to a Base2 object. That would have to be set by D12B2. It seems like there might be a more elegant way to do this.


TIA


ken




It seems like there might be a more elegant way to do this.



More than likely, there is - don't use multiple inheritance.


However, if you are going to insist on using multiple inheritance, there are some things you must note: D1 and D2 should be inherited virtually in D12B2 to avoid the diamond problem. Assuming do_d1_main and do_d2_main actually means main_func, then you can implement a D12B2::main_func that will be able to access m_needed_by_b1.


But back to the original note:


It sounds like Base1 and Base2 should not exist, and D1 and D2 should be pure abstract classes. Which would leave you with


class D1
{
public:
virtual void d1_func() = 0;
};

class D2
{
public:
virtual void d2_func() = 0;
};

class D12B2 : public virtual D1, public virtual D2
{
public:
D12B2();
~D12B2();

virtual void d1_func()
{
// do something
}

virtual void d2_func()
{
// do something else
}

private:
int m_myData;
};

Related Posts:

0 commentaires:

Enregistrer un commentaire