Hi,
If I have code like this :
class MyClass {
std::vector< int> arr_ ;
std::vector < std::pair < long, double> > adj ;
......
MyClass( const MyClass& rhs) {
......
this->arr_ = rhs.arr_ ; //will the elements get copied accross
//into this->arr ?
this->adj = rhs.adj ; //more complicated vector, do elements
//(pairs) get copiedinto this->adj ?
.....
}
MyClass& operator=( const MyClass& rhs) {
if( this != rhs ) { // <- compiler barfs here
// how can I check to make sure !(same object)?
.......
}
} 9 1318
Alfonzo Morra wrote: Hi,
If I have code like this :
class MyClass { std::vector< int> arr_ ; std::vector < std::pair < long, double> > adj ; ...... MyClass( const MyClass& rhs) { ...... this->arr_ = rhs.arr_ ; //will the elements get copied accross //into this->arr ? this->adj = rhs.adj ; //more complicated vector, do elements //(pairs) get copiedinto this->adj ? ..... }
MyClass& operator=( const MyClass& rhs) { if( this != rhs ) { // <- compiler barfs here // how can I check to make sure !(same object)? ....... } }
Ok, I realized (in the case of the copy assignment constructor), that I
have to pass the address of the reference (i.e. &rhs). I find this
slightly baffling as I thought a reference is synonymous with a pointer.
In which case, &(rhs) will be returning the adress of the pointer itself
and not the address that the pointer was pointing to. Further
elucidation please ...
Dear Alfonzo,
On Mon, 2005-07-11 at 09:45 +0000, Alfonzo Morra wrote: Ok, I realized (in the case of the copy assignment constructor), that I have to pass the address of the reference (i.e. &rhs). I find this slightly baffling as I thought a reference is synonymous with a pointer. In which case, &(rhs) will be returning the adress of the pointer itself and not the address that the pointer was pointing to. Further elucidation please ...
Why not pass the reference and take the address inside the function?
Much more readable and much more usual. A point of the new notation
"rhs" (without &) in case of references to obtain the object instead of
the address is that it makes operator overloading readable. Imaginge
that you would have a complex number class and you had to take the
address when passing comlex numers. That would be &c1+&c2. Ugh.
Best wishes,
Chris
Alfonzo Morra wrote: Hi,
If I have code like this :
class MyClass { std::vector< int> arr_ ; std::vector < std::pair < long, double> > adj ; ...... MyClass( const MyClass& rhs) { ...... this->arr_ = rhs.arr_ ; //will the elements get copied accross //into this->arr ?
Yes. A vector assigns everything thats in it.
this->adj = rhs.adj ; //more complicated vector, do elements //(pairs) get copiedinto this->adj ? .....
Same here.
--
Karl Heinz Buchegger kb******@gascad.at
Alfonzo Morra wrote: Ok, I realized (in the case of the copy assignment constructor), that I have to pass the address of the reference (i.e. &rhs). I find this slightly baffling as I thought a reference is synonymous with a pointer.
And you found out, that is it not.
A reference is another name for an existing object. Nothing more, nothing less.
In some cases a reference is internally implemented by the compiler by using
a pointer under the hood. But a reference *is not* a pointer.
--
Karl Heinz Buchegger kb******@gascad.at
Alfonzo Morra wrote: Hi,
If I have code like this :
class MyClass { std::vector< int> arr_ ; std::vector < std::pair < long, double> > adj ; ...... MyClass( const MyClass& rhs) { ...... this->arr_ = rhs.arr_ ; //will the elements get copied accross //into this->arr ? this->adj = rhs.adj ; //more complicated vector, do elements //(pairs) get copiedinto this->adj ? ..... }
MyClass& operator=( const MyClass& rhs) { if( this != rhs ) { // <- compiler barfs here // how can I check to make sure !(same object)? ....... } }
Thanks Karl
Alfonzo Morra schreef: Hi,
If I have code like this :
MyClass& operator=( const MyClass& rhs) { if( this != rhs ) { // <- compiler barfs here // how can I check to make sure !(same object)? ....... } }
The quick solution is &rhs, but that's probably now what you want.
The problem is that you still need to make the ..... part
exception-safe,
and if you do, you don't need the this==&rhs check.
E.g. one solution is the canonical assignment:
MyClass& operator=( const MyClass& rhs) {
MyClass tmp (rhs); // copy
this->swap(tmp); // now *this==rhs
return *this; // tmp.~MyClass destorys old value
}
Now, if you happen to have a self-assignment, it simply swaps two
identical values.
HTH,
Michiel Salters
msalters wrote: Alfonzo Morra schreef:
Hi,
If I have code like this :
MyClass& operator=( const MyClass& rhs) { if( this != rhs ) { // <- compiler barfs here // how can I check to make sure !(same object)? ....... } }
The quick solution is &rhs, but that's probably now what you want. The problem is that you still need to make the ..... part exception-safe, and if you do, you don't need the this==&rhs check.
E.g. one solution is the canonical assignment: MyClass& operator=( const MyClass& rhs) { MyClass tmp (rhs); // copy this->swap(tmp); // now *this==rhs return *this; // tmp.~MyClass destorys old value } Now, if you happen to have a self-assignment, it simply swaps two identical values.
HTH, Michiel Salters
Hi Michiel,
This looks more like what I'm trying to do. Could you please give me an
example of the implementation of the swap() function ?
Thanks
Alfonzo Morra wrote:
This looks more like what I'm trying to do. Could you please give me an example of the implementation of the swap() function ?
Aehm. Swap the content of 2 variables.
Shouldn't be a big problem for any decent programmer.
void swap( int& a, int& b )
{
int tmp( a );
a = b;
b = tmp;
}
This is so straight forward, that the STL contains a template for it.
(Now modify the above, that it becomes a member function and you have it)
Seriously: You should get some literature. You won't get very far in
your C++ studies without one. 'Try and error' simply doesn't work in a
language as complex as C++.
--
Karl Heinz Buchegger kb******@gascad.at
Karl Heinz Buchegger wrote: Alfonzo Morra wrote:
This looks more like what I'm trying to do. Could you please give me an example of the implementation of the swap() function ?
Aehm. Swap the content of 2 variables. Shouldn't be a big problem for any decent programmer.
void swap( int& a, int& b ) { int tmp( a ); a = b; b = tmp; }
This is so straight forward, that the STL contains a template for it. (Now modify the above, that it becomes a member function and you have it)
Seriously: You should get some literature. You won't get very far in your C++ studies without one. 'Try and error' simply doesn't work in a language as complex as C++.
LOL. I *know* how to swap variables, I just wanted to make sure I was
not jumping to conclusions before "rolling my own". This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: zhou |
last post by:
Hi there,
I am expecting that the following assignment will invoke assignment operator on vector (since myFunc() returns a const
vector & type), which makes a copy of the vector returned from...
|
by: Christof Krueger |
last post by:
Hello,
I'm quite new to C++ and have some base C knowledge. So I know how to
solve a given problem, by I sometimes tend to fall back to C. That is
what I want to avoid in my current project....
|
by: Kitty |
last post by:
Hello.
Given vector<Object>, I would like to find the position of an element "obj"
in this container. What function should I use? Thanks.
|
by: Alfonzo Morra |
last post by:
Hi,
If I have code like this :
class MyClass {
std::vector< in t> arr_ ;
std::vector < std::pair< long, double> > adj ;
......
MyClass( const MyClass& rhs) {
......
|
by: Alfonso Morra |
last post by:
I have the ff code for testing the concept of storing objects:
#include <vector>
#include <iostream>
using namespace std ;
class MyClass {
public:
MyClass(){
|
by: Raghuram N K |
last post by:
Hi,
Following program compiles and executes successfully in windows with
DevCPP compiler. When I compile the same in Linux with 'g++323' compiler
I get following assignment error:
cannot...
|
by: call_me_anything |
last post by:
why is the following not allowed :
vector <Base *vec_of_base;
vector <Derived *vec_of_derived;
vec_of_base = vec_of_derived;
Note : The following is allowed :
|
by: Grey Alien |
last post by:
If I have the ff struct:
struct A
{
unsigned int i;
char s;
} a, b;
And use them in code like this:
|
by: arnuld |
last post by:
It is quite an ugly hack but it is all I am able to come up with for
now :-( and it does the requires work. I want to improve the program, I
know you people have much better ideas ;-)
/* C++...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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,...
| |