can i create a static variable, class member, that can have diferent values from diferents instances?
can i create a static variable, class member, that can have diferent values from diferents instances?
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
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.Originally Posted by joaquim
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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
I could, but it would be better if you posted what you are actually trying to do.Originally Posted by joaquim
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).
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
see these 2 class's:
error messages: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"; };
"'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
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.Originally Posted by joaquim
You have a misconception in your objective itself. Let's consider a class with a static member variable:Originally Posted by joaquim
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:class test { public: static OnSomethingHandler ola; };
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.Code:OnSomethingHandler test::ola = []() { cout << "hello"; };
Suppose we went back to having a class with a non-static member variable:
Now, test::ola no longer makes sense: we can only access the ola member of a particular object, e.g.,Code:class test { public: OnSomethingHandler ola; };
Code:int main() { test obj; obj.ola = []() { cout << "hello"; }; obj.ola(); }That does not make sense: every variable has a type.Originally Posted by joaquim
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Why must you do that? When you say "change", do you mean "define and initialise" or do you mean "assign"?Originally Posted by joaquim
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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?Originally Posted by joaquim
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)