1

WRT below code, Abc & Xyz are 2 classes.

I get a reference in DoOperation(Xyz& temp) and want to assign it to a class data member i.e mem1, so that this reference can be used by other member functions lile DOOperation_2(), DOOperation_3() etc..

I know we can't declare a reference in C++ without initialization. But how do I handle such a scenario in C++ ?

class Abc
{
public:
    Xyz& mem; //ILLEGAL IN C++
     void DoOperation(Xyz& temp)
     {
        mem = temp;
     }   
     void DOOperation_2()
     {

     }
     DOOperation_3()
     {

     }


};
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
codeLover
  • 3,720
  • 10
  • 65
  • 121
  • You have 2 typos in your code `Xyz` vs `xYZ` and `mem1` vs `mem`. – mch Jul 11 '17 at 12:57
  • 1) Pass the reference to the class constructor: `Abc(Xyz& xyz) : mem1(xyz) {...}` 2) Use a pointer (or smart pointer like `std::unique_ptr`) instead of a reference. – 0x5453 Jul 11 '17 at 12:58
  • 1
    That is valid code, *if* you have a constructor of `Abc` which initializes the reference to actually reference some real instance of `Xyz`. *However* then the assignment in `DoOperation` doesn't actually reassign the reference because that's not possible, instead it basically does `mem1.operator=(temp)` – Some programmer dude Jul 11 '17 at 12:58
  • 1
    `Xyz& mem1; //ILLEGAL IN C++` What makes you say that? This is a member declaration. It's quite different to a variable declaration. – StoryTeller - Unslander Monica Jul 11 '17 at 12:58

4 Answers4

1

It's simple: Use a pointer. Unlike references, pointers are assignable.

class Abc
{
public:
     Xyz* mem1;
     void DoOperation(Xyz& temp)
     {
        mem1 = &temp;
     }   
};
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
1

As you correctly noted, you need to initialize the reference. You also cannot change what a reference "points" to after it has been initialized.

So the simple solution here is to just use a pointer

class Abc {
public:
    Xyz* mem1{nullptr};
    void DoOperation(Xyz* ptr) {
        mem1 = ptr;
    }
};

And then you can use mem1 later on in a method like this (with a nice assert)

void Abc::SomeMethod() {
    assert(this->mem1);
    mem1->something();
}

Note that a reference is very similar to a T* const (const pointer). The key differences are outlined nicely in this answer Difference between const. pointer and reference?


If you absolutely have to use a reference, the only thing you can do is to initialize a reference in a constructor during initialization.

class Abc {
public:
    Xyz& mem1;
    Abc(Xyz& mem1_in) : mem1{mem1_in} {}
};
Curious
  • 20,870
  • 8
  • 61
  • 146
1

Initialize it in constructor:

class Abc
{
public:
    Xyz& mem1;
     Abc (xYZ& temp) : mem1(temp) {} 
// ...
};

or use pointer:

class Abc
{
public:
    Xyz* mem = nullptr;

    void DoOperation(xYZ& temp)
    {
        mem = &temp;
    }
// ...
};
Jarod42
  • 203,559
  • 14
  • 181
  • 302
1

Use a pointer instead.

class Abc
{
public:
     Xyz* mem1; // pointer
     void DoOperation(xYZ& temp)
     {
        mem1 = &temp; // take the address of the variable and save it to the pointer
     }   
     void DOOperation_2()
     {

     }
     DOOperation_3()
     {

     }


};
Snackoverflow
  • 5,332
  • 7
  • 39
  • 69