|
|
Object Assignment
Example #1 (see code below)
Here is a simple example which assigns one object to another object of
the same type. This example shows that the data members in the derived
class as well as the data members in the base class are copied in one
simple assignment statement.
Example #2 (see code below)
If the base class is replaced with fstream I reasoned that the
assignment would again copy the data members of the derived class as
well as the data members of fstream. However I got the following error
messages:
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
'ios::operator =(const ios &)' is not accessible in function
derived::operator =(derived &)
Compiler could not generate operator= for class 'istream' in function
iostream::operator =(iostream &)
Compiler could not generate operator= for class 'ostream' in function
iostream::operator =(iostream &)
Why am I getting these messages and how can I fix it ?
/********** Example #1 **********/
#include<iostream>
using namespace std;
class base
{
int basei;
public:
base() {}
int get() { return basei; }
void set(int i) { basei = i; }
};
class derived : public base
{
int derivedi;
public:
derived() {}
get() { return derivedi; }
void set(int i) { derivedi = i; }
};
int main()
{
derived srce, dest;
srce.base::set(99);
srce.set(55);
dest = srce;
cout << srce.base::get() << " " << srce.get() << endl; // output
is 99 55
// prove the assignment works!
cout << dest.base::get() << " " << dest.get() << endl; // output
is 99 55
return 0;
}
/********** Example #2 **********/
#include<fstream>
using namespace std;
class derived : public fstream
{
int derivedi;
public:
derived() {}
get() { return derivedi; }
void set(int i) { derivedi = i; }
};
int main()
{
derived srce, dest;
srce.set(55);
dest = srce;
return 0;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
stevenhr
|
7/26/2004 10:48:42 PM |
|
"Ron Stevenhaagen" <stevenhr@kgh.kari.net> wrote...
> Example #1 (see code below)
>
> Here is a simple example which assigns one object to another object of
> the same type. This example shows that the data members in the derived
> class as well as the data members in the base class are copied in one
> simple assignment statement.
>
> Example #2 (see code below)
>
> If the base class is replaced with fstream I reasoned that the
> assignment would again copy the data members of the derived class as
> well as the data members of fstream. However I got the following error
> messages:
>
> Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
>
> 'ios::operator =(const ios &)' is not accessible in function
> derived::operator =(derived &)
> Compiler could not generate operator= for class 'istream' in function
> iostream::operator =(iostream &)
> Compiler could not generate operator= for class 'ostream' in function
> iostream::operator =(iostream &)
>
> Why am I getting these messages and how can I fix it ?
>
You can't fix it. fstream objects are not supposed to be copied either
by construction or by assignment. It's prohibited by the Standard.
fstream classes also are not designed to be derived from. If you need
some kind of special treatment of the stream, you should derive from
streambuf and put your functionality there.
Victor
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Victor
|
7/27/2004 10:50:37 AM
|
|
Ron Stevenhaagen wrote:
> Example #1 (see code below)
>
> Here is a simple example which assigns one object to another object of
> the same type. This example shows that the data members in the derived
> class as well as the data members in the base class are copied in one
> simple assignment statement.
>
> Example #2 (see code below)
>
> If the base class is replaced with fstream I reasoned that the
> assignment would again copy the data members of the derived class as
> well as the data members of fstream.
Nope, streams are not copyable as there are no sensible semantics to that
operation. In general, derivation and assignment don't mix well, take a
look at this:
derived1 d1;
derived2 d2;
d1 = d2;// calls base::operator=()
> 'ios::operator =(const ios &)' is not accessible in function
> derived::operator =(derived &)
> Compiler could not generate operator= for class 'istream' in function
> iostream::operator =(iostream &)
> Compiler could not generate operator= for class 'ostream' in function
> iostream::operator =(iostream &)
>
> Why am I getting these messages and how can I fix it ?
The assignment-operator is private, you need to change your design to fix
it.
> get() { return derivedi; }
Missing returntype. Is this really the code you are running? If so, try
turning on compiler warnings.
Uli
--
FAQ: http://parashift.com/c++-faq-lite/
/* bittersweet C++ */
default: break;
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Ulrich
|
7/27/2004 11:03:15 AM
|
|
|
2 Replies
148 Views
(page loaded in 0.043 seconds)
Similiar Articles: plotting rectangle aroung moving objects - comp.soft-sys.matlab ...Hello, i am doing a project on object tracking and i used the method of subtraction to detect the motion.. and now i am stuck on how to plot a rec... comp.lang.c++.moderated - page 175Object Assignment 2 100 (7/26/2004 10:48:42 PM) Example #1 (see code below) Here is a simple example which assigns one object to another object of the same type. center 3ds object in a viewport - comp.graphics.api.opengl ...I am not suggesting that this is the best way, but here is how I do it: Find the center of your object, project that point to get window coordinates (using gluProject ... Selecting a background frame for a moving object differentiation ...Hello Everyone, I am a student who has been drafted to work on a moving object segmentation project. If i have a series of frames that make up a vide... Need to link shared objects after changing source code but not the ...Hi! In our company we have a big c++ software project devided into several shared object libs. several programs link particular so's to executables. ... [Urjent help]how to draw box around detected object in runtime ...Hi, i can working on a object detection project. I already finish the program to detect the object in still picture and mark it. Problem is how to do ... real-time object tracking using optical flow / XY coordinates ...hello every one,,i'm doin my grad project,,and i want to track in real-time a swarm of mobile robots using a webcam and know their locations,,i've use... move object under mouse - comp.graphics.api.openglhow could move an object under the mouse on a 3d plane? the complete simple project is on http://www.geocities.com/cannellag/opengl.zip the procedur... Object REXX 1.0.3 vs 2.1.2 problem - comp.lang.rexx... Historical uses; 4 IBM Products that used ... is today perceived as the "Windows 3.0 look". OS/2 1.x ... Object REXX for Windows and OS/2 to the Open Object REXX project ... Hough transform for rectangular objects - comp.soft-sys.matlab ...Hi, I`m doing a project on License plate recognition....can anyone suggest best way to extract license plate from an image...i`m trying to use hough ... Assignment Object (Project)Represents an assignment for a task or resource. The Assignment object is a member of an Assignments or an OverAllocatedAssignments collection. Object Variable Assignment (Visual Basic)You use a normal assignment statement to assign an object to an object variable. You can assign an object expression or the Nothing keyword, as the following example ... 7/18/2012 6:11:45 PM
|
|
|
|
|
|
|
|
|