Get/Set Function Help

This is a discussion on Get/Set Function Help within the C++ Programming forums, part of the General Programming Boards category; I am writting some code and wish to use a get/set for a class variable. This variable will be used ...

  1. #1
    Registered User
    Join Date
    Aug 2008

    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...
    int main()
       ClassA *test_ptr = 0;
      if( test_ptr->test_B.getValue() )
         //do something...
    //within header
    //skeleton of classA
      ClassB test_B;

    bool test_value
    bool ClassB::getValue()
    {  return test_value;  }
    void ClassB::setValue()
         test_value = false;
        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
    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
    New Zealand
    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:
    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, 08:42 AM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  4. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  5. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM

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