how can i use a static variable in private session of a class

This is a discussion on how can i use a static variable in private session of a class within the C++ Programming forums, part of the General Programming Boards category; hello, im trying to access a variable in 2 or 3 member functions and let them modify it, and the ...

  1. #1
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493

    how can i use a static variable in private session of a class

    hello, im trying to access a variable in 2 or 3 member functions and let them modify it, and the data should be retained (thats why im using "static" for this purpose) so that whenever any function ( eg. member function #2 )needs updated variable "org" for example(that has been calculated and had been modifed in another function(and again should be modified and be used in some others), there is no problem to get the data ! .
    but . whenever i write" static int org" , and use "org" in member functions, the compiler generates error! stating that "|1131|undefined reference to `CLA:rg'|
    how can i solve this ?

    by the way , declaring a mapped array this way , does it retain the data each time we call it ? or it just resets! each time it exits a function!
    Code:
    class CLA
    {
    public:
    CLA (string);
    void InputString(string);
    void Parser(); 
    void Fetch();  
    void AnalizeString(); 
    void Tokenizer(); 
     void machinLanguageConvertor();
    void MemoryDumper(); 
    void ExecutionEngine();
    private:
    static int org;
    map<int,string>::iterator usrstr_it;
    map<int,string>  StringCollector;
    
    };
    and how can i pass a mapped array to a function ! i mean im trying to use a mapped array , so is it right to do :CLA (StringCollector); ?

    many tanx in advance
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  2. #2
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    90
    redefine org...

    put in global:
    Code:
    int CLA::org;

  3. #3
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    90
    so is it right to do :CLA (StringCollector)
    no...
    constructor CLA accepts string...
    change it into:
    Code:
    CLA(map<int, string>);
    if i'm not mistaken...

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    Quote Originally Posted by Masterx
    hello, im trying to access a variable in 2 or 3 member functions and let them modify it, and the data should be retained (thats why im using "static" for this purpose) so that whenever any function ( eg. member function #2 )needs updated variable "org" for example(that has been calculated and had been modifed in another function(and again should be modified and be used in some others), there is no problem to get the data ! .
    It sounds like you just want a non-static member variable. If you need the same member variable to be shared across different instances then you should consider using a static member variable.

    Quote Originally Posted by Masterx
    but . whenever i write" static int org" , and use "org" in member functions, the compiler generates error! stating that "|1131|undefined reference to `CLA:rg'|
    That is because you have merely declared the static member variable named org. You need to define it in exactly one source file by writing, say:
    Code:
    int CLA::org = 0;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    hello, im trying to access a variable in 2 or 3 member functions and let them modify it, and the data should be retained (thats why im using "static" for this purpose) so that whenever any function ( eg. member function #2 )needs updated variable "org" for example(that has been calculated and had been modifed in another function(and again should be modified and be used in some others), there is no problem to get the data !
    If you just want a value retained between method calls, wouldn't a non-static member be enough?

    Static is for cases where you want to have one of these variables shared between all instances of the class.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by auralius View Post
    redefine org...

    put in global:
    Code:
    int CLA::org;
    you mean in class i write int CLA:rg ? or in member functions which i want to use it .(if you meant in member functions, i tried it before , it just gave me that error again .( when it was declared static int org.)
    Quote Originally Posted by auralius View Post
    no...
    constructor CLA accepts string...
    change it into:
    Code:
    CLA(map<int, string>);
    if i'm not mistaken...
    tanx , i will give it a try .
    Quote Originally Posted by laserlight View Post
    It sounds like you just want a non-static member variable. If you need the same member variable to be shared across different instances then you should consider using a static member variable.


    That is because you have merely declared the static member variable named org. You need to define it in exactly one source file by writing, say:
    Code:
    int CLA::org = 0;
    well , there is only one org, and every function must use this org ! (and another variable that counts line (linecounter) , as you see , i cant have more than one instance of this variables specially the second one! because in one function , i use this counter to fill a mapped array, and thus each line repreasents one string!
    (function 1 uses linecounter 1 .... now function 2 must use that linecounter to get sth else somewhere , i canty use different counters for these functions! unfortuantley they are designed to work side by side ( func 1 do sth , then it saves the stuff to org e.g and then func2 gets it from org, and do its job and so on ... ) thats why im trying to use a static! because it is not destroyed when the function that uses the variable exits.!

    Quote Originally Posted by anon View Post
    If you just want a value retained between method calls, wouldn't a non-static member be enough?

    Static is for cases where you want to have one of these variables shared between all instances of the class.
    you mean declaringit as ordinary int in class, would make it work too?
    by using this method , doesnt the org value get reset each time a function uses it?
    Last edited by Masterx; 01-25-2009 at 08:23 AM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  7. #7
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by auralius View Post
    no...
    constructor CLA accepts string...
    change it into:
    Code:
    CLA(map<int, string>);
    if i'm not mistaken...
    by the way now what should i do ?
    Code:
            CLA::CLA(map<int, string> str)
            {
                    InputString(str);
            }
    is it right?
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    Quote Originally Posted by Masterx
    well , there is only one org, and every function must use this org ! (and another variable that counts line (linecounter) , as you see , i cant have more than one instance of this variables specially the second one! because in one function , i use this counter to fill a mapped array, and thus each line repreasents one string!
    As in there is only one org variable for each CLA object, or as in there is only one org variable for all the CLA objects?

    Quote Originally Posted by Masterx
    you mean declaringit as ordinary int in class, would make it work too?
    That is the impression I get: you want one org variable for each CLA object, not one org variable for all the CLA objects.

    Quote Originally Posted by Masterx
    by using this method , doesnt the org value get reset each time a function uses it?
    Perhaps you need to start experimenting with a small program:
    Code:
    #include <iostream>
    
    class X
    {
    public:
        X() : num(0) {}
    
        void setNum(int x)
        {
            num = x;
        }
    
        void printNum() const
        {
            std::cout << num << std::endl;
        }
    private:
        int num;
    };
    
    int main()
    {
        X x;
        x.setNum(123);
        x.printNum();
    }
    If your theory that the member variable value gets "reset" each time a member function uses it is correct, the output of the program would be 0. What output do you get?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by laserlight View Post
    As in there is only one org variable for each CLA object, or as in there is only one org variable for all the CLA objects?


    That is the impression I get: you want one org variable for each CLA object, not one org variable for all the CLA objects.


    Perhaps you need to start experimenting with a small program:
    Code:
    #include <iostream>
    
    class X
    {
    public:
        X() : num(0) {}
    
        void setNum(int x)
        {
            num = x;
        }
    
        void printNum() const
        {
            std::cout << num << std::endl;
        }
    private:
        int num;
    };
    
    int main()
    {
        X x;
        x.setNum(123);
        x.printNum();
    }
    If your theory that the member variable value gets "reset" each time a member function uses it is correct, the output of the program would be 0. What output do you get?
    it gives me 123. so making it private only will do it , yes?! and the value of num is accessible among all of the members . (as i want it this way , like this example .)
    and "
    you want one org variable for each CLA object, not one org variable for all the CLA objects."
    yes . i have only one org for each function to mess with it!.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    Quote Originally Posted by Masterx
    it gives me 123. so making it private only will do it , yes?! and the value of num is accessible among all of the members . (as i want it this way , like this example .)
    Yes.

    Now, onto other things: what does your CLA class model? It looks like it is doing too many things. Some of its member functions seem to have suitable names for entire classes
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by laserlight View Post
    Yes.

    Now, onto other things: what does your CLA class model? It looks like it is doing too many things. Some of its member functions seem to have suitable names for entire classes
    well because its the first time i use mapped arrays , i dont know how to implement these two sessions . thats why it looks so wierd and probably wrong!!
    it just gets user input !( should contain an array that contains strings (some statements! )
    and its simply asbefore i use mapped array)
    Code:
            CLA::CLA(string user_input)
            {
                    InputString(user_input);
            }
    and now i think this should be sth like this now:
    Code:
            CLA::CLA((map<int, string>) str)
            {
                    InputString(str);
            }
    and in InputString() we have :
    Code:
    void CLA::InputString(string user_input)
            {
                    
                    cout<<"Enter your String Please\n ";
                    getline( cin, user_input );
    
                    int linecounter = -1;          //counts the number ofstring input by user
                    OurString = user_input;
                    while (!exitscanner(OurString))
                    {
    
                            OurString = user_input;
                            StringCollector[linecounter++] = OurString;
                            //cout <<"\n$CLA$ ";                       //shows the user to input again.
                            getline( cin, user_input );
                    }
    
            }
    w
    Last edited by Masterx; 01-25-2009 at 08:55 AM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  12. #12
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    90
    Quote Originally Posted by Masterx View Post
    by the way now what should i do ?
    Code:
            CLA::CLA(map<int, string> str)
            {
                    InputString(str);
            }
    is it right?
    well, that's a constructor dude...it will be automatically run when you create an object of CLA.
    are you trying to assign member variable of a class to its own constructor?
    can't be like that...

    btw map is not that simple...
    check here: http://www.cplusplus.com/reference/stl/map/

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    Quote Originally Posted by Masterx
    it just gets user input !( should contain an array that contains strings (some statements! )
    You need to be more specific.

    Quote Originally Posted by Masterx
    well because its the first time i use mapped arrays , i dont know how to implement these two sessions . thats why it looks so wierd and probably wrong!!
    How do you know that std::map is the appropriate type in the first place? My point here is that the interface of your class looks poorly designed. You need to clarify the purpose of your class first. You need to provide it with a minimal but complete interface. You should not be jumping into the specifics of the implementation without thinking through these issues first.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by auralius View Post
    well, that's a constructor dude...it will be automatically run when you create an object of CLA.
    are you trying to assign member variable of a class to its own constructor?
    can't be like that...

    btw map is not that simple...
    check here: http://www.cplusplus.com/reference/stl/map/
    ! about the first issue , you are right , ( tobe honest , i was thinking to disable constructor because i couldnt find a way to get it to work!

    and about the second one ! i dont understand it . even the simplest part of it ! what does it mean ! did i get into a serious problem by using mapped array?
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    Quote Originally Posted by Masterx
    and about the second one ! i dont understand it . even the simplest part of it ! what does it mean ! did i get into a serious problem by using mapped array?
    As in you do not understand the material that auralius linked to? Oh, and stop calling std::map a "mapped array". It is not an array. In fact, std::map is typically implemented using a balanced binary tree.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. uploading file to http server via multipart form data
    By Dynamo in forum C++ Programming
    Replies: 1
    Last Post: 09-03-2008, 04:36 AM
  2. Linking errors with static var
    By Elysia in forum C++ Programming
    Replies: 8
    Last Post: 10-27-2007, 05:24 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 09:48 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21