Thread: inputting a params file with global variables (work around)

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    38

    inputting a params file with global variables (work around)

    This is what I am trying to do:
    Input global variables from a data file, and use those variables throughout my software.

    What I have done: I have created a class that has the global variables declared and init to some default setting. I then read the data file, and set the global variables to the data that I read in. My problem is that when I change the global variables in the class it is only a local change, and in the other classes (that use extern) only see the default setting. I understand WHY what I am doing is wrong and will not work. What I need is a work around for that.

    Question: Is there a way to declare a global variable, and then change the value; and have that value used throughout the software. ( I know that I could just pass the class, but I am trying to avoid this).

    thanks - and if this isn't clear please mention that
    chris

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Post what you tried
    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
    May 2003
    Posts
    38
    it is hard to post what I tried because this problem deals with inheritance across multiple files. But I'll try to give an example.

    this is basically what I tried:

    Code:
    #include "xxx.h"
    
    // global variables
    double value1 = 0.0;
    double value2 = 0.0;
    
    class ProgramDriver {
    private:
       ...typical constructors and what not
       Class1 c1;
       Class2 c2;
       ...
    public:
       ...
       function(void);
    };
    
    void ProgramDriver::function(){
       ...   
       ...read in data file;
     
       // set global variables
       value1 = 10.0;
       value2 = 20.0;
       printf("value1 = %lf\n", value1);
       printf("value2 = %lf\n", value2);
       ...
       cl1.start();  // do something in class 1
       cl2.start);   // do somethign in class 2
     }
    
    int main(void){
       ProgramDriver PD;
       PD.function();
    }
    and here is one of the "cl" classes:
    Code:
    #include "Class1.h"
    
    extern double value1;
    extern double value2;
    
    void Class1::start(){
       printf("value1 = %lf\n",value1);
       printf("value2 = %lf\n",value2);
    }
    This is psuedo code, I'm just trying to get my idea across
    chris

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Is this like what you want?
    Code:
    /* this is hello.cpp */
    #include <cstdio>
    #include "pd.h"
    int main(void){
       ProgramDriver PD;
       printf("main value1 = %lf\n", value1);
       printf("main value2 = %lf\n", value2);
       PD.function();
    }
    Code:
    /* this is pd.h */
    #ifndef PD_INCLUDED
    #define PD_INCLUDED
    
    #include "class1.h"
    
    extern double value1;
    extern double value2;
    
    class ProgramDriver {
    private:
       Class1 c1;
    public:
       void function(void);
    };
    
    #endif
    Code:
    /* this is pd.cpp */
    #include <cstdio>
    #include "pd.h"
    
    double value1 = 0.0;
    double value2 = 0.0;
    
    void ProgramDriver::function(){
       value1 = 10.0;
       value2 = 20.0;
       printf("ProgramDriver::function value1 = %lf\n", value1);
       printf("ProgramDriver::function value2 = %lf\n", value2);
       c1.start();  // do something in class 1
    }
    Code:
    /* this is class1.h */
    #ifndef CLASS1_INCLUDED
    #define CLASS1_INCLUDED
    
    class Class1 {
    public:
        void start();
    };
    #endif
    Code:
    /* this is class1.cpp */
    #include <cstdio>
    #include "pd.h"
    #include "class1.h"
    
    void Class1::start() {
       printf("Class1::start value1 = %lf\n", value1);
       printf("Class1::start value2 = %lf\n", value2);
    }
    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
    Join Date
    May 2003
    Posts
    38

    re

    in your ph.cpp you declare "double value1 = 0.0" as a global variable, and then in "pd.h" you declare "extern double value1". Maybe I am confused, but what is the point of that, don't you only need to use "extern double value1" in classes other then "pd". Extern just basically search all classes for global variables right?
    chris

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Why not edit things and try them out?

    Besides, the whole idea of global vars managed by a class is a bit weird. It would be better to manage them as class member variables (private), and then make a single global instance of the class.
    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.

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    38
    thanks, thats exactly the kind of suggestion I was looking for
    chris

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 12:02 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Global variables used in a linked class, getting errors.
    By RealityFusion in forum C++ Programming
    Replies: 3
    Last Post: 09-24-2005, 12:25 AM