1

I know how to get this done in Java, but I can't figure out how to do it in C++ Here is my questions: I have a class called "stack" and I create different Objects from stack class in my main method. I want to store a variable in the stack class, so all the Object can access the variable. My header file:

Template<class Item>
struct stackLinked{
   Item value;
   stackLinked* next;
}
Template<class T>
class stack{
    public:
      static int stored;
      .......
      .......
    private:
      .......
}

My main function:

stack<T> temp;
temp.stored=1;

stack<T> numbers;
stack<T> operations;

................

When I stored the value into temp, can numbers and operations have the same variable (same value)?

3
  • Yes. Best fire up debugger and see for yourself. Commented Apr 17, 2014 at 18:09
  • but i keeps getting error from my VS lnk2001 unresolved external symbol something like this Commented Apr 17, 2014 at 18:15
  • @user3265085 you have to define static variable as in my example Commented Apr 17, 2014 at 18:19

3 Answers 3

2

Yes. Static variable is one for all instances of stack. Every instance has access to the one and same static variable.

Example:

#include <iostream>
using namespace std;

class stack{
    public:
      static int stored;
};

int stack::stored;  // definition of static variable, necessary

int main() {
    // your code goes here
    stack s, m, n;
    s.stored = 4;
    std::cout << s.stored << "," << m.stored << "," << n.stored << std::endl;
    m.stored = 5;
    std::cout << s.stored << "," << m.stored << "," << n.stored << std::endl;
    n.stored = 1;
    std::cout << s.stored << "," << m.stored << "," << n.stored << std::endl;
    return 0;
}

output:

4,4,4

5,5,5

1,1,1

http://ideone.com/4m4FQw

Sign up to request clarification or add additional context in comments.

7 Comments

I tried this in my project, it did not remember the value. I am using a template class with the stack class. Does it affect the result ? I change the question above for the detail.
the reason I am adding a static value is I want to provide an input for user so they can pick the method of this program is running.
for each template class (for a given set of parameters) another static variable is created
Sorry for this. so If i have stack<int> , stack<char> , stack<double> all their stored'value will be different?
yes, there will be "stored" for stack<int> shared between all stack<int> instances, and another "stored" for stack<char> shared between all stack<char> instances, etc
|
1

Not sure what you really ask here, but I guess this is the answer you're looking for. Since stored is a static variable, it is shared by all instances of the class stack.

stack temp;
temp.stored=1;

stack numbers;  
stack operations;

//numbers.stored has value of 1
//operations.stored also has value of 1

1 Comment

"Since stored is a static variable, it is shared by all instances of the class stack" - the OP is already aware of that. The problem is an undefined reference during link because no definition of stored is provided.
0

Non-static class members are constructed at class instantiation, while a static class member is constructed at the start of program execution. Hence it needs to be defined at that time and not only declared for future instantiation. Just like in the example provided in previous answer.

int stack::stored; //...

Above line from the example allocates the variable's memory, in other words defines it. This cannot wait until the class is instantiated.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.