Thread: What exactly is a function signature?

  1. #16
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Quote Originally Posted by laserlight View Post
    Because member names in the derived class hide corresponding member names in the base class, so you should use a using declaration to bring back the member names from the base class as necessary.
    Would this be the appropriate solution
    Code:
    #include <iostream>
    
    class A {
      public:
       virtual int oper(int x, int y) {
          return x + y;
       }
       virtual double oper(double x, double y) {
          return x + y;
       }
    };
    
    class B : public A {
      public:
       virtual double oper(double x, double y) {
          return x * y;
       }
    };
    
    int main(void) {
       A x;
       B y;
       std::cout << x.oper(1,2) << std::endl; // 3
       std::cout << x.oper(1.10,2.0) << std::endl; // 3.1
       std::cout << y.oper(1,2) << std::endl; // 2, not 3 because oper has derived class's member has overriden base class's name
       std::cout << dynamic_cast<A&>(y).oper(1,2) << std::endl; // 3, realizes to use base class's member function for ints
       std::cout << y.oper(1.10,2.0) << std::endl; // 2.2
       return 0;
    }
    I have never run into this before. Does it come up anywhere in typical development or in the standard libraries?

  2. #17
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by laserlight View Post
    That is not true. The virtual function override cannot have a different return type specified, unless that return type is a subtype. In such cases, it can actually be a good idea to make use of such covariant return types.
    I think this is what I was referring to:
    Chapter 38: Practice Safe Overriding
    Although, the example they use is when changing default argument values.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #18
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,398
    Quote Originally Posted by Sebastiani View Post
    But what about:

    Code:
    struct base
    {
        virtual operator string ( void ) const;
        
        virtual operator int ( void ) const;
    };
    In one case the function name is "operator string" and in the other case "operator int". The two functions have different names and therefore do not overload each other.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #19
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Would this be the appropriate solution
    You could rather use:

    Code:
    y.A::oper(1,2)
    Laserlight's suggestion probably makes more sense, since it would be very awkward having to know which base class's method should be called in one or another particular case, especially since the compiler won't tell you when you get it wrong.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #20
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> In one case the function name is "operator string" and in the other case "operator int". The two functions have different names and therefore do not overload each other.

    Yes, but if you look at my second example it's clear that the practical implications are the same. The compiler finds an ambiguity that it can't resolve unless the "arguments" to the conversion (ie: float or double) are specified. In that sense, it's essentially as if the functions have the same name (although technically, you are probably correct). But as Laserlight pointed out, this isn't a matter of function overloading but of implicit conversion, so it doesn't even apply to the same section of the standard.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #21
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Quote Originally Posted by anon View Post
    You could rather use:
    Code:
    y.A::oper(1,2)
    Laserlight's suggestion probably makes more sense.
    So for my example class B would be
    Code:
    class B : public A {
      public:
       using A::oper;
       virtual double oper(double x, double y) {
          return x * y;
       }
    };
    This does as I expect, but did the compiler bring both of A's oper functions and then override the oper(double, double) function?

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,323
    Quote Originally Posted by cpjust
    I think this is what I was referring to:
    Chapter 38: Practice Safe Overriding
    Although, the example they use is when changing default argument values.
    I see. I am probably stating the obvious, but of course changing default arguments is different from changing return types.

    Quote Originally Posted by linuxdude
    This does as I expect, but did the compiler bring both of A's oper functions and then override the oper(double, double) function?
    Yes, the using declaration makes the name from the base class available in the derived class' scope, and both member functions have the same name.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. string::find_last_of function signature
    By WebSnozz in forum C++ Programming
    Replies: 6
    Last Post: 10-09-2002, 09:12 PM