Thread: Class and Global Variable Manipulation

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    47

    Class and Global Variable Manipulation

    Hey there!

    I have this program, where I have a class, and inside the class there is a function named Function1...Function1 has an argument that should be subtracted from a variable that is not in its class or argument...So let's say it's like this:

    Code:
    int GlobalVariable1;
    
    int main()
    {
    
    class Class1
    {
    void Function1(int Argument1);
    };
    
    void Class1::Function1(int Argument1)
    {
    /* I want the Argument1 in Class1 to be subtracted from the GlobalVariable1, which is not in the class. The code would be here! */
    }
    
    }
    How would I do this?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why do you want to do this? And why do you have your class and member function definition in the global main() function?
    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

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Better examples generally result in better replies, but...
    Code:
    #include <iostream>
    
    int GlobalVariable1;
    
    class Class1
    {
    public:
       void Function1(int Argument1);
    };
    
    void Class1::Function1(int Argument1)
    {
       /* I want the Argument1 in Class1 to be subtracted from the GlobalVariable1, 
         which is not in the class. The code would be here! */
       GlobalVariable1 -= Argument1;
    }
    
    int main()
    {
       Class1 Obj1;
       std::cout << "GlobalVariable1 = " << GlobalVariable1 << '\n';
       Obj1.Function1(5);
       std::cout << "GlobalVariable1 = " << GlobalVariable1 << '\n';
    }
    
    /* my output
    GlobalVariable1 = 0
    GlobalVariable1 = -5
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    47
    Yay! Thank you!

    Yeah, my code is actually 6000 times better, but I made this in two seconds, so I forgot alot of things. In the actual code, the class is declared outside of the main function...

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Global variables can be accessed by anyone. Classes are no different. However know that by doing this you are violating just about every object-oriented design principle. If you find that you need to do this to get the code working, IMO a re-design is in order.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. global and static variable in a class delivered in a DLL
    By George2 in forum C++ Programming
    Replies: 16
    Last Post: 04-13-2008, 08:19 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  5. Problam with a global class variable
    By WebmasterMattD in forum C++ Programming
    Replies: 11
    Last Post: 01-21-2003, 04:38 AM