Thread: c++ 11 - static variables by instance

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    451

    c++ 11 - static variables by instance

    can i create a static variable, class member, that can have diferent values from diferents instances?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    No, the purpose of a static class member variable is to be the same for every instance of the class. If you want different values for different instances you want a "normal" member variable.

    Jim

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by jimblumberg View Post
    No, the purpose of a static class member variable is to be the same for every instance of the class. If you want different values for different instances you want a "normal" member variable.

    Jim
    but i can't change the variables values in Global Scope, that's why i thot in static variables

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by joaquim
    but i can't change the variables values in Global Scope, that's why i thot in static variables
    As long as the object or a reference/pointer to it is in scope and not declared const, you can change its non-static member variables if they are accessible or have some associated public setter member function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    If these classes have constructors that take values you can initialize the variables when you declare the classes. But the bigger question is why are these classes global?

    Jim

  6. #6
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by laserlight View Post
    As long as the object or a reference/pointer to it is in scope and not declared const, you can change its non-static member variables if they are accessible or have some associated public setter member function.
    i'm sorry, can you explain more?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by joaquim
    i'm sorry, can you explain more?
    I could, but it would be better if you posted what you are actually trying to do.

    Write a program that defines a class with a non-static member variable. Create an object (or two) of the class, and show how you are trying to "change the variables values in Global Scope", and then tell us what problems you faced (e.g., what compile errors you got).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by laserlight View Post
    I could, but it would be better if you posted what you are actually trying to do.

    Write a program that defines a class with a non-static member variable. Create an object (or two) of the class, and show how you are trying to "change the variables values in Global Scope", and then tell us what problems you faced (e.g., what compile errors you got).
    see these 2 class's:
    Code:
    typedef std::function<void(void)> OnSomethingHandler;
    class test
    {
    
        public:
    
            test()
            {
                //nothing;
            }
            OnSomethingHandler ola;
    
    };
    
    class test2 final : test
    {
        public:
        test2()
        {
            //nothing;
        }
    }test2;
    
    //here is the Global Scope section
    //i'm trying change the variable value
    test2::ola=[]()
    {
        cout << "hello";
    };
    error messages:
    "'OnSomethingHandler test:la' is inaccessible|
    with static member i can do these""
    with static, i can resolve some errors, but i must found 1 way for do it by instance

    my objective is change the variable\function on the Global Section, without:
    1 - virtual functions: because i must re-declare them in test2;
    2 - normal variables: because they don't have a type...

    maybe i can find the solution for my objective by here

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by joaquim
    error messages:
    "'OnSomethingHandler test::ola' is inaccessible|
    You get this error because you privately derived from test. If you used public inheritance then I would expect a different error, i.e., because you are trying to access ola as if test2 were a class (at that point it is a variable name) and as if ola were a static member variable.

    Quote Originally Posted by joaquim
    my objective is change the variable\function on the Global Section
    You have a misconception in your objective itself. Let's consider a class with a static member variable:
    Code:
    class test
    {
    public:
        static OnSomethingHandler ola;
    };
    The above code defines a class named test. Within the class definition is a declaration of a static member variable named ola. This declaration is not a definition. You must define it somewhere, typically in exactly one source file, e.g.,
    Code:
    OnSomethingHandler test::ola = []()
    {
        cout << "hello";
    };
    In this case we not only defined ola, but also initialised it. Notice that ola is declared as a member of test and hence it is defined as a member of test. If you derived from test, you could access ola as test2::ola, but you cannot define it as test2::ola. Furthermore, we did not change ola at file scope ("global scope"), rather, we defined it and gave it an initial value. If we wanted to change ola, then we would do that from within a function.

    Suppose we went back to having a class with a non-static member variable:
    Code:
    class test
    {
    public:
        OnSomethingHandler ola;
    };
    Now, test::ola no longer makes sense: we can only access the ola member of a particular object, e.g.,
    Code:
    int main()
    {
        test obj;
        obj.ola = []()
        {
            cout << "hello";
        };
        obj.ola();
    }
    Quote Originally Posted by joaquim
    2 - normal variables: because they don't have a type...
    That does not make sense: every variable has a type.

    I think jimblumberg's post #5 may put you on the right track. It depends on the bigger picture of what you are trying to do: why do you need test, test2 and ola? Do you really want to use inheritance, or are you just trying to create different test objects that can have different lambda functions for ola?
    Last edited by laserlight; 09-13-2014 at 03:50 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by laserlight View Post
    You get this error because you privately derived from test. If you used public inheritance then I would expect a different error, i.e., because you are trying to access ola as if test2 were a class (at that point it is a variable name) and as if ola were a static member variable.


    You have a misconception in your objective itself. Let's consider a class with a static member variable:
    Code:
    class test
    {
    public:
        static OnSomethingHandler ola;
    };
    The above code defines a class named test. Within the class definition is a declaration of a static member variable named ola. This declaration is not a definition. You must define it somewhere, typically in exactly one source file, e.g.,
    Code:
    OnSomethingHandler test::ola = []()
    {
        cout << "hello";
    };
    In this case we not only defined ola, but also initialised it. Notice that ola is declared as a member of test and hence it is defined as a member of test. If you derived from test, you could access ola as test2::ola, but you cannot define it as test2::ola. Furthermore, we did not change ola at file scope ("global scope"), rather, we defined it and gave it an initial value. If we wanted to change ola, then we would do that from within a function.

    Suppose we went back to having a class with a non-static member variable:
    Code:
    class test
    {
    public:
        OnSomethingHandler ola;
    };
    Now, test::ola no longer makes sense: we can only access the ola member of a particular object, e.g.,
    Code:
    int main()
    {
        test obj;
        obj.ola = []()
        {
            cout << "hello";
        };
        obj.ola();
    }

    That does not make sense: every variable has a type.

    I think jimblumberg's post #5 may put you on the right track. It depends on the bigger picture of what you are trying to do: why do you need test, test2 and ola? Do you really want to use inheritance, or are you just trying to create different test objects that can have different lambda functions for ola?
    i'm trying doing diferent test objects that have diferent lambda functions for ola. but, like you have seen, i must change that values in Global Section. that's why, in begining, i thot on static variables

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by joaquim
    i must change that values in Global Section.
    Why must you do that? When you say "change", do you mean "define and initialise" or do you mean "assign"?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by laserlight View Post
    Why must you do that? When you say "change", do you mean "define and initialise" or do you mean "assign"?
    honestly i mean assign... but the C\C++ don't let me do that. so i must say define and initialise

  13. #13
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    it's 1 thing that i want to, if i can

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by joaquim
    honestly i mean assign... (...) it's 1 thing that i want to, if i can
    Why do you want to do that? What problem does it help you to solve or solve better?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by laserlight View Post
    Why do you want to do that? What problem does it help you to solve or solve better?
    in my opinion is more easy change some values in Global Scope(ok.. i'm trying do it like the VB events are changed)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling non-static functions on static variables
    By pandu in forum C++ Programming
    Replies: 14
    Last Post: 06-19-2008, 03:07 AM
  2. static instance variable linker error
    By animeaholic in forum C++ Programming
    Replies: 4
    Last Post: 01-23-2007, 09:50 PM
  3. static variables
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 08-28-2006, 06:35 AM
  4. Replies: 2
    Last Post: 10-02-2004, 10:12 AM
  5. Replies: 2
    Last Post: 12-25-2001, 04:18 PM