[comp.lang.c++] down cast

tomas@inmic.se (Tomas Lundstrom) (04/08/91)

A simple question:

Assume I have a class hierarchy like this:

class base {
	.
	.
	.
	private:
		int some_data;
	public:
		void some_func ();
}

class derived : public base {
	// no more data members
	.
	.
	public:
		void another_func ();	// only a bunch of functions added ...
}

The derived class introduces NO additional data members. NO virtual
functions or multiple inheritance is involved.
Now, assume this code fragment including a down cast:

	base *b = new base;

	((derived*)b)->another_func (); // legal and portable ?

This is guaranteed to work if an object of class 'base' and 'derived'
are of the same size. The question is: is it reasonable to assume that
the objects indeed ARE of the same size in all (major) existing
implementations ?

I want to be sure that this cast works before proceeding in my
project.

Yes, I know it's ugly, potentially implementation dependent and un-OO,
but so is the project I'm in ... :-) :-)

thanks in advance,

Tomas