Thread: Static members in class

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    15

    Static members in class

    I have this code which i want called in a function, in my class, but it wont't work as i need a seperate set of variables for each time i call the function.

    here is the code
    Code:
    static ConVar Cvar_m(pName, pStartVal, NULL, Description);
    ConVar *pCvar_m = &Cvar_m;
    pCvar_m->SetNext(0);
    g_pCvar->RegisterConCommandBase(pCvar_m);
    I want this code in my add function of my class C_Cvars..
    but if i want to add 2 or more then i get a crash because i need a different static ConVar for each time i add.

    So how can i make a function which will use different variable names each time.
    I thought of an array or macro.
    I would really appreciate some help on this, thanks.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I'm not shure that I understand what you're trying.
    looks like you trying to get the address of a local variable, but since that doesn't work you decided to make the variable static.
    But now all your registered pointers point to the same location. Right ?
    If that is your problen then you have to dynamically create the pointer to a ConVar.

    like
    Code:
    ConVar *pCvar_m = new ConVar;
    pCvar_m->SetNext(0);
    g_pCvar->RegisterConCommandBase(pCvar_m);
    Trouble is that you should delete that variable at some time. You will have to find a way to delete it if you don't need it amymore.
    Something like
    Code:
    g_pCvar->DeRegisterAll();
    Kurt
    Last edited by ZuK; 12-30-2006 at 05:44 AM.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    15
    thanks for the help, i did also try using it as an object but decided against that due to the deletion of object.

    this code bassically adds a variable into a computer game externally.

    but my problem is in order to add 2 i have to do this

    Code:
    static ConVar Cvar_enabled("PH_Enabled", "0", 0, "Enable or Disable ");
    static ConVar Cvar_menu("PH_Menu", "0", 0, "Enable or Disable the menu!");
    
    ConVar *pCvar_enabled = &Cvar_enabled;
    ConVar *pCvar_menu = &Cvar_menu;
    
    pCvar_enabled->SetNext(0);
    pCvar_menu->SetNext(0);
    
    g_pCvar->RegisterConCommandBase(pCvar_enabled);
    g_pCvar->RegisterConCommandBase(pCvar_menu);
    obviously this is messy, and i want to avoid it.

    so i have an add function which i pas this info for each i want to add:
    ("PH_Enabled", "0", 0, "Enable or Disable the hack!")

    but i need to have a seperate Cvar_m amd pCvar_m for each.
    so simply having the info added multiple times will not work.

    i need to change the add function so Cvar_m actually will have a different name each time so i get a variable for each time i add, not just use the same variable inside the function to store the data.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    If you never want to add more then 2 variables then your approach isn't that bad.
    If you want to make it more general then I would dynamically create that ConVar's and store the pointers in some container to be able to release them whenever needed.
    Kurt

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    15
    hmmm,

    i will have lots, so i want a streamlined approach lol.

    and again, i don't want to have to predefine the pointers.
    i just want the one function to do it all, no matter how many entry's i put in.
    well i really hope that can be done.
    Thanks for the continuing help.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You say that you register variables for an external game, so there should also be a function to deregister this varables again. So your approach to use static variables is not right anyway because this way you would never deregister your variables.
    So I think the only clean way would be to dynamically create this variables, put the pointers into some container and deregister the variables before you delete them.
    Kurt

  7. #7
    Registered User
    Join Date
    Dec 2006
    Posts
    15
    I'm not entirely sure how the variables get deregistered, but all i know is, if i try any other method of registering them, it will just result in a crash

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Are you trying to modify Valve Software's Source engine by any chance? if so, don't touch the new operator, the engine handles all its memory management. If you try to go above its head, then weird stuff happens! The new operator is overloaded, and behaves differently to the way you'd normally expect - The entire SDK only uses 'new' once among its lebenty-zillion lines of code and several hundred .cpp files (Presumably a call which interfaces directly with the engine)

    IMHO, the only way to do what you're asking is to either type that console variable stuff out individually, or design some hideous, ugly macro to do it for you (The SDK is already littered with more than enough mind-bogglingly incomprehensable macros as it is..)

    Aside from this, static variables are probably the only way to create ConVar objects that the engine recognises - like most of the entity factory objects which the SDK uses, the engine needs to know the address of the ConVar before the DLL loads, and for various reasons, this can only be done using static initialisation.

    (If you dig deep enough around the LINK_ENTITY_TO_CLASS macro, you'll see templated code which creates a unique static 'Factory' variable for every single Class which derives from CBaseEntity , and records the class name to the 'entity dictionary' container )

  9. #9
    Registered User
    Join Date
    Dec 2006
    Posts
    15
    thankyou so much for that info. I will have a look later today, i did try making a macro for it but it still resulted in a crash. I will have a look at theirs though.

  10. #10
    Registered User
    Join Date
    Dec 2006
    Posts
    15
    my macro code still results in a crash

    this is it.

    Code:
    #define MACRO_CVARS(pName, pStartVal, Description)							\
    	{																		\
    		static ConVar Cvar_##pName(#pName, #pStartVal, NULL, #Description);	\
    		ConVar *pCvar_##pName = & Cvar_##pName;								\
    		pCvar_##pName->SetNext(0);											\
    		g_pCvar->RegisterConCommandBase(pCvar_##pName);						\
    	}
    and i call it in this function

    Code:
    void C_Cvars::AddCvar(const char* pName, const char* pStartVal, const char* Description)
    {
    	MACRO_CVARS(pName, pStartVal, Description);
    }
    any ideas?
    thanks

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Out of curiousity, why do you need anything after the static declaration? As far as I can tell from here http://developer.valvesoftware.com/w...onsole_Control - you only need to declare that variable, and the rest is done for you..

  12. #12
    Registered User
    Join Date
    Dec 2006
    Posts
    15
    well normally yes...
    but I'm not making a mod that is loaded by the source engine. In this case, all convars would be registered yes.

    But I am making an external program which hooks in and needs to use a cvar class to register it myself. I probably couldv'e hooked into the rigth spot to have the source engine look up my convars.. but its easier just to register them the way i am doing it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Free.
    By chakra in forum C Programming
    Replies: 9
    Last Post: 12-15-2008, 11:20 AM
  2. initializing static members
    By steve1_rm in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2008, 05:45 AM
  3. How do i do this? (static structure inside class)
    By 39ster in forum C++ Programming
    Replies: 4
    Last Post: 11-17-2008, 03:14 AM
  4. Replies: 10
    Last Post: 07-26-2008, 08:44 AM
  5. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 09:54 AM