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
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?
Re: How to create a pointer to 'this'?
Quote:
Originally Posted by
Singing Boyo
Hi all,
I'm wondering how to create a pointer to 'this'.
Quote:
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
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 :D
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);
};
Re: How to create a pointer to 'this'?
Quote:
Originally Posted by
Singing Boyo
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.
Quote:
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
Re: How to create a pointer to 'this'?
Quote:
Originally Posted by
Singing Boyo
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 :D
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?
Re: How to create a pointer to 'this'?
Quote:
Originally Posted by
Paul McKenzie
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.
Re: How to create a pointer to 'this'?
Quote:
Originally Posted by
nuzzle
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.
Quote:
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
Re: How to create a pointer to 'this'?
Quote:
Originally Posted by
Singing Boyo
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.
Re: How to create a pointer to 'this'?
Quote:
Originally Posted by
nuzzle
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.
Re: How to create a pointer to 'this'?
Quote:
Originally Posted by
Arjay
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
Re: How to create a pointer to 'this'?
Quote:
Originally Posted by
Paul McKenzie
???
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.
Quote:
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.
Re: How to create a pointer to 'this'?
Quote:
Originally Posted by
nuzzle
You can use C++ for 30 years without ever leaving C.
Other programmers have gone down that route, I have not.
Quote:
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
Re: How to create a pointer to 'this'?
Quote:
Originally Posted by
nuzzle
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.
Re: How to create a pointer to 'this'?
Quote:
Originally Posted by
Arjay
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.