Operator overloading question

This is a discussion on Operator overloading question within the C++ Programming forums, part of the General Programming Boards category; In one of my classes i have an '=' operator that looks like this: Code: CBuffer& CBuffer::operator=(const CBuffer& pBuffer); When ...

  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
    21,644
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,644
    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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21