Topic: Inheritance and overloading of static methods


Author: Andreas Mueller <andreasm@darmstadt.gmd.de>
Date: 1999/09/02
Raw View


Barry Margolin wrote:

> In article <37C2D254.1C84063C@darmstadt.gmd.de>,
> Andreas Mueller  <andreasm@darmstadt.gmd.de> wrote:
> >Hi,
> >
> >I have the following problem:
> >
> >class B should inherit the static methods test(int) and test(char*) from
> >
> >class Base.
> >Additionally I want to overload these methods with test(float).
> >When I simply write a new method void test(float) the inherited methods
> >aren't available anymore.
> >
> >class A and class C just show that ineritance of static methods normally
> >works. I don't overload
> >the test-method in A, I just inherit from Base. In class C I simply
> >write a new method called
> >testFloat(float), which is the workaround I'm currently using. I _don't_
> >want to do it like I did in class D, which redefines all test-methods.
> >
> >Is there any way to do it the proper way (like tried in class B)? If
> >not, why not?
>
> No.  Overload resolution is performed *after* class resolution.  When
> looking for a function, you first search up the class hierarchy to find a
> class that defines the function.  Then you match the argument types against
> all the overloaded versions within that class.

Actually, there _is_ a solution. Class B has to look like this.

##### File: B.hpp #######################################
#ifndef __CLASS_B_HPP
#define __CLASS_B_HPP

#include "Base.hpp"

class B : public Base
{
    using Base::test;
 public:
  static void test(float f);

};

#endif

The using-keyword does exactly what I wanted.

Yours, Andreas



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Barry Margolin <barmar@bbnplanet.com>
Date: 1999/08/25
Raw View
In article <37C2D254.1C84063C@darmstadt.gmd.de>,
Andreas Mueller  <andreasm@darmstadt.gmd.de> wrote:
>Hi,
>
>I have the following problem:
>
>class B should inherit the static methods test(int) and test(char*) from
>
>class Base.
>Additionally I want to overload these methods with test(float).
>When I simply write a new method void test(float) the inherited methods
>aren't available anymore.
>
>class A and class C just show that ineritance of static methods normally
>works. I don't overload
>the test-method in A, I just inherit from Base. In class C I simply
>write a new method called
>testFloat(float), which is the workaround I'm currently using. I _don't_
>want to do it like I did in class D, which redefines all test-methods.
>
>Is there any way to do it the proper way (like tried in class B)? If
>not, why not?

No.  Overload resolution is performed *after* class resolution.  When
looking for a function, you first search up the class hierarchy to find a
class that defines the function.  Then you match the argument types against
all the overloaded versions within that class.

>    I should say here that this problem not only occurs under Microsoft
>Visual C 5.0 but also under Linux with egcs 95.

That's good, because it's required by the language specification.

>PPS
>Sadly it's not possible to look for a solution in the actual ansi c++
>specification.
>This should be online for free!

While that may be true, the answer should be in every C++ textbook on the
market.

--
Barry Margolin, barmar@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Andreas Mueller <andreasm@darmstadt.gmd.de>
Date: 1999/08/24
Raw View
Hi,

I have the following problem:

class B should inherit the static methods test(int) and test(char*) from

class Base.
Additionally I want to overload these methods with test(float).
When I simply write a new method void test(float) the inherited methods
aren't available anymore.

class A and class C just show that ineritance of static methods normally

works. I don't overload
the test-method in A, I just inherit from Base. In class C I simply
write a new method called
testFloat(float), which is the workaround I'm currently using. I _don't_

want to do it like I did in class D, which redefines all test-methods.

Is there any way to do it the proper way (like tried in class B)? If
not, why not?
Please answer also with mail because it's easier to check mails than to
check all the groups I posted to - thank you.

Yours,
    Andreas

PS
    I should say here that this problem not only occurs under Microsoft
Visual C 5.0 but also under Linux with egcs 95.
PPS
Sadly it's not possible to look for a solution in the actual ansi c++
specification.
This should be online for free!

Here's the source:

##### File: main.cpp ####################################
#include "A.hpp"
#include "B.hpp"
#include "C.hpp"
#include "D.hpp"
#include "Base.hpp"

int main(int argc,char* argv[])
{
 int testInt=17;
 char* testCharPtr=0;
 float testFloat=0.17f;

 Base::test(testInt);
 Base::test(testCharPtr);


 // Everything ok... static methods inherited...
 A::test(testInt);
 A::test(testCharPtr);

 // This won't work. No access to the old overloaded test-methods.
WHY??!
 //B::test(testInt);   // main.cpp(21) : warning C4244: 'argument' :
conversion from 'int' to 'float', possible loss of data
 //B::test(testCharPtr);  // main.cpp(22) : error C2664: 'test' : cannot

convert parameter 1 from 'char *' to 'float'
 B::test(testFloat);

 // this works
 C::test(testInt);
 C::test(testCharPtr);
 C::testFloat(testFloat);

 // this works, but it's not what I want... I want _inheritance_ not
redefinition!
 D::test(testInt);
 D::test(testCharPtr);
 D::test(testFloat);

 return 0;
}
#########################################################


##### File: Base.hpp ####################################

#ifndef __CLASS_BASE_HPP
#define __CLASS_BASE_HPP

class Base
{
 public:
  static void test(int i);
  static void test(char *c);
};

#endif
#########################################################


##### File: Base.cpp ####################################
#include "Base.hpp"

void Base::test(int i)
{
 i=0;
}

void Base::test(char *c)
{
 c=0;
}
#########################################################


##### File: A.hpp #######################################
#ifndef __CLASS_A_HPP
#define __CLASS_A_HPP

#include "Base.hpp"

class A : public Base
{
};

#endif
#########################################################


##### File: B.hpp #######################################
#ifndef __CLASS_B_HPP
#define __CLASS_B_HPP

#include "Base.hpp"

class B : public Base
{
 public:
  static void test(float f);

};

#endif
#########################################################


##### File: B.cpp #######################################
#include "B.hpp"

void B::test(float f)
{
 f=17.0f;
}
#########################################################


##### File: C.hpp #######################################
#ifndef __CLASS_C_HPP
#define __CLASS_C_HPP

#include "Base.hpp"

class C : public Base
{
 public:
  static void testFloat(float f);

};

#endif
#########################################################


##### File: C.cpp #######################################
#include "C.hpp"

void C::testFloat(float f)
{
 f=17.0f;
}
#########################################################


##### File: D.hpp #######################################
#ifndef __CLASS_D_HPP
#define __CLASS_D_HPP

#include "Base.hpp"

class D : public Base
{
 public:
  static void test(int i);
  static void test(char *c);
  static void test(float f);

};

#endif
#########################################################


##### File: D.cpp #######################################
#include "D.hpp"


void D::test(int i)
{
 Base::test(i);
}

void D::test(char *c)
{
 Base::test(c);
}

void D::test(float f)
{
 f=17.0f;
}
#########################################################
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]