this pointer problem

T

Thomas Baier

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
 
T

Thomas Baier

Thomas said:
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
 
T

Thomas Baier

Ron said:
This line isn't legal. I don't know what you mean by this.

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?
 
R

Ron Natalie

Thomas Baier said:
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).
 
L

lilburne

Thomas said:
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.
 
R

Ron Natalie

Thomas Baier said:
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 ?
 
E

E. Robert Tisdale

Thomas said:
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.
 
T

Thomas Baier

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).
 
T

Thomas Baier

The method with the error is
template <typename T> binBaum<T>& binBaum <T>::ausgleichen();
"ausgleichen" means balance.
 
T

Thomas Baier

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)); //append the knodes
}
*this = temp; //calling the cpy constructor


}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top