🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

1 total messages Started by psc@cadnetix.COM Fri, 03 Mar 1989 21:55
Virtual Operator Frustration
#2694
Author: psc@cadnetix.COM
Date: Fri, 03 Mar 1989 21:55
119 lines
4538 bytes
I have been wrestling with a problem involving overloaded virtual
operators= using cfront 1.2.1 for two days now.  The compiler will not
allow me to overload virtual operator functions in the base class without
doing the same in derived classes.  It says, "Sorry, not implemented.".

So I remove the virtual keyword and now expect I'm simply overriding one
base definition, but it whines that it parsed an "unexpected 1 argument"
for a constructor.  Why does it want to invoke a constructor anyway?  If
I provide a constructor that takes the same args as the operator, it
stops complaining, compiles, but invokes the constructor instead of the
operator.

Instead of a constructor, I put in an explicit function in the bottom level
derived classes which call the base class operators and, lo and behold, it
compiles.  Unfortunately, it costs me two function calls to get the job of
one done.  But why is this necessary?  Is it possible that the base class
public operator= is not being properly inherited?  Does it think I'm
attempting a bitwise copy and needs a constructor for a temporary object.
Has anyone encountered this?

I gained new respect for Zortech 1.07e.  I tried the problem at home with
this $99 product, hoping to get some more hints from someone else's error
messages, only no errors.  Not only did it compile, allowing overloaded
virtual operators in the base and non-overloaded derived classes, but it
performed exactly as I expected.  Guess I got my money's worth.

Code follows text.  The heirarchy is three tiered.  I tried to simplify to
a two tiered example, but cfront's behavior was inexplicably different in
that case. The heirarchy is: Base publically begets Derived1 which in turn
pubically begets both Derived2a and Derived 2b.

I want Derived2 classes to perform assignment with 2a taking a char* and
2b a long.  When opposite argument types are presented to these classes,
I want the appropriate Derived1 operator= to be invoked instead.  The
following code is the version Zortech liked.  Afterward is the output.

(cut here)
-------------------------------------------------------------------------
#include <stdio.h>
#include <stream.hpp>   // I did not name this file.

class Base
{   public:
        Base( ) { printf( "Base::Base( ), " ); }
        virtual void operator=( const char* val ) {
            cout << "Base::operator=( \"" << val << "\" )\n" ;
        }
        virtual void operator=( const long val ) {
            cout << "Base::operator=( " << val << " )\n" ;
        }
};

class Derived1 : public Base
{   public :
        Derived1( ) { printf( "Derived1::Derived1( ), " ); }
        virtual void operator=( const char* val ) {
            cout << "Derived1::operator=( \"" << val << "\" )\n" ;
        }
        virtual void operator=( const long val ) {
            cout << "Derived1::operator=( " << val << " )\n" ;
        }
};

class Derived2a : public Derived1
{   public :
        Derived2a( ) { puts( "Derived2a::Derived2a( )" ); }
        virtual void operator=( const char* val ) {
            cout << "Derived2a::operator=( \"" << val << "\" )\n" ;
        }
};

class Derived2b : public Derived1
{   public :
        Derived2b( ) { puts( "Derived2b::Derived2b( )" ); }
        virtual void operator=( const long val ) {
            cout << "Derived2b::operator=( " << val << " )\n" ;
        }
};

main( )
{
    Base* b1 = new Derived2a ;
    Base* b2 = new Derived2b ;
    Derived2a d2a ;
    Derived2b d2b ;

    // these should invoke the Derived2::operator=( )
    *b1 = "1st string" ;
    *b2 = 125 ;
    d2a = "2nd string" ;
    d2b = 250 ;

    // these should invoke the Derived1::operator=( )
    *b1 = 222 ;
    *b2 = "Oh no, cfront doesn't like this!" ;
    d2a = 666 ;
    d2b = "Help, where is 2.0?" ;
}
//                    Output follows
---------------------------------------------------------------
Base::Base( ), Derived1::Derived1( ), Derived2a::Derived2a( )
Base::Base( ), Derived1::Derived1( ), Derived2b::Derived2b( )
Base::Base( ), Derived1::Derived1( ), Derived2a::Derived2a( )
Base::Base( ), Derived1::Derived1( ), Derived2b::Derived2b( )
Derived2a::operator=( "1st string" )
Derived2b::operator=( 125 )
Derived2a::operator=( "2nd string" )
Derived2b::operator=( 250 )
Derived1::operator=( 222 )
Derived1::operator=( "Oh no, cfront doesn't like this!" )
Derived1::operator=( 666 )
Derived1::operator=( "Help, where is 2.0?" )


	Paul Crook c/o Cadnetix Corporation
	5775 Flatiron Parkway
	Boulder, CO 80301
	(303) 444-8075 x401
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads