Initializing Object References...See Example Code!!

Discussion in 'C++' started by richard_l@latter.demon.co.uk, May 15, 2005.

  1. Guest

    Hello All,

    Please see attached example code.

    I'm writing a program whereby I would like to reference an object from
    another object.

    For example I have an object called Value and I have created a
    reference to that object in the class Container by using Value& m_Val.


    At present the only way I can initialise this object is by initialising
    the object on creation eg.

    Container (Value& Val) : m_Value(Val) { ... }

    Is there another way of initalizing this reference whithout using this
    method?

    Many thanks in advance,

    Richard Latter


    Code....


    #include <iostream>

    using namespace std;


    class Value
    {
    public:
    int& m_nVal;

    public:
    Value (int& nVal) : m_nVal(nVal) { }

    public:
    ~Value () { }
    };


    class Container
    {
    public:
    Value& m_Value;

    Container (Value& Val) : m_Value(Val) { }
    public:
    ~Container () { }
    };



    int main (void)
    {
    int i=5;
    int& ri=i;
    Value val(i);
    Value &Vref=val;

    Container cont(val);

    cout << "i = " << i << endl;
    cout << "Value = " << val.m_nVal << endl;
    cout << "Container = " << cont.m_Value.m_nVal << endl;

    i = 20;

    cout << "i = " << i << endl;
    cout << "Value = " << val.m_nVal << endl;
    cout << "Container = " << cont.m_Value.m_nVal << endl;

    return (0);
    }
    , May 15, 2005
    #1
    1. Advertising

  2. wrote:
    > Please see attached example code.
    >
    > I'm writing a program whereby I would like to reference an object from
    > another object.
    >
    > For example I have an object called Value and I have created a
    > reference to that object in the class Container by using Value& m_Val.
    >
    >
    > At present the only way I can initialise this object is by
    > initialising the object on creation eg.
    >
    > Container (Value& Val) : m_Value(Val) { ... }
    >
    > Is there another way of initalizing this reference whithout using this
    > method?
    > [...]


    No. Members that are references have to be initialised in the constructor
    initialiser list. There is no other way.

    V
    Victor Bazarov, May 15, 2005
    #2
    1. Advertising

  3. Rapscallion Guest

    wrote:
    > I'm writing a program whereby I would like to reference an object

    from
    > another object.
    > For example I have an object called Value and I have created a
    > reference to that object in the class Container by using Value&

    m_Val.
    > At present the only way I can initialise this object is by

    initialising
    > the object on creation eg.
    >
    > Container (Value& Val) : m_Value(Val) { ... }
    >
    > Is there another way of initalizing this reference whithout using

    this
    > method?


    What is the problem you encounter? If you don't have a Value when you
    create a Container then use a pointer instead of a reference. Pointers
    can be reseated and NULL.

    R.C.
    Rapscallion, May 15, 2005
    #3
  4. Guest

    In any case, you should *always* use a pointer in this context.
    Pointers can be documented as "held" or "owned" (indicating which
    object is reponsible for the pointed-to object). A referenced object
    cannot be owned. Furthermore, in most cases where ownership of one
    object is passed to another, a smart pointer should be used. /david
    , May 15, 2005
    #4
    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. Alexander Stippler

    initializing references / null pointer

    Alexander Stippler, Oct 18, 2003, in forum: C++
    Replies:
    1
    Views:
    360
    Rob Williscroft
    Oct 18, 2003
  2. hrmadhu
    Replies:
    4
    Views:
    435
    hrmadhu
    Dec 28, 2003
  3. Reckoner
    Replies:
    3
    Views:
    351
  4. Jack Bates
    Replies:
    3
    Views:
    294
    Steven D'Aprano
    Aug 6, 2011
  5. K. Frank
    Replies:
    20
    Views:
    381
Loading...

Share This Page