polymorphism

J

Jim Langston

hi experts,


just out of curosity why polymorphism is allowed to work only with
pointers and references to the objects and not with the objects itself.


what are the reasons behind forbidding this behavior.


is this restriction anyway related to the ABC.

thanks

Consider this:

class base
{
public:
int a;
};

class derived: public base
{
public:
int b;
};

Now lets try to use them as polymorphic not using pointers:

base MyArray[3]; // We just allocated how much memory? sizeof(base) * 3 or
sizeof(derived) * 3?
derived MyDerived;

MyArray[1] = MyDerived; // but if the array is sizeof(base) * 3 then
MyDerived won't "fit"

Okay, so say you make the requirement that allocation for an object must be
as large as any derived of that class.
But now we have:

class derived2: public base
{
public:
int largearray[100000];
};

Would you require
base MyArray[3];
to allocate storage for those bytes it could never need?

A lot easier to just do as the current specifications state, polymorphism is
done with pointers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top