Thread: Quickest solution to the problem of a class function holding a global variable's name

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640

    Quickest solution to the problem of a class function holding a global variable's name

    As the title states...

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    int MyVariable;
    
    class MyClass
    {
       public:
          int MyVariable;
          void MyFunction(int MyVariable);
    };
    
    void MyClass::MyFunction(int MyVariable)
    {
       MyVariable = 3; //Access the argument variable
       ::MyVariable = 12; //Access the global variable
       this->MyVariable = 567; //Access the local variable
    }
    Last edited by Magos; 10-17-2002 at 12:54 PM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Not what i mean

    Its a funtion not a variable that holds the same name as a
    global variable and the compiler doesnt aprove it

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Error: param.cpp(7,2):Multiple declaration for 'Var'
    You can't have two identifiers with the same name, wether they be variables or functions. Use another name (like put an i in front of the variable if it's an integer).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You can do this (i think this is what you mean)

    Code:
    int some_number(void) {
        return 10;
    }
    
    class myClass {
    protected:
        int some_number;
    public:
        void set(int);
    };
    
    void myClass :: set(int some_number) {
        this->some_number = some_number;
    }
    I think you are doing this:

    Code:
    int free;
    
    void free(void *) {
        ....
    }
    And that is illegal. However, you can always use namespaces to do this.

    Code:
    namespace first {
       int free;
    }
    
    namespace second {
       void free(void *) { }
    }
    
    int main(void) {
        using namespace first;
        free = 10;
        void *someptr;
    
        for(int i = 0; i < free; i++) {
             using namespace second;
             free(someptr);
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Static variables and functions problem in a class
    By earth_angel in forum C++ Programming
    Replies: 16
    Last Post: 09-15-2005, 12:08 PM
  4. Problem with global variables
    By DominicTrix in forum C++ Programming
    Replies: 6
    Last Post: 09-08-2004, 01:26 PM
  5. defining and using a global class
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2004, 09:51 PM