Class and Global Variable Manipulation

This is a discussion on Class and Global Variable Manipulation within the C++ Programming forums, part of the General Programming Boards category; Hey there! I have this program, where I have a class, and inside the class there is a function named ...

  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
    22,115
    Why do you want to do this? And why do you have your class and member function definition in the global main() function?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,006
    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
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    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, 09: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, 09:19 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 04: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21