Thread: Get/Set Function Help

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    16

    Get/Set Function Help

    I am writting some code and wish to use a get/set for a class variable. This variable will be used within the main(), and also internally within the class. Here is some syntax to better explain the issue...
    Code:
    int main()
    {
       ClassA *test_ptr = 0;
    
      if( test_ptr->test_B.getValue() )
      {
         //do something...
         test_ptr->test_B.setValue();
      }
    Code:
    //within header
    //skeleton of classA
      ....
      ClassB test_B;
      ....

    //CLASSB HEADER
    Code:
    ClassB
    {
    public:
    
    getValue()
    
    setValue()
    
    private:
    
    bool test_value
    }
    CLASSB CPP
    Code:
    bool ClassB::getValue()
    {  return test_value;  }
    
    void ClassB::setValue()
    {
       if(test_value)
         test_value = false;
       else
        test_value = true;
    }
    My question is in order for test_value to be seen by main should I make test_value a pointer; or can I just make it normal bool value? I am worried about test_value being released/destoryed in operation of the code and I am not really certain if a pointer will resolve that issue. I have looked stuff up but I am still very fuzzy on this!

    Also if its a pointer should I declare it *test_value = true; in the constructor? Thank you for the help in advance.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    test_value is a member variable of your class, and only gets destroyed when your class gets destroyed. So no, you have no need of returning a pointer in your getter.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    test_value doesn't need to be seen by main. main can just call getValue. NO change to pointers is needed here.

    FYI, to toggle a variable:
    Code:
    void ClassB::setValue()
    {
       test_value = !test_value;
    }
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Recursive function
    By WatchTower in forum C Programming
    Replies: 11
    Last Post: 07-15-2009, 07:42 AM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  4. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  5. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM