Object Assignment

  • Follow


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:













7/18/2012 6:11:45 PM


Reply: