Thread: Importing variable from another .cpp or .h and making it local to main()?

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    194

    Importing variable from another .cpp or .h and making it local to main()?

    Is this sort of thing possible? Suppose I have 100 some variables in a file, if I were to put these in a .cpp or a .h file and link it with the main file, they would be global (right??).

    I was wondering if there was a way to make them local to the main function inside the main file.

    Or what's an alternative (like using file reading from a text file instead of .cpp and .h or using namespaces or whatever).

    Cheers!

  2. #2
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    89
    Quote Originally Posted by Nwb View Post
    Is this sort of thing possible? Suppose I have 100 some variables in a file, if I were to put these in a .cpp or a .h file and link it with the main file, they would be global (right??).

    I was wondering if there was a way to make them local to the main function inside the main file.

    Or what's an alternative (like using file reading from a text file instead of .cpp and .h or using namespaces or whatever).

    Cheers!
    Just define the variables at the global scope, outside of any function, within their implementation file. Then from your main driver declare them with the "extern" keyword. (Just don't try to initialize them to any value from there. That's done inside the implementation file.)

  3. #3
    Registered User
    Join Date
    Sep 2018
    Posts
    194
    Oh I assumed doing that would still give them global scope status. Guess not thanks.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,617
    100+ global variables sounds like a very bad place to be.

    Can you show what you're trying to achieve?
    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.

  5. #5
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    89
    Quote Originally Posted by Salem View Post
    100+ global variables sounds like a very bad place to be.

    Can you show what you're trying to achieve?
    True. Usually synonymous with "not thread safe".

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,465
    usually usually synonymous with "not thread safe".
    No usually considered a non-maintainable mess.

  7. #7
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    89
    Quote Originally Posted by jimblumberg View Post
    usually considered a non-maintainable mess.
    No doubt, but the effect on thread safety is still a much bigger problem.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,428
    Quote Originally Posted by Nwb
    Or what's an alternative (like using file reading from a text file instead of .cpp and .h or using namespaces or whatever).
    If you're thinking of 100 variables and reading from a file, perhaps you have in mind some kind of registry of configuration variables? These don't necessarily have to be a hundred different variables, e.g., perhaps a single registry consisting of a std::unordered_map<std::string, std::string> object would work to map configuration variable names to their values. You could have a file in an INI-style format to store the names and corresponding values.

    This unordered_map object could be declared in main, populated in a function called from main, and then passed around as needed, or if you really want you could make it global, but if you do that and you presumably don't want to modify the original configuration values after reading from file, you could apply the singleton pattern such that the global access only provides a const-view of the values.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Sep 2018
    Posts
    194
    Quote Originally Posted by Salem View Post
    100+ global variables sounds like a very bad place to be.

    Can you show what you're trying to achieve?
    Not global variables, that was my misconception. I assumed that when you use extern variable; they are entered into global scope, which I didn't want to happen.

    Anyways for the 100 variables part, I am making was making a quiz game. Now keep in mind that I just recently started learning about C++ in school a few months ago and we're still learning about one dimensional arrays, I was just playing around with this quiz.

    But I would really appreciate pointers. And for that I will post my quiz soon as it's done.. which it will and soon.

    So the idea is, I used a char* array for each Questions, Answers, OptionA, OptionB, OptionC, OptionD which held 50 some different variables separately which held text. Why did I use text variables? Because I wanted the order in which the questions and options are read out to be random.

    Why did I use char* (I don't even know what char* is.. my compiler told me I couldn't convert char to char* and since just adding an asterix made things work I just went with it)
    instead of just using one array and shuffling the elements? Because it would be harder to enter new Questions and for the most part it would be messy.

    _________________________________________

    So before, the variable definitions would take up majority space of the code. So I had to figure out how to put the variables into a different file so the code is more neat for other users. That's where all of this came into picture.

    ( I brought up file i/o because I thought you could not attach variables that are local to the main function from another .cpp file (which I found out to not be so at all).

    Cheers and thanks to everybody that replied!
    Last edited by Nwb; 09-19-2018 at 08:47 AM.

  10. #10
    Registered User
    Join Date
    Jun 2017
    Posts
    156
    Honestly, before writing such a game you should learn about string, struct and vector which would easily solve your problems with the variables.

    Code:
    struct QuizEntry // one variable for each question
    {
       string question;
       vector<string> answers;
       int correct_answer;
    };
    
    vector<QuizEntry> quiz; // one variable for all questions

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-25-2014, 10:36 PM
  2. string declaration as global vs a main local variable...
    By Jim Iwannou in forum C Programming
    Replies: 4
    Last Post: 11-14-2013, 03:12 AM
  3. Help with local char arrays and returning them to main
    By jvillanueva in forum C Programming
    Replies: 12
    Last Post: 09-15-2013, 10:31 PM
  4. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 03:35 AM
  5. Local variable not initialized
    By countpuchi in forum C Programming
    Replies: 16
    Last Post: 05-10-2007, 01:18 AM

Tags for this Thread