Multiple inheritance of abstract classes with same member functions 
Author Message
 Multiple inheritance of abstract classes with same member functions

Multiple inheritance of abstract classes with same member functions :
Class A
{
  virtual long print()=0;

Quote:
}

Class B
{
  virtual long print()=0;

Quote:
};

Class C : public A,public B
{

Quote:
}

How can I know specify two different implementations for virtual function
print() in interface A and interface B ?

I thought doing it as follows, but it doesn't compile :
Class C : public A,public B
{
  virtual long A::print() {...};
  virtual long B::print() {...};
  virtual long print() {...};

Quote:
}

To situate the problem :
Suppose that you have a COM object that exposes two interface that by
coincidence have a member function with the same signature (using ATL =>
multiple inheritance ) and you want to give two different implementations.

PS: Could you also be so kind to reply to the author also.
PS: I am using Microsoft VC++

Thanks in advance,
Alex Goeman



Tue, 25 Dec 2001 03:00:00 GMT  
 Multiple inheritance of abstract classes with same member functions

Quote:
> I thought doing it as follows, but it doesn't compile :
> Class C : public A,public B
> {
>   virtual long A::print() {...};
>   virtual long B::print() {...};
>   virtual long print() {...};
> }

In your case, it does not make sense to give different implementations for
the same function ! What would then be in the vtable for a class C object ?

An object of given type (run-time type) has only ONE implementation of a
given function at a time.

If you want to change that, then the vtable must change, which changes the
nature of the object.

Polymorphism does not mean "Multimorphism"

To take an old C++ example :

class Employee()
{
public :
   virtual int Salary() = 0;

Quote:
};

class Engineer : public Employee
{
public :
    virtual int Salary() { return 45; };

Quote:
}

class Account_Manager : public Employee
{
public :
    virtual int Salary() {return 90; };

Quote:
}

The two classes can't have three implementations of the same function
Salary()

Benjamin



Tue, 25 Dec 2001 03:00:00 GMT  
 Multiple inheritance of abstract classes with same member functions
Alex,

This is a trick I just recently learned from the book "ATL Internals".
Credit is given to Brent Rector, Chris Sells, and Tim Ewald for the
following, which I will paraphrase into your problem description.

1. Create two structures as follows:

struct _A : public A {
  virtual long print() { return Aprint(); }
  virtual long Aprint()=0;

Quote:
}

struct _B : public B (
  virtual long print() { return Bprint(); }
  virtual long Bprint()=0;

Quote:
}

2. In the derived class implement the function as follows:

class C : public A, public B {
public:
  ...
  long Aprint();
  long Bprint();
  ...

Quote:
}

--
David E. Smith
MCSE, MCT, MVP


Quote:
> Multiple inheritance of abstract classes with same member functions :
> Class A
> {
>   virtual long print()=0;
> }

> Class B
> {
>   virtual long print()=0;
> };

> Class C : public A,public B
> {
> }

> How can I know specify two different implementations for virtual function
> print() in interface A and interface B ?

> I thought doing it as follows, but it doesn't compile :
> Class C : public A,public B
> {
>   virtual long A::print() {...};
>   virtual long B::print() {...};
>   virtual long print() {...};
> }

> To situate the problem :
> Suppose that you have a COM object that exposes two interface that by
> coincidence have a member function with the same signature (using ATL =>
> multiple inheritance ) and you want to give two different implementations.

> PS: Could you also be so kind to reply to the author also.
> PS: I am using Microsoft VC++

> Thanks in advance,
> Alex Goeman




Tue, 25 Dec 2001 03:00:00 GMT  
 Multiple inheritance of abstract classes with same member functions
Actually, I think this example is slightly wrong -

the derived class should say:

class C: public _A, public _B

not

class C: public A, public B

The tecnique is also discussed in Scott Meyer's "Effective C++" item #43 - it's
a pretty good C++ book.

Quote:

> Alex,

> This is a trick I just recently learned from the book "ATL Internals".
> Credit is given to Brent Rector, Chris Sells, and Tim Ewald for the
> following, which I will paraphrase into your problem description.

> 1. Create two structures as follows:

> struct _A : public A {
>   virtual long print() { return Aprint(); }
>   virtual long Aprint()=0;
> }

> struct _B : public B (
>   virtual long print() { return Bprint(); }
>   virtual long Bprint()=0;
> }

> 2. In the derived class implement the function as follows:

> class C : public A, public B {
> public:
>   ...
>   long Aprint();
>   long Bprint();
>   ...
> }

> --
> David E. Smith
> MCSE, MCT, MVP



> > Multiple inheritance of abstract classes with same member functions :
> > Class A
> > {
> >   virtual long print()=0;
> > }

> > Class B
> > {
> >   virtual long print()=0;
> > };

> > Class C : public A,public B
> > {
> > }

> > How can I know specify two different implementations for virtual function
> > print() in interface A and interface B ?

> > I thought doing it as follows, but it doesn't compile :
> > Class C : public A,public B
> > {
> >   virtual long A::print() {...};
> >   virtual long B::print() {...};
> >   virtual long print() {...};
> > }

> > To situate the problem :
> > Suppose that you have a COM object that exposes two interface that by
> > coincidence have a member function with the same signature (using ATL =>
> > multiple inheritance ) and you want to give two different implementations.

> > PS: Could you also be so kind to reply to the author also.
> > PS: I am using Microsoft VC++

> > Thanks in advance,
> > Alex Goeman




Tue, 25 Dec 2001 03:00:00 GMT  
 Multiple inheritance of abstract classes with same member functions
Paul,

Thank you for catching my typo. You are, of course, correct. The error was
mine; I can't even copy correctly from someone else's book.

--
David E. Smith
MCSE, MCT, MVP


Quote:
> Actually, I think this example is slightly wrong -

> the derived class should say:

> class C: public _A, public _B

> not

> class C: public A, public B

> The tecnique is also discussed in Scott Meyer's "Effective C++" item #43 -
it's
> a pretty good C++ book.


> > Alex,

> > This is a trick I just recently learned from the book "ATL Internals".
> > Credit is given to Brent Rector, Chris Sells, and Tim Ewald for the
> > following, which I will paraphrase into your problem description.

> > 1. Create two structures as follows:

> > struct _A : public A {
> >   virtual long print() { return Aprint(); }
> >   virtual long Aprint()=0;
> > }

> > struct _B : public B (
> >   virtual long print() { return Bprint(); }
> >   virtual long Bprint()=0;
> > }

> > 2. In the derived class implement the function as follows:

> > class C : public A, public B {
> > public:
> >   ...
> >   long Aprint();
> >   long Bprint();
> >   ...
> > }

> > --
> > David E. Smith
> > MCSE, MCT, MVP



> > > Multiple inheritance of abstract classes with same member functions :
> > > Class A
> > > {
> > >   virtual long print()=0;
> > > }

> > > Class B
> > > {
> > >   virtual long print()=0;
> > > };

> > > Class C : public A,public B
> > > {
> > > }

> > > How can I know specify two different implementations for virtual
function
> > > print() in interface A and interface B ?

> > > I thought doing it as follows, but it doesn't compile :
> > > Class C : public A,public B
> > > {
> > >   virtual long A::print() {...};
> > >   virtual long B::print() {...};
> > >   virtual long print() {...};
> > > }

> > > To situate the problem :
> > > Suppose that you have a COM object that exposes two interface that by
> > > coincidence have a member function with the same signature (using ATL
=>
> > > multiple inheritance ) and you want to give two different
implementations.

> > > PS: Could you also be so kind to reply to the author also.
> > > PS: I am using Microsoft VC++

> > > Thanks in advance,
> > > Alex Goeman




Tue, 25 Dec 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Multiple inheritance of abstract classes with same member functions

2. Questions about inheritance and implementing abstract classes

3. Autogenerating Inherited (abstract class or interfaces) members code

4. error C2259 cannot instantiate abstract class due to following members

5. Public static members in abstract base classes?

6. how to initialize a class inheritance class function

7. Abstract classes and abstract properties?

8. Multiple IDispatch/Class Inheritance?

9. Casting derived class to base gives incorrect results w/multiple inheritance

10. using multiple inheritance of MFC base classes?

11. Multiple Inheritance for MDI classes - can we?

12. Casting derived class to base gives incorrect results w/multiple inheritance

 

 
Powered by phpBB® Forum Software