One member, many instances

This is a discussion on One member, many instances within the C++ Programming forums, part of the General Programming Boards category; Hi there! Is there any way that I can use just one variable(the same) for all instances of the class ...

  1. #1
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    One member, many instances

    Hi there!
    Is there any way that I can use just one variable(the same) for all instances of the class that has that variable? I thought about static, but didn´t work because the cpp file couldn´t see that variable(as predicted). I don´t post my code because it is huge(it is a game). So, if anyone can help but need more details just let me know!
    Thanks all!
    Nothing more to tell about me...
    Happy day =)

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,674
    Code:
    #include <iostream>
    using namespace std;
    
    class A
    {
    public:
        static int a;
    };
    
    int A::a; //must declare static member variables like so
    
    int main(void)
    {
        A a,b;
    
        a.a = 5;
    
        cout << b.a << endl;
    
        return 0;
    }
    gg

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    "I thought about static, but didn´t work because the cpp file couldn´t see that variable(as predicted)."

    So, static is just a concept for .h files?

  4. #4
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    7stud: Yes, but not necessarily.
    Here is what i've commonly seen done:

    mine.cpp
    Code:
    #include "mine.h"
    cout << "Hello" << abc.endl;
    mine.cpp
    Code:
    #include "mine.h"
    char *abc::endl = "\r\n";
    mine.h
    Code:
    class abc {
      public:
        static char *endl;
    }
    The only other way i know of is using externs. but even then, i think it complains about multiple storace classes or something...
    Last edited by Inquirer; 05-03-2003 at 01:39 PM.
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  5. #5
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    Add a member functions to get and set the static int, hide it away.

    A.h
    Code:
    class A
    {
    public:
        int get_a();
        void set_a(int n);
    protected:
        static int a;
    };
    A.cpp
    Code:
    #include "A.h"
    int A::a = 0;
    
    int A::get_a()
    {
        return a;
    }
    
    void A::set_a(int n)
    {
        a = n;
    }
    AProg.cpp
    Code:
    #include <iostream>
    #include "A.h"
    using namespace std;
    
    int main(void)
    {
        A a,b;
    
        a.set_a(5);
        cout << b.get_a() << endl;
        return 0;
    }
    Last edited by Scarlet7; 05-03-2003 at 02:11 PM.

  6. #6
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    Thumbs up Thanks!

    Thanks for you all!
    I didn´t test all the things yet, but I trust that must be right!
    Again thanks! This is very useful to me!
    Nothing more to tell about me...
    Happy day =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 2
    Last Post: 04-19-2008, 12:06 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM

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