Sharing a variable between classes of different .CPP files

This is a discussion on Sharing a variable between classes of different .CPP files within the C++ Programming forums, part of the General Programming Boards category; Hello. I have tried several chat rooms, and many google searches, but I havent found an answer to my problem. ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    3

    Sharing a variable between classes of different .CPP files

    Hello. I have tried several chat rooms, and many google searches, but I havent found an answer to my problem.

    How can I share a variable between 2 different classes that are in 2 different CPP files?

    Methods I have tried:
    1. Making the variable in one class Public.
    -> This failed because the classes are not in the same .cpp file. (This is what I was told in yahoochat room)

    2. Making each class a Friend of the other class.
    -> This failed for the same reason, I believe.

    3. I tried making a 3rd dummy class in a header file. The contents of this class was a public variable, and both the other classes were added as friends. Then I #included this header in both files.
    -> This too didnt work, but I dont know why.

    Thank you for any suggestions. If it helps, what Im trying to do is use framerate data from an opengl view copied into a global string which then is used in the status bar. No matter what Ive tried, I cant get the two classes in the two files to share the string.
    Thank you.

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    file 1:

    Code:
    int var;
    
    // use var...
    file 2:

    Code:
    extern int var;
    
    // use var...
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    well this migh not be a proper solution..
    You can store the value of that variable in a file and you can access it from another program and can use it..

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    the public data should be accessable to the other class just fine. You are doing something else wrong obviously. I must say though that making data public is a no-no. You should find a different way of structuring your code unless it's absolutely necessary.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    3

    Extern works

    Thank you no-one, your solution was exactly what is needed.

    Vasanth, you also had another good idea that I may use in the future.

    Hershlag, the problem is that the classes are in separate files, and thus all standard methods would not work.

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    I understand that both classes were in separate files. That is the normal method of coding classes. if the file for the second class includes the definition of the first class ie: include "FirstClass.h" you will have full knowledge of the variable where you need it. no-one's solution is an old sloppy C way of doing it but you are free to use it of course. If you are looking for a C++ way, keep it in the class and out of the world of global/extern.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Including extra .cpp files visual2008
    By Cathalo in forum C++ Programming
    Replies: 9
    Last Post: 06-16-2009, 03:29 AM
  2. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  3. Classes and header files
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 08-23-2002, 10:42 AM
  4. doin' classes in header files?
    By face_master in forum C++ Programming
    Replies: 9
    Last Post: 11-14-2001, 02:56 AM
  5. Sharing Classes using .DLL files?
    By Laos in forum C++ Programming
    Replies: 2
    Last Post: 08-30-2001, 04:58 AM

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