Jump to content
  • Advertisement
Sign in to follow this  
gasto

Reference initialization required.

This topic is 2037 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement

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)?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

 

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.

Edited by gasto

Share this post


Link to post
Share on other sites

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
Edited by gasto

Share this post


Link to post
Share on other sites

 

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).

Share this post


Link to post
Share on other sites

 

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!