assignment operator


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: assignment operator

  1. #1
    Join Date
    Oct 2005
    Posts
    173

    assignment operator

    Danny,
    I think in some posts back, you mentioned that operators free standing are much better than as member functions of a class.

    So, what about the assignment operator?

    I used this as a starting point and modified it as a free standing operator
    http://www.informit.com/guides/conte...plus&seqNum=16

    Code:
    ModelFile& operator=(const ModelFile & data)
    {
        return (ModelFile(data));   // copy constructor
    };
    HOwever, I get "error C2801: 'operator =' must be a non-static member"
    is there something different about assignment operator?

    So once I put it in the class, then I get another warning "warning C4172: returning address of local variable or temporary"

    is this not the right way to write an assingment operator?
    maybe something like this?
    Code:
    operator=(const ModelFile & data, ModelFile & dataout )
    {
        ... 
    };

    Thanks.
    Last edited by rssmps; 12-16-2005 at 07:59 PM.

  2. #2
    Join Date
    Aug 2005
    Location
    Melbourne...Australia
    Posts
    279
    I think what it is saying is you cant use a const..
    Code:
    ModelFile& operator =(ModelFile & data)
    {
        return (ModelFile(data));   // copy constructor
    };

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    I was referring to the relational operators, <, >, == etc., not to the assignment operator. The assignment opereator is a special case: it has to be declared as a member function, never as a free-standing function. This is why your compiler is complaining.
    Danny Kalev

  4. #4
    Join Date
    Sep 2005
    Location
    istanbul / Turkey
    Posts
    133
    ...
    return (ModelFile(data)); // copy constructor
    ...
    "warning C4172: returning address of local variable or temporary"
    ...
    you produce a stack object which will die after return.
    this is a dangling reference

  5. #5
    Join Date
    Nov 2003
    Posts
    4,118
    First, you need to make this operator a member function. Next, make sure that it returns a reference to *this. This is the canonical implementation of this operator. Finally, make sure that you really need to define this operator. If your class doesn't contain raw pointers and handles, it's pretty likely that you don't need to define this operator at all.
    Danny Kalev

  6. #6
    Join Date
    Oct 2005
    Posts
    173
    I didn't need it in the first version of my code. (using list as the container)

    However, I think speedwise, vector will be a bit better for overall general use so I switched to it. When I did that, the compiler complains and points me to the xutility file.

    Code:
    // TEMPLATE FUNCTION fill
    template<class _FwdIt, class _Ty> inline void fill(_FwdIt _First, _FwdIt _Last, const _Ty& _Val)
    {   // copy _Val through [_First, _Last)
        for (; _First != _Last; ++_First)
        *_First = _Val;
    }
    c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\xutility(1136) : error C2582: 'operator =' function is unavailable in 'ModelFile'

    c:\program Files\Microsoft Visual Studio .NET 2003\Vc7\include\vector(862) : see reference to function template instantiation 'void std::fill<std::vector<_Ty>::_Tptr,_Ty>(_FwdIt,_FwdIt,const _Ty &)' being compiled
    with
    [
    _Ty=ModelFile,
    _FwdIt=std::vector<ModelFile>::_Tptr
    ]
    c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\vector(809) : while compiling class-template member function 'void std::vector<_Ty>::_Insert_n(std::vector<_Ty>::iterator,std::vector<_Ty>::size_type,const _Ty &)'
    with
    [
    _Ty=ModelFile
    ]
    c:\Documents and Settings\ifemsa.cpp(45) : see reference to class template instantiation 'std::vector<_Ty>' being compiled
    with
    [
    _Ty=ModelFile
    ]
    -----------------------------
    as for the dangling pointer, that's becuase I was confused about it being a member function rather than stand alone. Now it makes sense why I got that error.

Similar Threads

  1. Replies: 3
    Last Post: 10-21-2005, 10:06 AM
  2. Replies: 1
    Last Post: 07-14-2005, 08:17 AM
  3. subtraction operator overload
    By arnie6 in forum C++
    Replies: 2
    Last Post: 04-21-2005, 05:35 PM
  4. Operator Overloading
    By sivsu in forum C++
    Replies: 1
    Last Post: 03-14-2005, 02:28 PM
  5. LIKE operator, stored procedure and vb code problem
    By John Wood in forum VB Classic
    Replies: 3
    Last Post: 03-26-2002, 08:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center