Thread: Assign a reference outside the constructor

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217

    Assign a reference outside the constructor

    Is it possible to set the address of a reference outside of the constructor?

    Code:
    int& a = b;
    works

    Code:
    int& a;
    a = b;
    Obviously just changes the value, not the reference.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Do you have something like?
    Code:
    class N
    {
        int& a;
        public:
        N(int& b) {???}
    };
    You'll need to use the constructors initializer list:
    Code:
        N(int& b): a(b) {}
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Quote Originally Posted by anon View Post
    Do you have something like?
    Code:
    class N
    {
        int& a;
        public:
        N(int& b) {???}
    };
    You'll need to use the constructors initializer list:
    Code:
        N(int& b): a(b) {}
    Does that work outside of constructors, such as a method? Like this:

    Code:
    class ClassCache
    {
    
    private:
      ScriptManager& scriptManager;
      ..
      ..
    
    public:
      ..
      ..
      void Assign(ScriptManager& scriptM): scriptManager(scriptM){}
    };
    EDIT: after a test, it turns out i HAVE to do that constructor initializer list thing for every object reference in the class. I guess its because its unsafe to have an unitialized object reference..?
    Last edited by 39ster; 05-03-2008 at 06:49 AM.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The language forbids you from having uninitialized references.

    If you need to change what the reference is referencing, you can always use pointers.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Yeah ill just leave it as a pointer. I can always do * to convert it to a reference or value.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Textbox
    By maxorator in forum Windows Programming
    Replies: 20
    Last Post: 09-25-2005, 10:04 AM
  3. How to: Use OpenGL with Jgrasp
    By Pickels in forum Game Programming
    Replies: 3
    Last Post: 08-30-2005, 10:37 AM
  4. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM
  5. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 11:02 AM