Problem with return value?

Aug 23, 2014 at 5:58am
I have a getter object that is suppose to return a value of type bool(default value is false). When a certain condition is met, the return value for the getter object is suppose to change to true. When I call the getter object in another function, however, the value remains false, despite the condition being met.

I did some research and I found out that the getter object is suppose to return a reference value. I did that and I still am having the same problem. Here is some pseudo code

1
2
3
4
5
6
7
8
9
10
11
// A.h

class A
{
  private:
     bool restart;
   
  public:
     bool& GetRestart();

}; 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//A.cpp

   A::A
  {
     restart = false;

  } 

   bool A::check()
   {
        if(mouse button clicked)
           restart = true
    }

   bool& A::GetRestart()
    {
        return restart
    }


1
2
3
4
5
6
7
// B.cpp

void B::check()
{
     cout << A.GetRestart() << endl;   // Value is always 0(false), even when 
}                                      // the condition above,if mouse clicked, is met(restart should turn to true)


Some insight would be appreciated. If anything else is needed let me know.
Aug 23, 2014 at 6:05am
It can only be one of a few things:

- You are not setting 'restart' to true. Try setting a breakpoint on that assignment to make sure it actually is running.

- You are setting 'restart' in one object to true... but are checking a different object where it is still set to false.

- You are setting it to true, but then later setting it back to false.

- You have memory corruption which is mucking up your object.



The first two are very easy to check for, so I'd start with those.
Aug 23, 2014 at 6:09am
For built-in types it doesn't really matter if you return a reference to a data member or not, but anyway:

You should probably show actual compilable code so that we can see where the problem is. What is your B class (or namespace?) and how does it relate to your A class? Are you even sure your if statement is being returned true? A is the name of a class, where do you define A as being an object of your A class? Is it a data member of your B class?

try doing
1
2
3
4
5
6
7
bool A::check()
{
    if (mouse button clicked) {
        std::cout << "click!\n";
        restart = true;
    }
}

to see if it's even triggering restart to become true.
Last edited on Aug 23, 2014 at 6:11am
Aug 23, 2014 at 6:10am
cout prints are a poor man's debugger.

Just set a breakpoint. =P

If you litter your code with cout statements you'll have to remember to go back and remove them all.
Aug 23, 2014 at 6:13am
Yes you're right, also I refresh the page but your reply doesn't show up until later, I wouldn't have even bothered posting if I had known.
Aug 23, 2014 at 6:23am
Haha, it's fine. =)

I'm always happy to see other people chiming in. Don't let me stop you.
Aug 23, 2014 at 9:09pm
Thanks Ganado and Disch, I think I know what the problem is. Its a design issue, I figured out an alternative solution.

Thanks again.
Aug 23, 2014 at 9:31pm
Printing as a debugging method does have certain advantages.
* It's useful to narrow down the failure of a long-running procedure. Imagine having to skip 500 breakpoint hits to get to the one you care about.
* It's often useful when the presence of a debugger affects program behavior.
Last edited on Aug 23, 2014 at 9:31pm
Aug 23, 2014 at 11:04pm
It's useful to narrow down the failure of a long-running procedure. Imagine having to skip 500 breakpoint hits to get to the one you care about.

Imagine having conditional breakpoints!
Aug 23, 2014 at 11:09pm
Conditional breakpoints have limited expressive power, or the exact condition that indicates failure may not be entirely clear. Conditional breakpoints also make the debuggee much slower, often too slow to be useful.
Topic archived. No new replies allowed.