RE: [Dev-C++] Copy Constructors and Overloaded Assignment Operators
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ioannis V. <no...@ho...> - 2002-07-20 20:44:39
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of Matthew Hickson
> Sent: Saturday, July 20, 2002 11:12 PM
> To: Dev...@li...
> Subject: [Dev-C++] Copy Constructors and Overloaded
> Assignment Operators
>
>
> Hello guys,
>
> Just a quick question...
>
> I'm having a rough time getting some copy constructors and overloaded
> assignment operators to work. I've had a look through my stuff (Ivor
> Horton - Wrox - Beginning C++; Steve Oualline - O'Reilly -
> Practical C++
> Programming), but to no avail.
>
> I feel like I'm asking how to unsubscribe here, so please be
> gentle... 8-)
> (By the way, can't an Admin just set up a list filter to
> clobber these
> guys... )
>
> Here's the error... (others are similar)
>
> "passing 'const Test' as 'this' argument of 'int Test::getData()'
> discards qualifiers
>
> [code begins]
> // Copy Constructor
> Test::Test(const Test& TestCopy){
> data = TestCopy.getData();
> data2 = TestCopy.getData2();
> }
>
> // Operator =
> Test& Test::operator=(const Test& Test2){
> data = Test2.getData();
> data2 = Test2.getData2();
>
> return *this;
> }
> [code ends]
>
> I've tried using my set functions (what I want to do anyhow...),
>
> i.e. this.setData(Test2.getData()); // this omitted too...
>
> I've also tried...
>
> data = Test2->getData();
>
> I'm going to keep looking, but a quick pointer to a good discussion
> on this would be great (or a good discussion).
>
> Any help would be greatly appreciated.
Assuming your data is an int x; define getData() as:
int getData() const { return x; }
Notice that const above.
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.cjb.net
* Alternative URL: http://run.to/noicys
|