A pointer reference as l-value
-
hi Here is my problem: I don't understand why in the following code the second line is error and the third is ok. In the first case where the error is I will get a copy and in the second reference to the pointer. Why the reference is considered l-value?
// Node class
class Node {
private:
int key;
Node* left;
Node* right;typedef Node\* Link;
public:
Node() { key=-1; left=NULL; right=NULL;};
void setKey(int aKey) { key = aKey; };
int Key() { return key; };
Link Left() { return left; };
Link& Right() { return right; };
};int _tmain(int argc, _TCHAR* argv[])
{
Node *p= new Node();
p->Left()= new Node(); //error : error C2106: '=' : left operand must be l-valuep->Right()= new Node(); //this one is lvalue return 0;
}
-
hi Here is my problem: I don't understand why in the following code the second line is error and the third is ok. In the first case where the error is I will get a copy and in the second reference to the pointer. Why the reference is considered l-value?
// Node class
class Node {
private:
int key;
Node* left;
Node* right;typedef Node\* Link;
public:
Node() { key=-1; left=NULL; right=NULL;};
void setKey(int aKey) { key = aKey; };
int Key() { return key; };
Link Left() { return left; };
Link& Right() { return right; };
};int _tmain(int argc, _TCHAR* argv[])
{
Node *p= new Node();
p->Left()= new Node(); //error : error C2106: '=' : left operand must be l-valuep->Right()= new Node(); //this one is lvalue return 0;
}
Left() returns a
Link
; and Right() returns aLink&
Nihil obstat
-
hi Here is my problem: I don't understand why in the following code the second line is error and the third is ok. In the first case where the error is I will get a copy and in the second reference to the pointer. Why the reference is considered l-value?
// Node class
class Node {
private:
int key;
Node* left;
Node* right;typedef Node\* Link;
public:
Node() { key=-1; left=NULL; right=NULL;};
void setKey(int aKey) { key = aKey; };
int Key() { return key; };
Link Left() { return left; };
Link& Right() { return right; };
};int _tmain(int argc, _TCHAR* argv[])
{
Node *p= new Node();
p->Left()= new Node(); //error : error C2106: '=' : left operand must be l-valuep->Right()= new Node(); //this one is lvalue return 0;
}
You code makes no sense, you are trying to set values into functions. The functions
Left()
andRight()
are called to return the values of those pointers. You should be using theNode
pointersleft
andright
; although you have made them private so you cannot as the class stands. Either make them public, or provide setter functions for them. -
You code makes no sense, you are trying to set values into functions. The functions
Left()
andRight()
are called to return the values of those pointers. You should be using theNode
pointersleft
andright
; although you have made them private so you cannot as the class stands. Either make them public, or provide setter functions for them.Yes, of course. Still, I am interested to know why the code does not work with both cases, or why only in the second case;
void setLeft(Node* l) { left = l; };
Also notice if I do, it works:
Node \*r= p->Left(); r= new Node(); //error : error C2106: '=' : left operand must be l-value
Maybe because in the second case it references directly a l-value by reference and in the second it uses a copy to a pointer?
-
Yes, of course. Still, I am interested to know why the code does not work with both cases, or why only in the second case;
void setLeft(Node* l) { left = l; };
Also notice if I do, it works:
Node \*r= p->Left(); r= new Node(); //error : error C2106: '=' : left operand must be l-value
Maybe because in the second case it references directly a l-value by reference and in the second it uses a copy to a pointer?
-
Yes, of course. Still, I am interested to know why the code does not work with both cases, or why only in the second case;
void setLeft(Node* l) { left = l; };
Also notice if I do, it works:
Node \*r= p->Left(); r= new Node(); //error : error C2106: '=' : left operand must be l-value
Maybe because in the second case it references directly a l-value by reference and in the second it uses a copy to a pointer?
If you do
Node *r= p->Left();
r= new Node();p->left is still NULL afterwards, because you copied the (NULL) pointer and assigned the allocated object to the copy. If you do
p->Right() = new Node();
instead, p->right is assigned the newly allocated object.
-
If you do
Node *r= p->Left();
r= new Node();p->left is still NULL afterwards, because you copied the (NULL) pointer and assigned the allocated object to the copy. If you do
p->Right() = new Node();
instead, p->right is assigned the newly allocated object.
yeap, I guess the ideea is not to use copies as out params.
-
If you do
Node *r= p->Left();
r= new Node();p->left is still NULL afterwards, because you copied the (NULL) pointer and assigned the allocated object to the copy. If you do
p->Right() = new Node();
instead, p->right is assigned the newly allocated object.
so the ideea in case of c++ pointers will be not to use copies as out params? only references. I mean not in all cases. Here the pointer has already memory allocated. So to use the copy is ok; but I cannot allocate memory to a copy - of a pointer. Does anyone know to explain what is exactly a copy of a pointer and how it is handled internaly?
class CTest
{
private:
char *a;
public:
CTest(int n=10)
{
a= new char[10];
a="george";
}//return by value char\* GetPtr() { return a; }
};
int _tmain(int argc, _TCHAR* argv[])
{
CTest obj;char *ry= obj.GetPtr();
cout<<"sir="<
-
If you do
Node *r= p->Left();
r= new Node();p->left is still NULL afterwards, because you copied the (NULL) pointer and assigned the allocated object to the copy. If you do
p->Right() = new Node();
instead, p->right is assigned the newly allocated object.
yeap: rule, do not allocate memory to copies. Still I don't understand what happens internally. In the bellow code ptOut will be a parameter on the internal Stack? How are handled params which come as refs? Internaly all the params, pointers etc.. have also their own address? Or they are taken like some kind of aliases.
class CTest
{
private:
char *a;
public:
CTest(int n=10)
{
a= new char[10];
a="george";
}//By Reference void GetPtrEx2(char\* &ptOut) { ptOut=a; }
};
-
yeap: rule, do not allocate memory to copies. Still I don't understand what happens internally. In the bellow code ptOut will be a parameter on the internal Stack? How are handled params which come as refs? Internaly all the params, pointers etc.. have also their own address? Or they are taken like some kind of aliases.
class CTest
{
private:
char *a;
public:
CTest(int n=10)
{
a= new char[10];
a="george";
}//By Reference void GetPtrEx2(char\* &ptOut) { ptOut=a; }
};
In the function GetPtrEx2() the address of the parameter will be passed on the stack. So with the assignment you change the address the pointer you called the function with points to. You should however check your constructor. I don't think it will work as intended.