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.
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.
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)?
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.
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 var2Imagine 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.
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
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 bOutput:
30
Python reference are C++ pointers (they are also garbage collected, of course).
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 bOutput:
30
Just because they're named the same doesn't mean they are implemented the same.
This topic has been locked by a moderator. New replies are not allowed.
GameDev.net uses cookies to ensure you have the best experience on our platform. Learn more