-
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.
-
I think what it is saying is you cant use a const..
Code:
ModelFile& operator =(ModelFile & data)
{
return (ModelFile(data)); // copy constructor
};
-
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
-
...
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
-
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
-
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
-
By premartha in forum C++
Replies: 3
Last Post: 10-21-2005, 10:06 AM
-
Replies: 1
Last Post: 07-14-2005, 08:17 AM
-
Replies: 2
Last Post: 04-21-2005, 05:35 PM
-
Replies: 1
Last Post: 03-14-2005, 02:28 PM
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks