CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 28
  1. #1
    Join Date
    May 2009
    Posts
    19

    Question How to create a pointer to 'this'?

    Hi all,

    I'm wondering how to create a pointer to 'this'. I have an implementation of a dynamic memory list, but it takes pointers to the objects to allow the addition of arrays, and so, when I tried to add 'this' it doesn't work. I either get 'illegal lvalue to unary &', or, when I tried to write a method that took single objects, segmentation faults at runtime. So I'm wondering if there is a way to create a pointer to a this object.

    Thanks,
    Singing Boyo

  2. #2
    Join Date
    Apr 2008
    Posts
    725

    Re: How to create a pointer to 'this'?

    to get a pointer to 'this' you can do similar to:
    Code:
    class MyClass
    {
    public:
      MyClass()
      {
        thisCpy = this;
        ptrThis = &thisCpy;
      }
    
      MyClass** ptrThis;
    
      MyClass* thisCpy;
    };
    
    int main()
    {
      MyClass a;
    
      MyClass** ptrPtr = a.ptrThis;
      
    }
    I'm not sure that's what you're after, though?
    Last edited by Amleto; January 16th, 2010 at 07:25 AM.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How to create a pointer to 'this'?

    Quote Originally Posted by Singing Boyo View Post
    Hi all,

    I'm wondering how to create a pointer to 'this'.
    So I'm wondering if there is a way to create a pointer to a this object.
    In C++, "this" is already a pointer to an object. There is no such thing as a "this" object.

    Instead of describing, show us actual code so we know what you're trying to do.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    May 2009
    Posts
    19

    Re: How to create a pointer to 'this'?

    The code is long as it is part of a mini-compiler (tracking variable names) but here are the exact methods being used.

    Also, sorry for the bad terminology - I'm more of a java person, so to me, everything is an object

    the code that needs a pointer to create the pointer to the this pointer...

    Code:
    PostInitObject::PostInitObject(PType type)
            //PObject is a typedef for PostInitObject
    	List<PObject*>* tlist = postInits->data2For(type);
            PObject** ptrtothis = &this;//non-lvalue in unary '&'
            //I also tried almeto's way of doing things, and it caused the program to crash
            //this is the only source of errors in the program at this point - it is this call that fails if
            //I use almeto's idea
    	tlist->add(ptrtothis);
    And the add method for the list, though I don't believe it has any problems.
    Code:
    TEMPLT void List<t>::add(const t* obj, int len){//len is 1 by default
    	if(totalSize-objectCount>len){
    		for(int i=0; i<len; i++){
    			contents[i+objectCount] = obj[i];
    		}
    	}else{
    		increaseSize(len);
    		for(int i=0; i<len; i++){
    			contents[i+objectCount] = obj[i];
    		}
    	}
    	objectCount+=len;
    }
    and here are the full class declarations, just in case
    Code:
    class Variable:public PostInitObject{
    	static List<string>* nameList;
    	static PType vartype;//type of the variable in assembly terms
    public:
    	Variable();
    	Variable(string name, VariableType abbrevtype) throw(Duplicate);
    	void postInit();
    	void writeSizedRef();
    	virtual ~Variable();
    private:
    	string name;
    	VariableType type;
    	friend void initParser();
    	friend void endParser();
    	static void sInit();
    	static void sClose();
    };
    
    template<class t> class List{
    	t* contents;
    	int objectCount, totalSize, id;
    	static int UID;
    protected:
    	t* copy(int& length);
    	void increaseSize(int amt);
    public:
    	List();
    	List(t* contents, int length);
    	List(int listlength);
    	List(t* contents, int length, int additionalLength);
    	~List();
    	t* at(int index);
    	/* Removes the specified index from this list.
    	 * Note that if the given index is greater than the
    	 * number of objects in the List, the given index
    	 * will be modified to the last index in the last, and
    	 * the last index in the list will be removed.
    	 */
    	t remove(int& index);
    	bool remove(t obj);
    	bool contains(t obj);
    	void add(const t* object, int length=1);
    	void insert(int index, const t* object,int length=1);
    	int size(){return objectCount;}
    	bool operator == (List<t> lst){return lst.id==id;}
    private:
    //these are left undefined - lists cant be copied
    	List(const List<t>& c);
    	bool operator=(const List<t>& c);
    };

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How to create a pointer to 'this'?

    Quote Originally Posted by Singing Boyo View Post
    Also, sorry for the bad terminology - I'm more of a java person, so to me, everything is an object
    In C++, this is a pointer to the "current" object, if you are within a member function of the object.
    the code that needs a pointer to create the pointer to the this pointer...
    Why so much pointer usage? Why not use C++ container classes such as std::list? Why are you trying to code your own? The std::list class does everything your code is trying to do.

    Hopefully, you are not trying to write C++ in the style of Java, or trying to make C++ look like Java by introducing "Object" classes. If you are, the code will be overly-complicated, error-prone, and unnecessary. For example, I have been coding C++ for almost 20 years now, and I have never seen any code that requires storing the address of the this pointer.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; January 16th, 2010 at 01:23 PM.

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to create a pointer to 'this'?

    Quote Originally Posted by Singing Boyo View Post
    The code is long as it is part of a mini-compiler (tracking variable names) but here are the exact methods being used.

    Also, sorry for the bad terminology - I'm more of a java person, so to me, everything is an object

    the code that needs a pointer to create the pointer to the this pointer...

    Code:
    PostInitObject::PostInitObject(PType type)
            //PObject is a typedef for PostInitObject
        List<PObject*>* tlist = postInits->data2For(type);
            PObject** ptrtothis = &this;//non-lvalue in unary '&'
            //I also tried almeto's way of doing things, and it caused the program to crash
            //this is the only source of errors in the program at this point - it is this call that fails if
            //I use almeto's idea
        tlist->add(ptrtothis);
    And the add method for the list, though I don't believe it has any problems.
    Code:
    TEMPLT void List<t>::add(const t* obj, int len){//len is 1 by default
        if(totalSize-objectCount>len){
            for(int i=0; i<len; i++){
                contents[i+objectCount] = obj[i];
            }
        }else{
            increaseSize(len);
            for(int i=0; i<len; i++){
                contents[i+objectCount] = obj[i];
            }
        }
        objectCount+=len;
    }
    and here are the full class declarations, just in case
    Code:
    class Variable:public PostInitObject{
        static List<string>* nameList;
        static PType vartype;//type of the variable in assembly terms
    public:
        Variable();
        Variable(string name, VariableType abbrevtype) throw(Duplicate);
        void postInit();
        void writeSizedRef();
        virtual ~Variable();
    private:
        string name;
        VariableType type;
        friend void initParser();
        friend void endParser();
        static void sInit();
        static void sClose();
    };
     
    template<class t> class List{
        t* contents;
        int objectCount, totalSize, id;
        static int UID;
    protected:
        t* copy(int& length);
        void increaseSize(int amt);
    public:
        List();
        List(t* contents, int length);
        List(int listlength);
        List(t* contents, int length, int additionalLength);
        ~List();
        t* at(int index);
        /* Removes the specified index from this list.
         * Note that if the given index is greater than the
         * number of objects in the List, the given index
         * will be modified to the last index in the last, and
         * the last index in the list will be removed.
         */
        t remove(int& index);
        bool remove(t obj);
        bool contains(t obj);
        void add(const t* object, int length=1);
        void insert(int index, const t* object,int length=1);
        int size(){return objectCount;}
        bool operator == (List<t> lst){return lst.id==id;}
    private:
    //these are left undefined - lists cant be copied
        List(const List<t>& c);
        bool operator=(const List<t>& c);
    };
    In this code, where do you use 'this' or where would you like to?

  7. #7
    Join Date
    May 2009
    Posts
    2,413

    Re: How to create a pointer to 'this'?

    Quote Originally Posted by Paul McKenzie View Post
    I have been coding C++ for almost 20 years now, and I have never seen any code that requires storing the address of the this pointer.
    It's only because you haven't been using the object oriented (OO) programming paradigm.

    In OO, objects are less managed from the outside and more from the inside. This naturally means the this pointer becomes a more prominent figure.
    Last edited by nuzzle; January 19th, 2010 at 06:14 PM.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How to create a pointer to 'this'?

    Quote Originally Posted by nuzzle View Post
    It's only because you haven't been using the object oriented (OO) programming paradigm.
    ???
    I have been using it for a very long time. You can't use C++ for over 15 years professionally without using it.
    In OO, objects are less managed from the outside. Instead they're managing themselves and then the this pointer becomes a more prominent figure.
    Did you read the requirements of the OP? He or she wants to somehow use for some reason a pointer to the this pointer. I know exactly why the this pointer is used -- the question is why would anyone need a pointer to the this pointer. The OP is obviously getting Java and C++ mixed up.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; January 19th, 2010 at 06:26 PM.

  9. #9
    Join Date
    May 2009
    Posts
    2,413

    Re: How to create a pointer to 'this'?

    Quote Originally Posted by Singing Boyo View Post

    PObject** ptrtothis = &this;
    this is a pointer and a pointer has no address unless it's stored somewhere.

    You need to store the this pointer somewhere and then take the address of that storage location. That would be a pointer to a pointer.
    Last edited by nuzzle; January 19th, 2010 at 06:28 PM.

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to create a pointer to 'this'?

    Quote Originally Posted by nuzzle View Post
    this is a pointer and a pointer has no address unless it's stored somewhere.

    You need to store the this pointer somewhere and then take the address of that storage location. That would be a pointer to a pointer.
    Why? From within the context of a class, "this" is implied.

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How to create a pointer to 'this'?

    Quote Originally Posted by Arjay View Post
    Why? From within the context of a class, "this" is implied.
    I think this all started from the OP believing that "this" is an object (read the OP's posts). So he/she wants a pointer to the "this" object.

    Of course, C++ is not Java, and the whole thing goes downhill from there...

    Regards,

    Paul McKenzie

  12. #12
    Join Date
    May 2009
    Posts
    2,413

    Re: How to create a pointer to 'this'?

    Quote Originally Posted by Paul McKenzie View Post
    ???
    I have been using it for a very long time. You can't use C++ for over 15 years professionally without using it.
    You can use C++ for 30 years without ever leaving C. And most C++ programmers never make heavy use of OO, the way Java programmers do.

    the question is why would anyone need a pointer to the this pointer. The OP is obviously getting Java and C++ mixed up.
    The this pointer is just any pointer. Why would anyone need a pointer to a pointer? I don't know but it's definately not uncommon in C++ that people do.

  13. #13
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How to create a pointer to 'this'?

    Quote Originally Posted by nuzzle View Post
    You can use C++ for 30 years without ever leaving C.
    Other programmers have gone down that route, I have not.
    And most C++ programmers never make heavy use of OO, the way Java programmers do.
    The OP is using more 'C' in their code than C++. Once you get into pointer to pointer, that is approaching 'C' coding, not C++.

    Regards,

    Paul McKenzie

  14. #14
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to create a pointer to 'this'?

    Quote Originally Posted by nuzzle View Post
    You can use C++ for 30 years without ever leaving C. And most C++ programmers never make heavy use of OO, the way Java programmers do.



    The this pointer is just any pointer. Why would anyone need a pointer to a pointer? I don't know but it's definately not uncommon in C++ that people do.
    It sounds you are referring to C programmers that don't really understand C++. Often they end up with code that's more like C with classes rather than true OO code.

    I would like to see an example of tracking a pointer to "this" from within a C++ class (in the manner described above) is more useful than using "this->" directly.

  15. #15
    Join Date
    May 2009
    Posts
    2,413

    Re: How to create a pointer to 'this'?

    Quote Originally Posted by Arjay View Post
    Why? From within the context of a class, "this" is implied.
    Sure, this is implied within an object, but to be able to create a ** pointer to the object you first need to store this somewhere.

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured