-
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.
-
Re: How to create a pointer to 'this'?
Quote:
Originally Posted by
Paul McKenzie
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++.
Well, the OP is a C++ newbie and cannot be accused of being too much or too little C-isch, can he?
He's making heavy use of the this pointer though. If I understood you correctly you had a problem with that, didn't you?
-
Re: How to create a pointer to 'this'?
Quote:
Originally Posted by
Paul McKenzie
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++.
You're inconsistent. First you accused the OP of using C++ like Java, and now you're accusing him of using C++ like C.
Make your mind up.
-
Re: How to create a pointer to 'this'?
Quote:
Originally Posted by
nuzzle
You're inconsistent. First you accused the OP of using C++ like Java
Quote:
Hopefully, you are not trying to write C++ in the style of Java
There is no accusation there.
Quote:
, and now you're accusing him of using C++ like C.
I will wait for the OP to respond. Thank you very much.
Regards,
Paul McKenzie
-
Re: How to create a pointer to 'this'?
Quote:
Originally Posted by
Paul McKenzie
I will wait for the OP to respond.
Why on earth would you do that?
Everything you've said in this thread is unrelated to anything the OP said.
-
Re: How to create a pointer to 'this'?
Quote:
Originally Posted by
nuzzle
Why on earth would you do that?
Because the OP started the thread. Why are you being belligerent for no apparent reason? Calm down.
Quote:
Everything you've said in this thread is unrelated to anything the OP said.
OK, whatever you say...
Regards,
Paul McKenzie
-
Re: How to create a pointer to 'this'?
Quote:
Originally Posted by
nuzzle
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.
I still don't see where you need to have a ** pointer to this. If internally, it's implied. If external, I get the pointer when I new up the object. If I'm missing something, please illustrate with a code snippet.
-
Re: How to create a pointer to 'this'?
Quote:
Originally Posted by
Arjay
I still don't see where you need to have a ** pointer to this. If internally, it's implied. If external, I get the pointer when I new up the object. If I'm missing something, please illustrate with a code snippet.
I'll illustrate with a code snippet,
void main() {}
-
Re: How to create a pointer to 'this'?
Or with this little poem,
I strongly believe there is,
an implicit pointer called this.
There must be something this is pointing at,
I pray it is like that.
-
Re: How to create a pointer to 'this'?
Quote:
Originally Posted by
nuzzle
I'll illustrate with a code snippet,
void main() {}
I was hoping for a serious example.
-
Re: How to create a pointer to 'this'?
Quote:
Originally Posted by
Arjay
I was hoping for a serious example.
Well, not from me.
For the first time in my life I'me going to quit a forum without being banned.
Thank you all and good luck.
-
Re: How to create a pointer to 'this'?
Quote:
Originally Posted by
nuzzle
Well, not from me.
For the first time in my life I'me going to quit a forum without being banned.
Thank you all and good luck.
nuzzle,
you've been here far, far less than me,
but notice how you already have 2 green light reputation and I don't.
that means, you've done something right during your short stay here at the CG,
and that those people were helped by you.
The debate on this thread got little hot, no doubt about it.
But it is in this flame that all of us are forged,
for the reasons too hard to understand and too painful to accept,
to take the next hammering of the blacksmith.
So for the sake of people who might need your help in days to come,
I hope you stick around.
-
Re: How to create a pointer to 'this'?
Indeed! The impersonal nature of web-based debate can result in very "thick skin" when compared with face-to-face discussions. Don't take it personally.
-
Re: How to create a pointer to 'this'?
I seriously would like a code sample that demostrates the logic of having a pointer that points to the temporary r-value pointer of "this" :S. Of course the op doesn't need to do such a thing. he thinks the "this" is an object and not a pointer to the object itself. If anyone knows c++, they know how "this" is typically implemented. They know that it gets pushed on the stack as a parameter for the member function being called.
like
if( Kid.isChild() ) cout << "Yes" << endl;
would be
if( isChild( &Kid ) cout << "Yes" << endl;
that is psuedo code btw
That code makes sense why you shouldn't copy the address of where the pointers is located( &this ) since it is push on the stack and has limited life scope. It will no longer be valid after the member function is called. This is what Paul meant, i believe, when saying why would you need to do this?? It is just not logical
The op clearly thought that "this" was an object and not a pointer to the object itself :)
Again I seriously would like a code sample by Nuzzle that demostrates the logic of having a pointer that points to the temporary r-value pointer of "this" :S
Code:
List<PObject*>* tlist = postInits->data2For(type);
PObject** ptrtothis = &this;//non-lvalue in unary '&'
tlist->add(ptrtothis);
should be
Code:
List<PObject*>* tlist = postInits->data2For(type);
tlist->add( this )
also
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;
}
is redundant
Code:
TEMPLT void List<t>::add(const t* obj, int len){//len is 1 by default
if(totalSize-objectCount < len) increaseSize(len);
for(int i=0; i<len; i++)
contents[i+objectCount] = obj[i];
objectCount+=len;
}
<= is the opposite of >, but I used < because it made more sense....
the original code said totalSize-objectCount > len.., but let's say
(10 - 5 > 5) = (5 > 5) == false. if false, increaseSize( 5 ), but i think it should say
(10 - 5 >= 5) = (5 >= 5) == true. if false, increaseSize( 5 ). I think this is right if the totalSize is the total amount in which the list is allocated to hold?
Maybe I am wrong, but I don't see the full implementation either :)
Also Nuzzle, please relax some. No one here is going to flame you unless you're "very!" incorrect or touch a soft topic :D
Also feel free to correct me if I am wrong, but be sure you understand what I meant :D