By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,843 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

this pointer problem

P: n/a
Hi there,

I've got a class with an method that creates a temporary object of its own
class and then copies this objekt into the this pointer like

class myclass {
protected:
//...
public:
//...
void function() {
temp = myclass;
//do something with temp...
*this = temp; //this line produces no error by the compiler
//but in fact temp and this aren't the same
}
};

So could anybody tell me where to find my mistake?
Thanks for help

Thomas
Jul 19 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Thomas Baier wrote:
Hi there,

I've got a class with an method that creates a temporary object of its own
class and then copies this objekt into the this pointer like

class myclass {
protected:
//...
public:
//...
void function() {
myclass temp; //CORRECTED
//do something with temp...
*this = temp; //this line
produces no error by the compiler
//but in fact temp
and this aren't
the same
}
};

So could anybody tell me where to find my mistake?
Thanks for help

Thomas


Jul 19 '05 #2

P: n/a

"Thomas Baier" <th****@tho-bai.de> wrote in message news:3f**********************@newsread4.arcor-online.net...
temp = myclass;

This line isn't legal. I don't know what you mean by this.
*this = temp; //this line produces no error by the compiler
//but in fact temp and this aren't the same

This is not illegal though of dubious use. It invokes operator= to copy temp to *this
(or whatever else your copy-assignment operator does).


Jul 19 '05 #3

P: n/a
Ron Natalie wrote:

"Thomas Baier" <th****@tho-bai.de> wrote in message
news:3f**********************@newsread4.arcor-online.net...
> temp = myclass;

This line isn't legal. I don't know what you mean by this.
*this = temp; //this line
produces no error by the compiler
//but in fact
temp and this
aren't the same

This is not illegal though of dubious use. It invokes operator= to copy
temp to *this (or whatever else your copy-assignment operator does).

To make it more concrete: I've got a binary tree class and I've got a method
that balances the tree. This method is of type void. So I'm creating a new
temporary tree out of the members of the original tree that is balanced and
then I want to copy the temporary tree into the original tree object, but
that doesn't work.

I've got some code like

void tree::balancing()
{
tree temp;
//creating balanced tree in temp with the nodes of "this"

showTree(temp); //shows the correct balanced tree
*this = tree;

showTree(*this); //show a wrong tree with neverending loop cause one tree
is node of itself
}

So what should I do instead of this to get it working?
Jul 19 '05 #4

P: n/a
Thomas Baier wrote:

So what should I do instead of this to get it working?


Fix operator=. Obviously something is going wrong there, but
you haven't shown what your operator= does (or if you are
relying on the compiler generated one), or what the classes
internal data is.

Jul 19 '05 #5

P: n/a

"Thomas Baier" <th****@tho-bai.de> wrote in message news:3f***********************@newsread2.arcor-online.net...
showTree(temp); //shows the correct balanced tree
*this = tree;

showTree(*this); //show a wrong tree with neverending loop cause one tree
is node of itself
}

What does your operator= do ?
Jul 19 '05 #6

P: n/a
Thomas Baier wrote:


I've got a class with an method that creates a temporary object of its own
class and then copies this object into the this pointer
No it doesn't.
It copies temp into the object to which this points.
cat myclass.cc class myclass {
protected:
//...
public:
//...
void function(void) {
myclass temp; //CORRECTED
//do something with temp...
*this = temp; //this line produces no error by the compiler
//but in fact temp and this aren't the same
}
};
g++ -Wall -ansi -pedantic -O2 -S myclass.cc
So could anybody tell me where to find my mistake?


There is no mistake.

Your mistake is in the code that you didn't show to us.
You cannot compare temp which is type myclass
to this which is type myclass*.
You will find that

temp == *this

instead.

Jul 19 '05 #7

P: n/a
What kind of mistake should that be? Cause the temp object is correct. I can
show it and there's no mistake in it. The =operateor calls my copy
construktor:

template <typename T> binBaum <T>::binBaum(const binBaum& cpy)
{
cout << "\ncopying\n"; //To see when it is called
key = cpy.key;
content = cpy.content;
parent = cpy.parent;
left = cpy.left;
right = cpy.right;
level = cpy.level;

}

As you see I'm able to see that the =operator calls my copy constructor. So
every part of the objects should be the same, but it is not.
So I'm posting the full unshorted code to you (I did not translate the
comments (German), but I hope you'll see what I want to do).
Jul 19 '05 #8

P: n/a
The method with the error is
template <typename T> binBaum<T>& binBaum <T>::ausgleichen();
"ausgleichen" means balance.
Thomas Baier wrote:
What kind of mistake should that be? Cause the temp object is correct. I
can show it and there's no mistake in it. The =operateor calls my copy
construktor:

template <typename T> binBaum <T>::binBaum(const binBaum& cpy)
{
cout << "\ncopying\n"; //To see when it
is called key = cpy.key;
content = cpy.content;
parent = cpy.parent;
left = cpy.left;
right = cpy.right;
level = cpy.level;

}

As you see I'm able to see that the =operator calls my copy constructor.
So every part of the objects should be the same, but it is not.
So I'm posting the full unshorted code to you (I did not translate the
comments (German), but I hope you'll see what I want to do).


Jul 19 '05 #9

P: n/a
Unfortunately I've posted a version of the method "ausgleichen" where I have
comment the important part out. So the method must be:
template <typename T> void binBaum <T>::ausgleichen()
{
List<int> list = traversieren(0); //Traversieren
list.isort(); //Sortieren
list = buildbinsuchbaum(list,0,list.getLength()); //liste in preorder
erstellen

binBaum<T> temp(list[0]); //fst element as root
for(int i=1; i<list.getLength(); i++) {
temp.Add(new binBaum(list[i])); //append the knodes
}
*this = temp; //calling the cpy constructor
}
Jul 19 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.