Skip to main content
GameDev.net gamedev.net
🔒 Locked

Reference initialization required.

Started by gasto Apr 15, 2014 at 2:27 AM 5 replies 2.1k views
Original Post
gasto
gasto

What's the rationale behind the reference initialization requirement?

Pointers and general objects do not need to be initialized, it is recommended but not enforced by the compiler.

Intel Core 2 Quad CPU Q6600, 2.4 GHz. 3GB RAM. ATI Radeon HD 3400.
ChaosEngine
ChaosEngine

I assume you're talking C++?

References in C++ are immutable. Once they are set, they cannot be made to refer to another variable. There isn't actually any syntax that would allow that.


int var1 = 0;

int& ref = var1;

ref = 10; // sets var to be 10

int var2 = 666;

ref = var2; // sets var1 to be the value of var2

Imagine if you could reset references in C++


class MyClass
{
   int var_;
public:
   MyClass(int var)
     : var_(var) {}

   void operator=(int var)
   {
       var_ = var;
   }
};

MyClass c1(10);
MyClass c2(20);

MyClass& ref; // what does this refer to?

ref = c1; // hang on... did you mean ref REFERS to c1 or are invoking ref->operator=(10)?
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
SeanMiddleditch
SeanMiddleditch

What's the rationale behind the reference initialization requirement?
Pointers and general objects do not need to be initialized, it is recommended but not enforced by the compiler.


Objects _do_ need to be initialized, always, every time. If you don't explicitly initialize an object then its default constructor is invoked to initialize it automatically. An object can have its default constructor removed (or set to a stricter access level) making an explicit initialization required.

A reference is an alias of an object. An unbound reference makes no sense. You can't bind a reference after it's created. You can't check if a reference is bound or not. If you don't want that behavior, don't use a reference.
Sean Middleditch – Game Systems Engineer – Join my team!
gasto
gasto

I assume you're talking C++?

References in C++ are immutable. Once they are set, they cannot be made to refer to another variable. There isn't actually any syntax that would allow that.


int var1 = 0;

int& ref = var1;

ref = 10; // sets var to be 10

int var2 = 666;

ref = var2; // sets var1 to be the value of var2

Imagine if you could reset references in C++


class MyClass
{
   int var_;
public:
   MyClass(int var)
     : var_(var) {}

   void operator=(int var)
   {
       var_ = var;
   }
};

MyClass c1(10);
MyClass c2(20);

MyClass& ref; // what does this refer to?

ref = c1; // hang on... did you mean ref REFERS to c1 or are invoking ref->operator=(10)?

You are right.
It is only in the initialization of a reference that the value at the right of an assignment is treated as an lvalue without using the ampersand(address of) operator.
As such, one can have either object-refering-to change or object-refered-to assignment when using the assignment operator. They are obviously mutually exclusive and the latter is senseless alone.

Intel Core 2 Quad CPU Q6600, 2.4 GHz. 3GB RAM. ATI Radeon HD 3400.
gasto
gasto

In Python, references are mutable and their labels (referers)are changed to refer to other referents(objects) by using an = operation.


a=20
b=a
b=30 # Changing object refering-to (referent 30)
del a # Who cares?! b is referring to another object now. Object 20 is garbage collected.  
print b

Output:


30
Intel Core 2 Quad CPU Q6600, 2.4 GHz. 3GB RAM. ATI Radeon HD 3400.
rozz666
rozz666

In Python, references are mutable and their labels (referers)are changed to refer to other referents(objects) by using an = operation.


a=20
b=a
b=30 # Changing object refering-to (referent 30)
del a # Who cares?! b is referring to another object now. Object 20 is garbage collected.  
print b

Output:


30

Python reference are C++ pointers (they are also garbage collected, of course).

Satharis
Satharis

In Python, references are mutable and their labels (referers)are changed to refer to other referents(objects) by using an = operation.


a=20
b=a
b=30 # Changing object refering-to (referent 30)
del a # Who cares?! b is referring to another object now. Object 20 is garbage collected.  
print b

Output:


30

Just because they're named the same doesn't mean they are implemented the same.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.