CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 17 of 17
  1. #16
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: operator overloading

    Quote Originally Posted by laserlight View Post
    What is operator+ supposed to do? Why work with a temporary char* when you can work with a temporary FunnyString that is returned? Have you considered implementing operator+=, then possibly implementing operator+ by using operator+=?

    EDIT:

    Sounds possible, but I cannot find where dakotabk made the mistake of setting the data member variable to anything other than the result of new[] or a null pointer constant.
    He was going to change it to point to an empty string.

    As for operator+, it is to allow efficient implementation of this that the standard does not require that it internally holds a contiguous buffer. Effectively you can have a variation of a string that has two or more pointers concatenated together (A rope, I think they call it).

    It is relatively easy to implement a string that works. It is extremely difficult to implement one that works well in all (or at least the most common) situations.
    Last edited by NMTop40; May 20th, 2009 at 10:23 AM.

  2. #17
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: operator overloading

    Quote Originally Posted by dakotabk View Post
    The problem is, when an object contains pointer and destructor also ( destruct this pointer ), you can not return this object , don't you?
    That's what the copy constructor is for. In this code:

    Code:
    MyClass dosomething(const MyClass &mc1, const MyClass &mc2)
    {
        MyClass result;
        // stuff
        return result;
    }
    Then indeed, result's destructor will be called at some point; but only *after* the copy constructor is used to take its relevant values to whatever is waiting outside the function to receive them.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured