Different Object from same class access to the static value C++

user3265085

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)?

4pie0

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Access anonymous object property from static class c#

From Dev

Set a variable for a class from an object and access the same value from another object of that class

From Dev

Access HttpContextBase object from static class

From Dev

C++ - How to access private members of a class, from a static function of the same class?

From Dev

C# How to access static class List<> from another class

From Dev

java unable to get value of static variable from the same class

From Dev

How to use same object with its value in different class

From Dev

VB Cannot access class from different project, same solution

From Dev

Access object from another class c++

From Dev

Access different tables using the same type of object (NHibernate, C#)

From Dev

Java - Comparator Class for same attributes from different object types

From Dev

Finding value of ENUM from object in ArrayList from different class

From Java

Access static class members from dynamic library in c++

From Dev

Access private static method from outside the class C++

From Dev

Same Class Object With Different Methods For iOS and watchOS (Objective-C)

From Dev

get value from different HTML elements that have the same class

From Dev

How to access a value of a private instance variable from a different class?

From Dev

Are static template class variables with different instantiations the same?

From Dev

How to access the .class object in java without using the class name from a static context

From Java

Can I access the static variables of the 'Class' Object?

From Dev

Put different subclasses from the same base class into a list C#

From Dev

access to ArrayList from a different class

From Dev

How to access an object value within the same object

From Dev

Access child class static member from a base class static member

From Dev

Access the value of a variable, from a different script, using a string (C#)

From Dev

How to reference an object from a different class [C#]

From Dev

Entity Framework doesn't associate a still existing object in the database context with the same object referenced from a different class

From Dev

copy contents of static arraylist from different class?

From Dev

Problems with Static Variables from Different a Class

Related Related

  1. 1

    Access anonymous object property from static class c#

  2. 2

    Set a variable for a class from an object and access the same value from another object of that class

  3. 3

    Access HttpContextBase object from static class

  4. 4

    C++ - How to access private members of a class, from a static function of the same class?

  5. 5

    C# How to access static class List<> from another class

  6. 6

    java unable to get value of static variable from the same class

  7. 7

    How to use same object with its value in different class

  8. 8

    VB Cannot access class from different project, same solution

  9. 9

    Access object from another class c++

  10. 10

    Access different tables using the same type of object (NHibernate, C#)

  11. 11

    Java - Comparator Class for same attributes from different object types

  12. 12

    Finding value of ENUM from object in ArrayList from different class

  13. 13

    Access static class members from dynamic library in c++

  14. 14

    Access private static method from outside the class C++

  15. 15

    Same Class Object With Different Methods For iOS and watchOS (Objective-C)

  16. 16

    get value from different HTML elements that have the same class

  17. 17

    How to access a value of a private instance variable from a different class?

  18. 18

    Are static template class variables with different instantiations the same?

  19. 19

    How to access the .class object in java without using the class name from a static context

  20. 20

    Can I access the static variables of the 'Class' Object?

  21. 21

    Put different subclasses from the same base class into a list C#

  22. 22

    access to ArrayList from a different class

  23. 23

    How to access an object value within the same object

  24. 24

    Access child class static member from a base class static member

  25. 25

    Access the value of a variable, from a different script, using a string (C#)

  26. 26

    How to reference an object from a different class [C#]

  27. 27

    Entity Framework doesn't associate a still existing object in the database context with the same object referenced from a different class

  28. 28

    copy contents of static arraylist from different class?

  29. 29

    Problems with Static Variables from Different a Class

HotTag

Archive