Overloaded operator with templated classes

Discussion in 'C++' started by sam.barker0@gmail.com, Oct 12, 2008.

  1. Guest

    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>::eek: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
     
    , Oct 12, 2008
    #1
    1. Advertising

  2. Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, Oct 12, 2008
    #2
    1. Advertising

  3. Guest

    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, 2008
    #3
  4. Guest

    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, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jacob Foshee
    Replies:
    1
    Views:
    307
    David White
    Oct 15, 2003
  2. John Goche
    Replies:
    2
    Views:
    355
    Frederick Gotham
    Sep 4, 2006
  3. Afshin
    Replies:
    0
    Views:
    298
    Afshin
    Aug 12, 2011
  4. Afshin
    Replies:
    0
    Views:
    306
    Afshin
    Aug 12, 2011
  5. Afshin
    Replies:
    7
    Views:
    448
    Victor Bazarov
    Aug 16, 2011
Loading...

Share This Page