Jump to content
  • Advertisement
Sign in to follow this  
OklyDokly

Can anyone see what's wrong with this code?

This topic is 4818 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!