Thread: How to reference variables declared in codes from header file?

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    3

    How to reference variables declared in codes from header file?

    Hello,

    I have some inline functions declared in the header file and need to access some variables declared in the codes that include the header file, is there a where to do so?

    For example:
    Code:
    module.h:
    inline t_print() { printf("modid = %d\n", modid); }
    
    // this should print 1 or 2
    inline some_func() { t_print(); };
    
    module1.cpp:
    static long modid = 1;
    t_print();  // this should give modid = 1
    
    module2.cpp:
    static long modid = 2;
    t_print(); // this should give modid = 2
    Any suggestion out there? Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you have

    #include "module.h"

    in both source files, what do you get?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    3
    Thanks for the reply.

    Yes, actually both modules in the example include module.h file. Then when module1.cpp calls some_func(), the printed line should be with "modid = 1"; and when module2.cpp calls some_func(), the printed line would be "modid = 2".

    But since modids are declared as static in the cpp modules, they are not accessible in the header file. Thus the question is whether there is a way, or alternate solution to acheive the similar requirement?

    Thanks again.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You should have some_func and t_print take modid as a parameter, that's what parameters are for.

    Otherwise extern might work for you.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    3
    Yes, passing as argument would solve the situation. I guess that would be the only solution now. Initially the reason I wanted to use static in cpp module was to avoid passing as argument, the programmers need not know about the modid.

    Thank you.

  6. #6
    Banned
    Join Date
    Oct 2005
    Posts
    4
    Don't complicate things just pass modid as a parameter to the function t_print(). Using global variables can be quite an inconvenience. Your best bet may be to go object oriented and store the globals in their separate modules. That's just a suggestion, though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  3. Need help understanding Header Files
    By Kaidao in forum C++ Programming
    Replies: 11
    Last Post: 03-25-2008, 10:02 AM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM