minor annoyance: derived class' operator=

New Message Reply Date view Thread view Subject view Author view

From: Skluzacek, Christian (C.Skluzacek++at++fokkerspace.nl)
Date: 02/15/2002 05:14:31


Hi all,

This is probably more of a C++ question but it came up when I was deriving
my classes from pfMemory. Two small things really:

1. pfMemory declares operator= a virtual function which gives me warnings
during compilation because the derived class' signature does not match
pfMemory's signature.

2. From what I've seen, operator= usually uses references instead of
pointers. pfMemory uses pointers.

Ok, these aren't really errors but it is kind of annoying to see all of
these warning messages come up when I am compiling (under Irix) with
-fullwarn.

Following is a test program which reproduces these warnings. Compile (under
Irix) with:

CC -LANG:std -o opequals opequals.C

add -DUSEPTR to use the pointer version
add -DVIRTUAL=virtual to use the virtual version.

opequals.C:
-----------------
#include <iostream>

#ifndef VIRTUAL
#define VIRTUAL
#endif

////////////////////////////////////////////////////////////////////////////
//
struct Base
{
  Base( int val = 0 ) : baseData(val) {}
#ifdef USEPTR
  VIRTUAL Base * operator=( const Base * rhs )
    {
      if ( this != rhs ) {
        baseData = rhs->baseData;
      }
      return this;
    } // operator=
#else
  VIRTUAL Base & operator=( const Base & rhs )
    {
      if ( this != &rhs ) {
        baseData = rhs.baseData;
      }
      return *this;
    } // operator=

#endif

  bool operator==( const Base & rhs ) const
    {
      return baseData == rhs.baseData;
    } // operator==

  friend std::ostream& operator<<( std::ostream & os, const Base & b )
    {
      os << "Base: " << b.baseData; return os;
    } // operator<<

  int baseData;
}; // class Base
////////////////////////////////////////////////////////////////////////////
//
struct Derived : public Base
{
  Derived( int dval = 0, int bval = 0) : Base(bval), derivedData(dval) {}

#ifdef USEPTR
  VIRTUAL Derived * operator=( const Derived * rhs )
    {
      if ( this != rhs ) {
        Base::operator=(rhs);
        derivedData = rhs->derivedData;
      }
      return this;
    } // operator=
#else
  VIRTUAL Derived & operator=( const Derived & rhs )
    {
      if ( this != &rhs ) {
        Base::operator=(rhs);
        derivedData = rhs.derivedData;
      }
      return *this;
    } // operator=
#endif

  bool operator==( const Derived & rhs ) const
    {
      return ( Base::operator==(rhs) &&
               derivedData == rhs.derivedData );
    }

  friend std::ostream& operator<<( std::ostream & os, const Derived & d )
    {
      os << static_cast<const Base &>(d) << std::endl
         << "Derived: " << d.derivedData; return os;
    } // operato<<

  int derivedData;
}; // class Derived

////////////////////////////////////////////////////////////////////////////
//
int main( int, char ** )
{
  Base b1(1), b2(2);
  Derived d1(11,21), d2(12,22);

  std::cout<<b1<<std::endl;
  std::cout<<d1<<std::endl;
  b1 = d1;
// d2 = b1; <--- illegal
  std::cout<<b1<<std::endl;

} // main

****************************************************************************

This communication is intended for use by the addressee and may
contain confidential or privileged information. If you receive this
communication unintentionally, please notify us immediately and
delete the message from your computer without making any copies.

****************************************************************************


New Message Reply Date view Thread view Subject view Author view

This archive was generated by hypermail 2b29 : Fri Feb 15 2002 - 05:14:48 PST

This message has been cleansed for anti-spam protection. Replace '++at++' in any mail addresses with the '@' symbol.