Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. A pointer reference as l-value

A pointer reference as l-value

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    George Nistor
    wrote on last edited by
    #1

    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-value

    p->Right()= new Node(); //this one is lvalue
    
    return 0;
    

    }

    M L 2 Replies Last reply
    0
    • G George Nistor

      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-value

      p->Right()= new Node(); //this one is lvalue
      
      return 0;
      

      }

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #2

      Left() returns a Link; and Right() returns a Link&

      Nihil obstat

      1 Reply Last reply
      0
      • G George Nistor

        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-value

        p->Right()= new Node(); //this one is lvalue
        
        return 0;
        

        }

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        You code makes no sense, you are trying to set values into functions. The functions Left() and Right() are called to return the values of those pointers. You should be using the Node pointers left and right; although you have made them private so you cannot as the class stands. Either make them public, or provide setter functions for them.

        G 1 Reply Last reply
        0
        • L Lost User

          You code makes no sense, you are trying to set values into functions. The functions Left() and Right() are called to return the values of those pointers. You should be using the Node pointers left and right; although you have made them private so you cannot as the class stands. Either make them public, or provide setter functions for them.

          G Offline
          G Offline
          George Nistor
          wrote on last edited by
          #4

          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?

          L F 2 Replies Last reply
          0
          • G George Nistor

            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?

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            See http://msdn.microsoft.com/en-us/library/f90831hc.aspx[^].

            1 Reply Last reply
            0
            • G George Nistor

              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?

              F Offline
              F Offline
              Freak30
              wrote on last edited by
              #6

              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.

              G 3 Replies Last reply
              0
              • F Freak30

                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.

                G Offline
                G Offline
                George Nistor
                wrote on last edited by
                #7

                yeap, I guess the ideea is not to use copies as out params.

                1 Reply Last reply
                0
                • F Freak30

                  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.

                  G Offline
                  G Offline
                  George Nistor
                  wrote on last edited by
                  #8

                  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="<

                  1 Reply Last reply
                  0
                  • F Freak30

                    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.

                    G Offline
                    G Offline
                    George Nistor
                    wrote on last edited by
                    #9

                    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;
                    }
                    

                    };

                    F 1 Reply Last reply
                    0
                    • G George Nistor

                      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;
                      }
                      

                      };

                      F Offline
                      F Offline
                      Freak30
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups