Topic: Redefinition of overloaded virtual functions.


Author: pabloh@hpwalea.hpl.hp.com (Pablo Halpern )
Date: 22 Jun 92 16:57:31 GMT
Raw View
In article <AAKBpFgeV5@scorp.icyb.kiev.ua>, scorp@scorp.icyb.kiev.ua
(Kritov Vyatcheslav Vitalievitch) writes:
|>IMHO, when you  use func() its not the same as func(void)
|>func() is the function with unknown ( not variable )
|>number of parameters. Therefore you have the same body
|>for two funcs

You are confusing C++ with ANSI C.  In ANSI C, what you say is true.  The
ANSI committee allows a function to be declared without paramters, meaning
that the compiler does not know the parameters and must, therefore, accept
any paramters.  This was done for compatability with K&R C, i.e. C before
prototypes.  It is considered an obolescent feature.

In C++, prototypes are *mandatory*.

   int foo();

is the same as

   int foo(void);

The C++ compiler will not match this function with a call that has
parameters.  In other words, C++ finished obsoleting the "unknown
parameters" feature.

- Pablo

------------------------------------------------------------------------
Pablo Halpern             (617) 290-3542
HP Waltham                pabloh@hpwarq.wal.hp.com

I am self-employed, so my opinions *do* reflect those of my employer.
However, they may not reflect the opinions of my client.
------------------------------------------------------------------------




Author: glw@io.uswest.com (Glenn Williams)
Date: Tue, 16 Jun 1992 22:00:00 GMT
Raw View
I have just run into a problem that I can't find in the ARM or the
Lippman Primer.

I want to define 2 overloaded virtual functions in a base class and in a
derived class only redefine one of them.

When I define only one of the functions in a derived class, I get a
warning that it is overriding the function that I did not redefine from the
base class. And if I try to use the function that is defined only in the base
class I get an undefined member error.

Do I have to redefine every single overloaded virtual function?

Where is this discussion in the ARM? Or anywhere else for that matter?

/* BEGIN SOURCE */
#include <stdio.h>
#include <string.h>

class BaseClass;

class BaseClass
{
 public:
  BaseClass(){};
  BaseClass( char *d ){ data = d; };
  ~BaseClass(){};

  //
  virtual char *Data();
  virtual char *Data( BaseClass *t );

 private:
  char *data;
};

class DerivedClass : public BaseClass
{
 public:
  DerivedClass(){};
  DerivedClass( char *d ) : BaseClass( d ){};
  ~DerivedClass(){};

  //
  virtual char *Data( BaseClass *t );
};

char *BaseClass::Data()
{
 return( data );
}

char *BaseClass::Data( BaseClass *t )
{
 return( t->Data() );
}

char *DerivedClass::Data( BaseClass *t )
{
 return( t->Data() );
}

main()
{
 char *str = "string";
 char *s;

 DerivedClass n( str );

 s = n.Data();
}
/* END SOURCE */


****************************************************************************
Glenn Williams
US WEST
****************************************************************************
****************************************************************************
"Your spookin' the cattle."
 Curly
****************************************************************************
****************************************************************************
"It'll be a big duke-a-roo, with me in one corner and Batman in the other."
 Joker
****************************************************************************





Author: bill@amber.ssd.csd.harris.com (Bill Leonard)
Date: 17 Jun 92 15:54:19 GMT
Raw View
In article <1992Jun16.220000.27177@advtech.uswest.com>, glw@io.uswest.com (Glenn Williams) writes:
> I want to define 2 overloaded virtual functions in a base class and in a
> derived class only redefine one of them.
>
> When I define only one of the functions in a derived class, I get a
> warning that it is overriding the function that I did not redefine from the
> base class. And if I try to use the function that is defined only in the base
> class I get an undefined member error.

I believe this to be correct behavior.

> Where is this discussion in the ARM? Or anywhere else for that matter?

See Section 13.1 of ARM.  This says (paraphrased) that you cannot overload
a function by inheriting one version and then defining a new one.

However, I have a slightly different scenario that touches on the same
issue.  Suppose I have the following classes:

     class A {
     public:
        virtual void
        foo(int) ;
     } ;

     class B {
     public:
        virtual void
        foo(void) ;
     } ;

     class C : public A, public B {
     public:
        virtual void
        foo(void) ;
     } ;

The important points are these: Function 'foo(int)' will only be invoked
through a pointer to A.  Function 'foo(void)' will only be invoked through
a pointer to B.  The virtualness of 'foo' is also important, of course.

Like Glenn, I get a warning that C::foo hides A::foo(int), but I don't
think I care.  My question is, will C::foo correctly replace B::foo in the
virtual-function table, and still allow whatever version of 'foo(int)' C
inherited to be called through a pointer to A?

In my case, I never try to call either version of 'foo' directly through an
object or pointer to C, so the hiding/overloading problem is not an issue
for me, I think.

This seems to be working as I expect, but I'd like some reassurance that it
is correct.

--
Bill Leonard
Harris Computer Systems Division
2101 W. Cypress Creek Road
Fort Lauderdale, FL  33309
bill@ssd.csd.harris.com
---------------------------------------------------------------------------
Foreign Aid:
   The transfer of money from poor people in rich countries
   to rich people in poor countries.
---------------------------------------------------------------------------




Author: scorp@scorp.icyb.kiev.ua (Kritov Vyatcheslav Vitalievitch)
Date: Wed, 17 Jun 92 15:40:52 +0300
Raw View
>   I want to define 2 overloaded virtual functions in a base class and in a
>   derived class only redefine one of them.
>
>   When I define only one of the functions in a derived class, I get a
>   warning that it is overriding the function that I did not redefine from the
>   base class. And if I try to use the function that is defined only in the base
>   class I get an undefined member error.
>
>   Do I have to redefine every single overloaded virtual function?
>
>   Where is this discussion in the ARM? Or anywhere else for that matter?
>
>   /* BEGIN SOURCE */
>   #include <stdio.h>
>   #include <string.h>
>
>   class BaseClass;
>
>   class BaseClass
>   {
>           public:
>                   BaseClass(){};
>                   BaseClass( char *d ){ data = d; };
>                   ~BaseClass(){};
>
>                   //
>                   virtual char *Data();
>                   virtual char *Data( BaseClass *t );
>
>           private:
>                   char *data;
>   };
>
>   class DerivedClass : public BaseClass
>   {
>           public:
>                   DerivedClass(){};
>                   DerivedClass( char *d ) : BaseClass( d ){};
>                   ~DerivedClass(){};
>
>                   //
>                   virtual char *Data( BaseClass *t );
>   };
>
>   char *BaseClass::Data()
>   {
>           return( data );
>   }
>
>   char *BaseClass::Data( BaseClass *t )
>   {
>           return( t->Data() );
>   }
>
>   char *DerivedClass::Data( BaseClass *t )
>   {
>           return( t->Data() );
>   }
>
>   main()
>   {
>           char *str = "string";
>           char *s;
>
>           DerivedClass n( str );
>
>           s = n.Data();
>   }
>   /* END SOURCE */
IMHO, when you  use func() its not the same as func(void)
func() is the function with unknown ( not variable )
number of parameters. Therefore you have the same body
for two funcs

--
| 2b|~2b - not FAQ | Kiev , Ukraine (044) 277-37-71 | Carry Flag over world |
| Kritov Slava - ( Phystech's Nest ) at scorp@scorp.icyb.kiev.ua%ussr.eu.net|
| "Welcome,my son, Welcome to the Machine,/ Where have you been ?" ** PF ** |