Problem with references

Discussion in 'C++' started by mlt, Jan 11, 2009.

  1. mlt

    mlt Guest

    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?
     
    mlt, Jan 11, 2009
    #1
    1. Advertising

  2. mlt

    Guest

    On Jan 11, 6:42 pm, "mlt" <> wrote:
    > 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
     
    , Jan 11, 2009
    #2
    1. Advertising

  3. mlt

    mlt Guest

    <> wrote in message
    news:...
    On Jan 11, 6:42 pm, "mlt" <> wrote:
    > 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.
     
    mlt, Jan 12, 2009
    #3
  4. mlt

    SG Guest

    On 12 Jan., 00:42, "mlt" <> wrote:

    > 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
     
    SG, Jan 12, 2009
    #4
  5. mlt

    SG Guest

    On 12 Jan., 01:05, "mlt" <> wrote:
    > 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
     
    SG, Jan 12, 2009
    #5
  6. mlt

    mlt Guest

    "SG" <> wrote in message
    news:...
    On 12 Jan., 00:42, "mlt" <> wrote:

    > 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?
     
    mlt, Jan 12, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roger Leigh
    Replies:
    8
    Views:
    473
    Karl Heinz Buchegger
    Nov 17, 2003
  2. Replies:
    3
    Views:
    484
    Victor Bazarov
    Nov 10, 2004
  3. DanielEKFA
    Replies:
    8
    Views:
    641
    DanielEKFA
    May 16, 2005
  4. Replies:
    8
    Views:
    760
    Bruno Desthuilliers
    Dec 12, 2006
  5. Lars Willich
    Replies:
    13
    Views:
    906
    Ian Shef
    Oct 23, 2007
Loading...

Share This Page