Thread: Operator overloading question

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217

    Operator overloading question

    In one of my classes i have an '=' operator that looks like this:
    Code:
    CBuffer& CBuffer::operator=(const CBuffer& pBuffer);
    When i use this in the below code it treats it as a reference, instead of calling the operator"
    Code:
    void someFunc(CBuffer& pBuff)
    {
      CBuffer something = pBuff; //treats it as a reference, rather than calling the overloaded operator.
    }
    I have to do it like this for it to call the operator:
    Code:
    void someFunc(CBuffer& pBuff)
    {
      CBuffer something;
      something = pBuff;
    }
    Is there anyway i can make it work how i got it in the first example?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Code:
    CBuffer something = pBuff;
    The above is actually an invocation of the copy constructor. In fact, in this case it looks like you do not need to use the copy assignment operator. You could consider passing the argument by const reference instead of reference.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Quote Originally Posted by laserlight View Post
    Code:
    CBuffer something = pBuff;
    The above is actually an invocation of the copy constructor. In fact, in this case it looks like you do not need to use the copy assignment operator. You could consider passing the argument by const reference instead of reference.
    Code:
        CBuffer(const CBuffer& pBuffer)
        {
            *this = pBuffer;
        }
    Did that and it works properly now

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Did that and it works properly now
    It is more common to do it the other way round, i.e., write the copy assignment operator in terms of the copy constructor and a swap() member function. For example:
    Code:
    CBuffer::CBuffer(const CBuffer& pBuffer)
    {
        // Copy pBuffer to this new object.
    }
    
    CBuffer& CBuffer::operator=(const CBuffer& pBuffer)
    {
        if (this != &pBuffer)
        {
            CBuffer temp(pBuffer);
            swap(temp);
        }
        return *this;
    }
    Note to self: post #2000
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM