Home
Developer Resources
QNX RTOS v4
QNX RTOS v4 Knowledge Base

QNX RTOS v4 Knowledge Base

Foundry27
Foundry27
QNX RTOS v4 project
Resources

QNX RTOS v4 Knowledge Base

Title C++ code modifying values of class members
Ref. No. QNX.000009311
Category(ies) Development
Issue I'm porting an application to C++. Everything is going fine, except for one nagging problem-my generated code often modifies the values of class members without any source code explicitly telling it to do so. Here's a test case to show you what I mean:

#include <iostream.h>

class count{
x09private:
x09x09int num;
x09public:
x09x09count(){
x09x09x09num=7;
x09x09x09//cout << "count constructor: num = " << num ;
x09x09}

x09x09~count(){
x09x09x09cout << "count destrucotr: num = " << num << endl;
x09x09}

x09x09count inc(){
x09x09x09num++;
x09x09x09return *this;
x09x09}
x09x09count dec(){
x09x09x09num--;
x09x09x09return *this;
x09x09}
x09x09int get(){
x09x09x09return num;
x09x09}

};

x09void
main()
{
x09count a;

x09cout << "nna is (10?) " << a.inc().inc().inc().get() << 'n';
x09cout << "a is (10?)" << a.get() << 'n';

}

Here's the output that I get:

x09a is (10?) 10
x09a is (10?) 8

I haven't altered object a between the first output and the second. So what could be causing the values of the members in object a to change?
Solution In your example, you've declared the count.inc() and count.dec() methods as returning an instance of the class count. The problem is that the instance of the class returned isn't the same instance that you passed in. So what you get back from these methods is a "copy" of the object, not the real object. As a result, any further operations affect only the copies; the original remains unchanged.

Let's look at how your code example generates the first output line (note that the num member is incremented via the inc() member function):

1x09a.num is incremented once (a.num = 8) and a copy of object a is returned; let's call this b

2x09b.num is incremented (b.num = 9) and a copy of b is returned: c

3x09c.num is incremented (c.num = 10), and yet another copy is returned: d

4x09the value of d.num (10) is fetched via d.get() and displayed

x09To generate the second output line, your code example simply fetches the value of a.num (8) via a.get() and then displays the value.
x09The best way to fix your problem is to declare the count.inc() and count.dec() methods as returning a reference (&) to the current object. This approach prevents the copies from being returned and ensures the correct instance is used. It also speeds up execution by avoiding the memory allocation and deallocation required to return multiple copies.

Here's the corrected code:

#include <iostream.h>

class count{
x09private:
x09x09int num;
x09public:
x09x09count(){
x09x09x09num=7
x09x09x09//cout << "count constructor: num = " << num ;
x09x09}
x09x09count(int i){
x09x09x09num=i;
x09x09cout  << "count constructor: num = << num << endl;
x09x09}

x09x09~count(){
x09x09cout << "count destructor: num = " << num << endl;
x09x09}
x09x09&count inc(){ // return a reference to the actual object, not a copy num++;
x09x09x09return *this;
x09x09}
x09x09&count dec(){ // return a reference to the actual object, not a copy num--;
x09x09x09return *this;
x09x09}

x09x09int get(){
x09x09x09return num;
x09x09}

};
x09void
main()
{
x09count a;

x09cout << "nna is (10?) " << a.inc().inc().inc().get() << 'n';
x09cout << "a is (10?) " << a.get() <<'n';

The above code will output:

x09a is (10?) 10
x09a is (10?) 10

just as you'd expect.

For more information, see the discussion on "References" (r8.4.3) in the Reference Manual section of C++ Programming Language, which accompanies WATCOM C++ for QNX.