473,387 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

this pointer problem

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
9 2347
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

"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
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
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

"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bryan Parkoff | last post by:
I create one 32 Bits variable and four pointer variables. Four pointer variables link or point to one 32 Bits variable. Each pointer variable is 8 Bits. Look at my example below. unsigned int...
4
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived...
5
by: tthunder | last post by:
Hi @all, Perhaps some of you know my problem, but I had to start a new thread. The old one started to become very very confusing. Here clean code (which compiles well with my BCB 6.0 compiler)....
6
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a...
42
by: baumann | last post by:
hi all, typedef int (*pfunc)(int , int); pfunc a_func; i know it's ok, but how can define a_func without typedef statement? thanks .
33
by: dough | last post by:
Is it possible in C to declare and initialize a pointer that points to itself? Why or why not?
51
by: Joe Van Dyk | last post by:
When you delete a pointer, you should set it to NULL, right? Joe
30
by: Jess | last post by:
Hello, I tried a program as follows: include<iostream> using namespace std; class A{ public:
7
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
19
by: Eric | last post by:
What is this? ( char * ) &( ( struct aStruct * ) 0 ) It looks like it's taking the address of something that contains a pointer to null, and casting it to a pointer to char. (Actually,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.