Problem with references

M

mlt

I have 3 classes A, B and C. A reference to B is passed to C from A:

class A {

private:
B b;
C c;
float time;
public:

void init() {
time = 0.00f;

// Pass a reference of b to c.
c.setup(b);
}

void run() {

// update B, why is this not visible in C??
b.update(time);

time +=0.001;
}
};





class B {
private:
float time;

public:
void update(float t){
time = t;
}

float getTime() {
time;
}
};





class C {

private:
B b;

public:

// Takes a reference to B
void setup(B & b_in) {
b = b_in;
}

void testB() {
std::cout << "time in b = " << b.getTime()<< std::endl;
}
};



As can be seen 'b' is modified/updated in A::run() which is called multiple
times. But when I call:

C::testB();

the updated time is not printed. Why are changes made in B from A not
visible in C?
 
J

joecook

I have 3 classes A, B and C.  A reference to B is passed to C from A:

class A {

private:
    B b;
    C c;
    float time;
public:

void init() {
    time = 0.00f;

    // Pass a reference of b to c.
    c.setup(b);

}

void run() {

    // update B, why is this not visible in C??
    b.update(time);

    time +=0.001;

}
};

class B {
private:
    float time;

public:
    void update(float t){
        time = t;
    }

    float getTime() {
        time;
    }

};

class C {

private:
    B b;

public:

    // Takes a reference to B
    void setup(B & b_in) {
        b = b_in;
    }

    void testB() {
        std::cout << "time in b = " << b.getTime()<< std::endl;
    }

};

As can be seen 'b' is modified/updated in A::run() which is called multiple
times. But when I call:

C::testB();

the updated time is not printed. Why are changes made in B from A not
visible in C?

You can't call "C::testB()" unless you have an object. (It isn't a
static function).

I think you are confused between what is a class and what is an
object. You can alter an object's state. You can't make changes in B
or C. You can only make changes on objects of those types.

Joe Cook
 
M

mlt

I have 3 classes A, B and C. A reference to B is passed to C from A:

class A {

private:
B b;
C c;
float time;
public:

void init() {
time = 0.00f;

// Pass a reference of b to c.
c.setup(b);

}

void run() {

// update B, why is this not visible in C??
b.update(time);

time +=0.001;

}
};

class B {
private:
float time;

public:
void update(float t){
time = t;
}

float getTime() {
time;
}

};

class C {

private:
B b;

public:

// Takes a reference to B
void setup(B & b_in) {
b = b_in;
}

void testB() {
std::cout << "time in b = " << b.getTime()<< std::endl;
}

};

As can be seen 'b' is modified/updated in A::run() which is called
multiple
times. But when I call:

C::testB();

the updated time is not printed. Why are changes made in B from A not
visible in C?

You can't call "C::testB()" unless you have an object. (It isn't a
static function).

I think you are confused between what is a class and what is an
object. You can alter an object's state. You can't make changes in B
or C. You can only make changes on objects of those types.




I know that, it was just to show the point. When I call 'testB()' it of
course done with something like:

// From some context:
....
C c;

c.testB();

where c is an instance of C.

The point is that the code compiles and runs but for some reason is the
changes to b is not visible from C even though it holds a reference to B.
 
S

SG

class C {

private:
    B b;

public:

    // Takes a reference to B
    void setup(B & b_in) {
        b = b_in;
    }

It seems that you are confused about references and objects. 'b_in'
is a reference to an object of type B but 'b' is just another object
of type B. In the function setup() you simply copy-assign one object
to another. You're copying the object refered to by 'b_in' to the
object 'b'. When you later change the object refered to by 'b_in' you
can't expect to see the same change in the member C::b because 'b' is
a different object.

It is a big difference compared to Java/C#/D.

You might want to use something like this:

class C {
B* pb;
public:
// Takes a reference to B
void setup(B & b_in) {
pb = &b_in;
}
...

Now, 'pb' points the the original object. You may check the time via

pb->getTime();


Cheers!
SG
 
S

SG

The point is that the code compiles and runs but for some reason is the
changes to b is not visible from C even though it holds a reference to B.

No, it doesn't. It holds a different object of type B. There is
little difference between class types and build-in types _in this
respect_:

int i1 = 23;
int & i1ref = i1;
// i1 and i1ref are practically the same object
int i2 = i1ref;
// i2 is a new object we initialized with i1/i1ref
i1 = 42;
// i2 is still 23
cout << i2 << endl; // will still print 23

HTH,
SG
 
M

mlt

class C {

private:
B b;

public:

// Takes a reference to B
void setup(B & b_in) {
b = b_in;
}

It seems that you are confused about references and objects. 'b_in'
is a reference to an object of type B but 'b' is just another object
of type B. In the function setup() you simply copy-assign one object
to another. You're copying the object refered to by 'b_in' to the
object 'b'. When you later change the object refered to by 'b_in' you
can't expect to see the same change in the member C::b because 'b' is
a different object.

It is a big difference compared to Java/C#/D.

You might want to use something like this:

class C {
B* pb;
public:
// Takes a reference to B
void setup(B & b_in) {
pb = &b_in;
}
...

Now, 'pb' points the the original object. You may check the time via

pb->getTime();




Ok so what I want is only possible with pointers and cannot be made using
only references?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top