472,993 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,993 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 1476
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.