Questions on classes reference to single variable

This is a discussion on Questions on classes reference to single variable within the C++ Programming forums, part of the General Programming Boards category; So, I know that this code works, but I have several questions. 1) Is is practical to have multiple classes ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    2

    Questions on classes reference to single variable

    So, I know that this code works, but I have several questions.

    1) Is is practical to have multiple classes reference to a single variable or class entity ?

    2) When writing a multithreading program, will this technique break?

    3) is there a smarter or better approach, than this simple code?

    Code:
    #include <iostream>
    using namespace std;
    
    class Increase
    {
    	int & numb;
    public:
    	Increase( int & n ) : numb(n) { }
    
    	void by( unsigned int val) {  numb += val; }
    
    };
    
    class Decrease
    {
    	int & numb;
    public:
    	Decrease( int & n) : numb(n) { }
    
    	void by( unsigned int val) { numb -= val; }
    };
    
    
    int main(void)
    {
      int number = 20;
      Increase inc(number);
      Decrease dec(number);
    
      inc.by(20);
      cout<< number << "\n";
      dec.by(10);
      cout<< number <<"\n";
      return 0;
    }

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    Quote Originally Posted by Asker00FF View Post
    1) Is is practical to have multiple classes reference to a single variable or class entity ?
    Yes..especially if the 'entity' is huge enough.
    2) When writing a multithreading program, will this technique break?
    Provided that you use locks in a smart way...it will work as expected.
    (It can be done without locks..but I've not treaded that path yet.)
    3) is there a smarter or better approach, than this simple code?
    Depends on what exactly you want to do.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    2
    I was designing a command pattern for my sprite class. The basic commands were Move and otherAction ( for now). I had plan to have each sprite reference to the Draw and SpriteSheet class.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 09-30-2011, 09:46 AM
  2. Replies: 7
    Last Post: 04-16-2011, 12:28 PM
  3. Extracting Integers From a Single Variable
    By parx86 in forum C Programming
    Replies: 5
    Last Post: 05-02-2008, 12:27 PM
  4. single environment variable
    By SpEkTrE in forum Linux Programming
    Replies: 2
    Last Post: 10-21-2003, 12:36 PM
  5. single-tone classes
    By glUser3f in forum C++ Programming
    Replies: 0
    Last Post: 10-07-2003, 03:02 PM

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