Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Overloaded operator with templated classes

Reply
Thread Tools

Overloaded operator with templated classes

 
 
sam.barker0@gmail.com
Guest
Posts: n/a
 
      10-12-2008
Hi,

I have designed a class with an overloaded = operator.

The problem is that

whenever I invoke the method
like

const myclass<char> astring=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>:perator=(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
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      10-12-2008
wrote:

> Hi,
>
> I have designed a class with an overloaded = operator.
>
> The problem is that
>
> whenever I invoke the method
> like
>
> const myclass<char> astring=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
 
Reply With Quote
 
 
 
 
sam.barker0@gmail.com
Guest
Posts: n/a
 
      10-12-2008
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

 
Reply With Quote
 
sam.barker0@gmail.com
Guest
Posts: n/a
 
      10-12-2008
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in calling an overloaded operator= from inside anotheroverloaded operator= Afshin C++ 7 08-16-2011 12:46 PM
Problem in calling an overloaded operator= from inside anotheroverloaded operator= in C++ Afshin C++ 0 08-12-2011 12:19 PM
Problem in calling an overloaded operator= from inside anotheroverloaded operator Afshin C++ 0 08-12-2011 12:01 PM
"overloaded cast operator" and "operator const" John Goche C++ 2 09-04-2006 02:48 PM
VS03: overloaded non-member operator(<<) for templated class Jacob Foshee C++ 1 10-15-2003 12:21 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57