Jump to content
  • Advertisement

Archived

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

TWilliams

Singleton Class Issues

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

Ok, this is driving me crazy... I''ve been tasked with writing the Object Manager and Animation Managers for our final project here at school, which in itself isn''t an issue. Since I only one single instances of each, I made them singleton classes. Here is the kicker though, when the Object Manager loads an object, it has to tell the Animation manager to link up the animations. In order for that to happen, the Animation manager is going to need to be able access data from the Object Manager (so that it can get the start/end keyframes). So basically... Singleton A is aware of Singleton B, and Singleton B is aware of Singleton A. This does not appear to work. If I do the #include for the header file inside the singleton''s #ifndef section, one of the classes is suddenly not aware of the other one''s existance (tells me that it''s ''missing storage-class or type specifiers''), if I move the #include outside the #ifndef, I get heap limit warnings ''#include nesting level is 363 deep; possible infinite recursion'' No one else in my group, or the other group, has done anything like this (other group isn''t doing an object manager) and the two teachers I''ve asked aren''t entirely sure what the problem is either. I''m going to be asking one of the other teachers later today, but I really don''t want to sit here on my butt waiting any longer than I have too (Anim Manager needs to be done on the 11th). If -anyone- has any ideas, I''d appreciate it! Heck, if any of you with the idea that fixes this is going to GDC on the 6th through the 8th, I''ll buy you a drink! Just one though, I''m only a poor student ;0)

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Don''t include the header file for class A in Class B. Them in the cpp file, this means you can''t have a class member that is a pointer to one of the classes (well, you can have one, but not both).

Really, in one class you wind up doing this in every function that uses the one singleton class:
//GetHandle to SingletonClass
CSingleton2* Singleton2 = NULL;
Singleton2 = Singleton2->GetObject();


and the other class you can have the pointer as a member.


CSingleton2.cpp

  
#include "Singleton1.h"
#include "Singleton2.h"

//////////////////////////////////////////////////////////////////////

// Construction/Destruction

//////////////////////////////////////////////////////////////////////


CSingleton2::CSingleton2()
{

}

CSingleton2::~CSingleton2()
{

}

CSingleton2* CSingleton2::GetObject()
{
static CSingleton2 obj;
return &obj;
}

void CSingleton2::SomeMember()
{
//GetHandle to SingletonClass

CSingleton1* Singleton1 = NULL;
Singleton1 = Singleton1->GetObject();

Singleton1->SomeMember();

}


Singleton1.cpp

  
#include "Singleton1.h"
#include "Singleton2.h"

//////////////////////////////////////////////////////////////////////

// Construction/Destruction

//////////////////////////////////////////////////////////////////////


CSingleton1::CSingleton1()
{

}

CSingleton1::~CSingleton1()
{

}

CSingleton1* CSingleton1::GetObject()
{
static CSingleton1 obj;
return &obj;
}

void CSingleton1::SomeMember()
{
//GetHandle to SingletonClass

CSingleton2* Singleton2 = NULL;
Singleton2 = Singleton2->GetObject();

Singleton2->SomeMember();

}

--------------------Configuration: Nothing - Win32 Debug--------------------
Compiling...
Singleton1.cpp
Singleton2.cpp
Generating Code...
Linking...

Nothing.exe - 0 error(s), 0 warning(s)

Share this post


Link to post
Share on other sites
I think TWilliams'' problem is solved by forward referencing. I don''t think it''s the interaction that''s his problem, but rather simply making two classes aware of each other''s existences:

  
// forward reference


class Singleton2;
 
class Singleton1
{
// you don''t need a full type definition to declare pointers or references


void SomeFunction( const Singleton2 & s2 );
// ...further declarations


};
 
 
// actual Singleton2 class declaration


class Singleton2
{
// since Singleton1 is already full declared, there''s no problem with

// this reference


void SomeFunction( const Singleton1 & s1 );
// ...further declarations


};

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Oluseyi
I think TWilliams'' problem is solved by <em>forward referencing</em>. I don''t think it''s the interaction that''s his problem, but rather simply making two classes aware of each other''s existences.


Very interesting, I never even thought about that.

My engine is VERY modular so I tend to keep the class members relevant to the class itself. I use the Singleton pattern for faster restructuring and a messaging system for generic communication and synchronization.

I used my source code as an example (well, a generic project based on it).

Share this post


Link to post
Share on other sites
Cool guys, thanks...

Forward declaration, eh? I don''t think I''ve ever done that. Yet another thing that I''ll need to look into... anyway, I''ve got it working with Anonymous Poster''s suggestion. But I''m -defiantly- going to keep Oluseyi''s input on file as well. Never know when you''ll need something like that.

Share this post


Link to post
Share on other sites
Ok, I''m actually going to use the forward decleration... it seems like it might be a little bit faster than going ObjectManager::GetInstance()->GetObject(#)->Object all the time Thanks for your suggestions though, definatly learned something today, and thats the important thing.

That, and now my team leader won''t kill me...

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.

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!