It is currently Sun Jun 25, 2017 12:10 pm

All times are UTC - 5 hours




 Page 1 of 1 [ 5 posts ] 
Author Message
 Post subject: Working with structures using typedef or static.
PostPosted: Mon Nov 21, 2011 9:09 pm 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 447
Im working with a structure that, when generated I want only one instance of it for the whole program. the structure is declared at global scope

someheader.h
#ifndef ...
#define ...

struct Singleton {
int iVar;
float fVar;

Singleton( int _iVar, float _fVar ) {
iVar = _iVar;
fVar =_fVar;
}
};

#endif ...

someclass.h
#ifndef ...
#define ...
#include "someheader.h"

class SomeClass {

private:
std::map< Enum, (pointer to singleton) > _vMap;

public:
void GenearteOneInstanceOfMap(); // Adventually this function will be moved to a global scope
// But and needs to be called first to populate this map. But I still want to have a member in this class
// that only points to it so _vMap will become _vpMap. This class will be created MANY times and I don't
// want say 1000 _vMaps created with each instance of the class generating the same list. I want only
// One instance of this member variable that will point to the map generated. Would a function pointer work?

};
#endif

somclass.cpp
#include "someclass.h"

SomeClass::GenerateOneInstanceOfMap() {
_vMap.insert( make_pair( Enum1, ReferenceToStruct( param a, param b ) ) );
...
...
...
_vMap.insert( make_pair( EnumLast, ReferenceToStruct( param a, param b ) ) );
}

// end file .cpp

This is the general idea of what I want to happen, do I use a typdef of the struct? make the struct static, make a typdef of the struct then make a static pointer to that typedef.... etc.
LOL, the compiler has been running haywire for me, and can not quite seem to get it to work
correctly. I do believe this can be done, unless if the fact that the struct has a constructor
and I am trying to pass a pointer into the std map... Any bit of help would be great and Thank You In Advance!!! :) ...


Offline
 Profile  
 
 Post subject: Re: Working with structures using typedef or static.
PostPosted: Tue Nov 22, 2011 5:54 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
If your map is the only thing that you want your "singleton" to contain then you can do this:

//in class header file
class MyClass {
private:
  static std::map<A,B> s_map;
public:
  MyClass();
  ~MyClass();

   static void initializeMap();
};

//in class source file
std::map<A,B> MyClass::s_map;

void MyClass::initializeMap() {
  //put stuff into your map
  s_map[A1] = B1;
  s_map[A2] = B2;
  s_map[A3] = B3;
  s_map[A4] = B4;
}

//in source code
//call this once at the start to fill your map
MyClass::initializeMap()

//Now when you go an use the MyClass, it will always come with the s_map populated for you with the values you wanted.


Offline
 Profile  
 
 Post subject: Re: Working with structures using typedef or static.
PostPosted: Wed Nov 23, 2011 1:56 am 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 447
Thank you for your help Marek. I was able to setup up the code the way you showed me.

Just to make it a little more clear. The map itself should be the singleton.

The idea is MyClass will be created multiple times, and it needs this map of default values. The values in the map will not change. And when MyClass is created dynamically with new memory where it might be created 100 times. I just want 1 map as a reference that the class can pull data from. This way the map isn't being recreated 100 times and I don't need 100 copies of this map.

The question I have now, just to be sure is. Does or will the implementation you showed or explained to me, have this behavoir ?


Offline
 Profile  
 
 Post subject: Re: Working with structures using typedef or static.
PostPosted: Wed Nov 23, 2011 5:52 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
skilz80 wrote:
Does or will the implementation you showed or explained to me, have this behavoir ?


Yes, that is exactly how a static class member variable works.


Offline
 Profile  
 
 Post subject: Re: Working with structures using typedef or static.
PostPosted: Wed Nov 23, 2011 2:20 pm 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 447
Thank you for the reply, I seen the use of this throughout the GameEngine and GhostToast, but when I was trying to implement it in my own project, with the passing around of certain variables. I was able to compile and build, but was getting runtime errors that were thrown from the std library. So I went away from my Large scale project and tried to simulate it with a simple struct and class and was getting the same behavior. Now that you explained to me how to set this up properly; All works well. :)


Offline
 Profile  
 
Display posts from previous:  Sort by  
 Page 1 of 1 [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  

cron