Overloaded operator with templated classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sam.barker0@gmail.com

    #1

    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<charast ring=another_ob ject;

    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<typena me T>
    myclass<T>& myclass<T>::ope rator=(const myclass<T>& rhs)
    {

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

    //allocate again
    this->var1=rhs.var 1;
    this->head=rhs.hea d;
    //call another method
    recursively_cop y(head,rhs.head );

    }
    return *this;

    }


    Why is this happening
    Cheers,
    Sam
  • Kai-Uwe Bux

    #2
    Re: Overloaded operator with templated classes

    sam.barker0@gma il.com wrote:
    Hi,
    >
    I have designed a class with an overloaded = operator.
    >
    The problem is that
    >
    whenever I invoke the method
    like
    >
    const myclass<charast ring=another_ob ject;
    >
    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

    Comment

    • sam.barker0@gmail.com

      #3
      Re: Overloaded operator with templated classes

      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

      Comment

      • sam.barker0@gmail.com

        #4
        Re: Overloaded operator with templated classes

        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

        Comment

        Working...