16

This is probably a simple question, but I'm stuck on it. I'm trying to pass an object down from ObjectA to ObjectB (which is a member of ObjectA) via it's constructor. However, instead of passing by value, I want to pass only a const reference and store that reference indefinitely. The problem is that I'm not sure how to do it.

I can get this working with pointers like this:

class ClassB
{
private:
    int *ptrInternalX;
public:
    ClassB( int *tempX );
}

ClassB::ClassB( int *tempX )
{
    ptrInternalX = tempX
}

This way, an object is created and passed a pointer to an int, and that pointer is stored inside the class for later use.

However, pointers make me worry about memory leaks and other issues when using larger objects, so I'd like to try to do something like this using 'constant references' (const &). However, this doesn't seem to work...

class ClassB
{
private:
    int &internalX;
public:
    ClassB( const int &tempX );
}

ClassB::ClassB( const int &tempX )
{
    internalX = tempX
}

I know that references are essentially an 'alias' for an existing variable (a different name that refers to the same memory address), and they need to be initialized immediately using an existing variable. So this creates an error in my program!

Is this even possible? Or is there a better/more clear way of doing something like this? The reasons I want to use constant references are the speed of passing just a reference instead of a large object while keeping the data safe from accidental changes and memory leaks... I'm sure that there is a simple and straight-forward way to do this but I'm not very familiar with const reference passing.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
MrKatSwordfish
  • 1,504
  • 5
  • 18
  • 30
  • 4
    Use this: http://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor – chris Jan 05 '13 at 07:15
  • 3
    re "pointers make me worry about memory leaks and other issues when using larger objects, so I'd like to try to do something like this using 'constant references' (const &)". References have *the same problem*, plus that a reference member makes your class non-assignable. So while there can be good reasons to use references, the stated reason is not one. – Cheers and hth. - Alf Jan 05 '13 at 07:25
  • I was under the impression that because a reference couldn't have the memory address that it refers to altered, it is a little bit safer than a pointer. I'll look into it a bit more, thanks for the help! – MrKatSwordfish Jan 05 '13 at 07:29

1 Answers1

21
class    ClassB
{
    private:
        const int&    internalX;
    public:
        ClassB(const int& tempX);
}

ClassB::ClassB(const int& tempX):
     internalX(tempX)
{
}

As you said, a reference has to be initialized immediately. Thus, if you want your reference to be a class member, you have to use your constructor's initialization list to set it.

(This short explanation might also make things clearer for you, as it is specifically centered on the situation you've just met)

Good luck

cmc
  • 2,061
  • 1
  • 19
  • 18
  • 3
    1) `internalX` should be const 2) this could bind a reference to a constructor-local temporary which is bad – Pubby Jan 05 '13 at 07:18
  • Yep internalX should be const, fixed – cmc Jan 05 '13 at 07:21
  • Thanks! This is really helpful. I had no idea that initialization lists were even a thing... :( haha – MrKatSwordfish Jan 05 '13 at 07:34
  • 3
    You're welcome! Please look at the links I gave you then, `initialization list` is an easy concept that you must master in order to develop in C++! – cmc Jan 05 '13 at 07:37