Skip to main content
GameDev.net gamedev.net
🔒 Locked

Can anyone see what's wrong with this code?

Started by OklyDokly Sep 9, 2005 at 5:56 PM 2 replies 1k views
Original Post
OklyDokly
OklyDokly
Hey I thought I knew C++ pretty well, but this code seems to be crashing on the 3rd line of the main function and I can't see why. Anyone got any ideas?

// Me class
class Me
{
protected:
	int m_I;
public:
	Me( int i ) { m_I = i; }
	virtual ~Me() {}

	virtual void Print() = 0;
};


// Inherited you class
class You : public Me
{
	int j;
public:
	You() :	 Me( 0 )
	{
		j = m_I;
	}

	virtual ~You() {}

	virtual void Print()
	{
		printf( "%i\n", j );
	}
};



int _tmain(int argc, _TCHAR* argv[])
{
	Me* you = new You[ 3 ];
	you[0].Print();
	you[1].Print();
	you[2].Print();

	return 0;
}

Thanks
Enigma
Enigma
Arrays cannot be treated polymorphicly. In a Type * array the elements have static type Type and an index of the form array[index] is calculated equivalently to *static_cast< Type * >(static_cast< char * >(array) + index * sizeof(Type)). The expression new DerivedType[size] creates an array with members whose type is DerivedType. Elements in this array are located equivalently to *static_cast< DerivedType * >(static_cast< char * >(array) + index * sizeof(DerivedType)). Since sizeof(Type) usually isn't the same as sizeof(DerivedType) the indices do not match up. That might be a little heavy for you, so here's a diagram:
  sizeof(Type)    <------>---+--------+--------+--------+--------+---   |        |        |        |        |---+--------+--------+--------+--------+---       ^        ^        ^        ^    array[0] array[1] array[2] array[3] sizeof(DerivedType)    <---------->---+------------+------------+------------+------------+---   |            |            |            |            |---+------------+------------+------------+------------+---

So the array is accessed as if it were laid out in the top diagram, but in reality it is laid out as in the bottom diagram. The solution is to use an array of pointers to objects:
int _tmain(int argc, _TCHAR* argv[]){	Me * * you = new You*[ 3 ];	you[0] = new You();	you[1] = new You();	you[2] = new You();	you[0]->Print();	you[1]->Print();	you[2]->Print();	return 0;}

(Of course, this creates a memory pool since nothing gets deleted and is not exception safe, but it's only a trivial example).

Enigma
OklyDokly
OklyDokly
Of course, silly me :)

Thanks for your help though, annoying how the fix is always a little more complicated :S...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.