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.
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.
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);
};
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 12:23 PM.
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.
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?
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 05:26 PM.
???
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.
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.
Bookmarks