473,320 Members | 1,713 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,320 software developers and data experts.

Overloaded operator with templated classes

Hi,

I have designed a class with an overloaded = operator.

The problem is that

whenever I invoke the method
like

const myclass<charastring=another_object;

my overloaded gets invoked and everything is fine.

but if I write

const myclass<char>& astring=another_object;

I guess the synthesised overload function takes place instead of my
overloaded function.

astring is a shallow copy of another_object.
My overloaded method is defined as

template<typename T>
myclass<T>& myclass<T>::operator=(const myclass<T>& rhs)
{

if(this != &rhs)
{
//free the allocated memory

//allocate again
this->var1=rhs.var1;
this->head=rhs.head;
//call another method
recursively_copy(head,rhs.head);

}
return *this;

}
Why is this happening
Cheers,
Sam
Oct 12 '08 #1
3 1495
sa*********@gmail.com wrote:
Hi,

I have designed a class with an overloaded = operator.

The problem is that

whenever I invoke the method
like

const myclass<charastring=another_object;

my overloaded gets invoked and everything is fine.
That should not invoke the assignment operator but a constructor. What you
do is copy-initialization and unrelated to assignment.

but if I write

const myclass<char>& astring=another_object;
That initializes a reference. The rules are in [8.5.3]. What matters here is
that there is no requirement that a copy of the object is created.

I guess the synthesised overload function takes place instead of my
overloaded function.
No, that does not happen.
astring is a shallow copy of another_object.
No, but it might look that way. Very likely, it _is_ your object.
[snip]

BTW: is there any reason not to use std::string as your string class (just
guessing from the names)?
Best

Kai-Uwe Bux
Oct 12 '08 #2
Thanks Kai.It makes sense.That line should do copy initialisation.
>>const myclass<char>& astring=another_object;
>That initializes a reference. The rules are in [8.5.3]. What matters here is
that there is no requirement that a copy of the object is created.

Yep what you say is correct.There is no requirement for a copy of the
object.

Cheers,
Jegan

Oct 12 '08 #3
Thanks Kai.It makes sense.That line should do copy initialisation.
>>const myclass<char>& astring=another_object;
That initializes a reference. The rules are in [8.5.3]. What matters here is
that there is no requirement that a copy of the object is created.
Yep what you say is correct.There is no requirement for a copy of the
object.

Cheers,
Sam
Oct 12 '08 #4

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

Similar topics

4
by: franky.backeljauw | last post by:
Hello, I have a problem with using a copy constructor to convert an object of a templated class to object of another templated class. Let me first include the code (my question is below): ...
2
by: Tony Johansson | last post by:
Hello Experts!! I have two small classes called Intvektor and Matris shown at the bottom and a main. Class Intvektor will create a one dimension array of integer by allocate memory dynamically...
1
by: Alex Zhitlenok | last post by:
Hi, My question is how to resolve in C# ambiguous overloaded operators? Let say, I have two unrelated classes A and B, each one implements overloaded operator + with the first parameter of type...
3
by: cybertof | last post by:
Hello, Is it possible in C# to have 2 overloaded functions with - same names - same parameters - different return type values If no, is it possible in another language ?
4
by: gobis | last post by:
Hello everyone, After I could not overload the << operator as a friend operator in my template class (yes, it did not have to be a template class, but I was testing something for the class I was...
4
by: Amadeus W. M. | last post by:
What is the difference between friend ostream & operator<<(ostream & OUT, const Foo & f){ // output f return OUT; } and template <class X>
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
5
by: Fokko Beekhof | last post by:
Hello all, please consider the following code: -------------------------------------------------- #include <tr1/memory> struct BaseA { int x;
3
by: jr.freester | last post by:
I have created to classes Matrix and System. System is made up of type matrix. ---------------------------------------------------------------------------------- class Matrix { private: int...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.