Jump to content

  • Log In with Google      Sign In   
  • Create Account


Reference initialization required.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 gasto   Members   -  Reputation: 262

Like
0Likes
Like

Posted 14 April 2014 - 08:27 PM

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.

Sponsor:

#2 ChaosEngine   Crossbones+   -  Reputation: 2150

Like
3Likes
Like

Posted 14 April 2014 - 08:44 PM

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

#3 SeanMiddleditch   Members   -  Reputation: 4094

Like
2Likes
Like

Posted 14 April 2014 - 09:13 PM

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.

#4 gasto   Members   -  Reputation: 262

Like
0Likes
Like

Posted 15 April 2014 - 10:31 AM

 

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, 15 April 2014 - 11:18 AM.

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

#5 gasto   Members   -  Reputation: 262

Like
0Likes
Like

Posted 15 April 2014 - 10:44 AM

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, 15 April 2014 - 11:02 AM.

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

#6 rozz666   Members   -  Reputation: 600

Like
1Likes
Like

Posted 15 April 2014 - 01:05 PM

 

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



#7 Satharis   Members   -  Reputation: 949

Like
0Likes
Like

Posted 15 April 2014 - 06:34 PM

 

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS