468,249 Members | 1,580 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,249 developers. It's quick & easy.

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 1233
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by franky.backeljauw | last post: by
2 posts views Thread by Tony Johansson | last post: by
1 post views Thread by Alex Zhitlenok | last post: by
3 posts views Thread by cybertof | last post: by
4 posts views Thread by Amadeus W. M. | last post: by
3 posts views Thread by jr.freester | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.