473,387 Members | 2,436 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.

Question on vector assignment and object equality determination

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)?
.......
}
}

Jul 23 '05 #1
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 ...

Jul 23 '05 #2
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

Jul 23 '05 #3
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
Jul 23 '05 #4
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
Jul 23 '05 #5


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

Jul 23 '05 #6


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

Jul 23 '05 #7


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

Jul 23 '05 #8
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
Jul 23 '05 #9


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

Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
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...
10
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....
10
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.
1
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) { ......
12
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(){
1
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...
16
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 :
24
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:
10
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++...
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: 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$) { } ...
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
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:
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...
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.