Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

cowsarenotevil

Dumb question

This topic is 5732 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

I know this seems dumb, but if you defined a class like this:
  
class thingy
{
    int x;
    thingy otherthingy;
};
[source]

Would that crash your computer, givin that you would have thingy.otherthingy.otherthingy.otherthingy.etc...?  
-~-The Cow of Darkness-~-

Share this post


Link to post
Share on other sites
Advertisement
I don''t see how it could possibly compile, since the size of an object of that class would be infinite (and it can''t very well compile if the space needed to allocate an object cannot be calculated).

Share this post


Link to post
Share on other sites
It''s not necessarily a dumb question, so make your topics meaningful instead. All of us have to learn some time.

You shouldn''t be able to compile that chunk of code because the compiler does not know the full definition of a thingy by the time it gets to the "otherthingy" member.

If you are trying to have an object linked to another object of the same type, then use a pointer to a thingy:

class thingy {
int v;
thingy* next_thingy;
};

The compiler is able to handle this because a pointer is just an address to an object, and the compiler doesn''t need to know the definition of the object at that point.

Regards,
Jeff

Share this post


Link to post
Share on other sites
Although in C it won''t work, the idea of a recursive type is actually perfectly acceptable. For instance (and this might not be the best example coz it''s obvious how the implementation goes, but conceptually...) a list in Lisp can be considered to be a recursive type (paraphrased into C++ notation):

struct List
{
int data;
List rest_of_list;
};

Along with a particular ''null'' list which enables you to stop the recursion.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!