Sign in to follow this  
Silly_con

Inheritance question C++

Recommended Posts

Silly_con    136
My first problem is:

class IBase {
public:
	virtual void func1() = 0;
	virtual void func2() const = 0;
	virtual void func3() = 0;
};

class Derived : public IBase {
public:
	void func1() { ... }
	void func2() const { ... }
	void func3() { ... }

	void func4() { ... }
};

IBase *b = new Derived();
b->func1(); // ok
b->func2(); // ok
b->func3(); // ok
b->func4(); // compiling error, why ?
delete b;

My second question, I have heard that avoid multiple inheritance is a good practice, but is a good practice do this for almost all classes ? class A : public ErrorHandling<A>, public ClassInformation<A>, public InterfaceA {}; ErrorHandling is a class like the one described in effective C++, ClassInformation is a class that have methods to count instances, get class size, get this pointer and such of things. thnx

Share this post


Link to post
Share on other sites
Enigma    1410
1. b is a pointer to IBase. Imagine you had:
class OtherDerived
:
public IBase
{

public:
void func1();
void func2() const;
void func3();
// no func4

};

IBase* b = new OtherDerived();
b->func1(); // ok
b->func2(); // ok
b->func3(); // ok
b->func4(); // of course it's an error OtherDerived has no func4!
delete b;


Since calling func4 only makes sense for Derived objects it can only be called for objects which are definitely Derived objects, not IBase objects which may not have a func4 member function.

By the way you should have a virtual destructor for IBase if you intend to delete derived objects via a pointer-to-base.

Enigma

Share this post


Link to post
Share on other sites
Xai    1848
The derived instance HAS all 4 functions, but the compiler is not allowed to KNOW that it has the func4 when using a base class pointer ...

to bring this point home, consider this function:

void TestFunc(IBase *object)
{
object->func3(); // fine
// it should be fairly obvious that this next cannot compile
object->func4();
// because the compile, using STATIC, COMPILE-TIME type checking
// is expecting an object of type IBase, or ANY derived class, not
// a specific derived class
}

and yes, as mentioned before, you NEED a virtual destructor.

since you are doing this:

IBase *object = new Derived();

you have just guaranteed yourself an error if you do not have a virtual destructor.

You can fix it two ways:

Derived *derived = new Derived();
IBase *base = derived;

// do stuff here

delete derived;

OR

add a virtual destructor to IBase

Share this post


Link to post
Share on other sites
darookie    1441
Quote:
Original post by Silly_con
My second question, I have heard that avoid multiple inheritance is a good practice, but is a good practice do this for almost all classes ?

No. Multiple inheritance has its issues (even in C++). Problems are diamond hierarchies virtual inheritance and such.
Also you need to carefully decide what you are trying to model.
The class information from your example is rather aggregation (A contains class information and is not class information).
The public interfaceA can also be modelled by the PIMPL idiom a.s.o. In short: there is no single technique that is best suited for all problems. Get familiar with many approaches (functional, policy-based, generic programming, ...) and select the one that is best suited to solve your problem efficiently.

Regards,
Pat.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this