alternative to multiple inheritance in c++

  • Thread starter Thread starter weird0
  • Start date Start date
W

weird0

Suppose i have two classes A and B:

class A
{

public method_a();

}

class B
{

public method_b();
}

if i want to have multiple inheritance in c# like C++ so i can have
access to both methods:

class C : public A,B
{

public method_c();
}

HOw can i achive that in c#?
 
weird0 said:
Suppose i have two classes A and B:

class A
{

public method_a();

}

class B
{

public method_b();
}

if i want to have multiple inheritance in c# like C++ so i can have
access to both methods:

class C : public A,B
{

public method_c();
}

HOw can i achive that in c#?

You just can't. In general, prefer composition to inheritance - so
class C would have an instance of A and an instance of B as member
variables.

Note that although .NET has no multiple inheritance of implementation,
you can implement multiple interfaces.
 
Suppose i have two classes A and B:

class A
{

public method_a();

}

class B
{

public method_b();

}

if i want to have multiple inheritance in c# like C++ so i can have
access to both methods:

class C : public A,B
{

public method_c();

}

HOw can i achive that in c#?

I would recommend changing A & B both into interfaces IA and IB. Or
you may also let B inherit A and let C inherit B.

Freiddy
http://fei.yuanbw.googlepages.com/
 
Back
Top