Jump to content
  • Advertisement
Sign in to follow this  
Oranda

C++ Class Recursion

This topic is 4812 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'm trying to make two different C++ classes reference each other, but can't figure out how. Basically, I want the method of one class to return an instace of the other class, and that class to be able to return an instance of the first, like this:
//class1.h
public class class1
{
  class2 getClass2(int index);
} 

//class2.h
public class class2
{
  class1 getClass1(int index);
} 

I'm using DevC++ with MingW for this project. Any ideas on how to get this to work, without having to do something messy like have one or the other return a void?

Share this post


Link to post
Share on other sites
Advertisement

//////////////////////////
//class1.h

class class2;

class class1
{
class2 getClass2(int index);
};

//////////////////////////
//class2.h
class class 1;

class class2
{
class1 getClass1(int index);
};


Must-read article.

Share this post


Link to post
Share on other sites
Thanks man, consider my b0x0rz r0x0r3d ^^. I actually meant them to be pointers but forgot the "*"'s. Anyways that article had the perfect answer, I never knew you could prototype a class pointer. Very cool.

Share this post


Link to post
Share on other sites
Quote:
Original post by Oranda
Thanks man, consider my b0x0rz r0x0r3d ^^. I actually meant them to be pointers but forgot the "*"'s. Anyways that article had the perfect answer, I never knew you could prototype a class pointer. Very cool.


An incomplete type (which is what the class declaration results in) can be used to define a pointer, a reference, or to declare a function taking or returning that type. Of course, actually creating or using a variable requires a complete type.

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!