×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

I got an explanation for you.

class Base{
public:
virtual void f(){}
virtual void f(int){}
};

class Derived : public Base{
public:
void f(){}
};

int main()
{
Derived d;
d.f(1);
}

This code has two names Base::f and Derived::f, Base::f is overloaded. When
the compiler sees d.f(1) it first looks up the name and finds Derived::f. It
doesn't look any further because all its doing at the moment is looking up
the name f. Having found Derived::f is then sees if it can match arguments
(i.e. do overload resolution). In this case it finds it can't so it gives up
with an error message.

It was designed for this way for reasons of simplicity, the rules for name
lookup and overload resolution are complicated enough already. Seperating
these rules makes things simpler.

the answer from a c++ expert on the internet.
Report

Replies, comments and Discussions:

  • 工作学习 / English / A question about C++ polymorphism & inheritence
    • 你都没说是什么问题,如何帮你:-/
      • Here you are! THANKS.
        本文发表在 rolia.net 枫下论坛Here is an example, is there anybody can tell me why the line 28 can not match , but the line 30 works.
        AS my understanding, the only difference is
        >> the line28 will use polymorphism and line 30 not.

        and in general, this question can be described as:
        if base class has several virtual member functions
        with same name but different parameters, and then the
        derived calss only override one of these functions,
        can we create the derived OBJECT from derived class (
        derivedclass * dc = new derivedclass), and call those
        function whicj are not be overridden by the derived class),

        or the only way is use the polymorphism to create the derived class ( baseclass *dc2= new derivedclass) ?


        What I want to know is the reason? Why it doesnot work?
        not how can we make it works, 'cause I know how. ;))


        1 #include <stdio.h>
        2 #include
        3
        4 class BASE
        5 {
        6 public:
        7 BASE() {};
        8 ~BASE() {};
        9 virtual fun() { cout << " BASE fun()" << endl; } ;
        10 virtual fun(int ) { cout << " BASE fun(int)" << endl; } ;
        11 virtual fun(int , int) { cout << " BASE fun(int )" << endl; } ;
        12 };
        13
        14 class DETRIVED: public BASE
        15 {
        16 public:
        17 DETRIVED() {};
        18 ~DETRIVED() {};
        19 fun() { cout << " DETRIVED fun()" << endl; } ;
        20 };
        21
        22
        23 int main()
        24 {
        25 DETRIVED *d1= new DETRIVED();
        26 BASE *d2= new DETRIVED();
        27
        28 d1->fun(1);
        29
        30 d2->fun(1);
        31 return 0;
        32 }





        "test.cc" 32 lines, 541 characters
        [ public]> g++ test.cc
        g++ test.cc
        test.cc: In function `int main()':
        test.cc:28: no matching function for call to `DETRIVED::fun (int)'
        test.cc:19: candidates are: DETRIVED::fun()

        [public]>更多精彩文章及讨论,请光临枫下论坛 rolia.net
        • the reasons:
          1. In the definition of

          14 class DETRIVED: public BASE
          15 {
          16 public:
          17 DETRIVED() {};
          18 ~DETRIVED() {};
          19 fun() { cout << " DETRIVED fun()" << endl; } ;

          firstly, the fun() is overided, so the fun(int) in BASE is not accessible in DETRIVED.
          secondly, you use "25 DETRIVED *d1= new DETRIVED()", so d1 is pointer of object of DERIVED instead of BASE like d2.
          • You are right, the key point is “if you override one of the overloaded member functions in the base class, the other overloaded versions become hidden in the derived class”
          • So it is C++ specific error, right?
            I mean, in general OO concept, the derived class should be able to only override one of the
            overloaded function in base calss, and not necessory to do anything to make the others visiable in derived class.

            Anyway, thanks very much!
            • No, it is not an error. The real world is complicated. What if the derived class has its own ways for other overloaded functions? There is always a trade-off.
          • Sorry, it is a C++ compiler specific error, right?
            :)
        • Thanks guys, but .. come in pls.
          本文发表在 rolia.net 枫下论坛But I have to ask the further information:
          1> It is C++ compiler error, right?
          In the general OO languaes concept,
          this should not be hidden, right?
          See the special example:
          if basecalss has 100 overloaded functions which
          are using the same name but different argues, but derived calss only
          want to override one of them and want to act the same way
          to all others.
          The current C++ compiler will hide the others, so derived calss has
          to declare them again to call the superclass.
          Is ir reasonable?
          I thinkn it break the basic OO concepts.


          2> Is there any one here know about the Vtbl &vptr?
          Of course, if you know, you will tell me the VTBL for d1 & d2 (in my previoud example) shoulb be same, but the overloaded function in d1 VTBL will be hidden by C++ compiler?
          And you might tell me, you can use some way to call the hidden functions.

          Ok, if so, I mean we can call the hidden function by some way (not directly),
          why the C++ compiler hidden from user directly calling?
          Is there ant benifits or good reasons?

          So I think there might be some hidden reason which make the 2 VTBL actually different, but it seems "SAME" to us at our currrent undersanding and test.

          Is there anyone know the detailed, hidden reason if it exist?

          or give me a reason why C++ need to do so?

          THANKS,更多精彩文章及讨论,请光临枫下论坛 rolia.net
          • I got an explanation for you.
            class Base{
            public:
            virtual void f(){}
            virtual void f(int){}
            };

            class Derived : public Base{
            public:
            void f(){}
            };

            int main()
            {
            Derived d;
            d.f(1);
            }

            This code has two names Base::f and Derived::f, Base::f is overloaded. When
            the compiler sees d.f(1) it first looks up the name and finds Derived::f. It
            doesn't look any further because all its doing at the moment is looking up
            the name f. Having found Derived::f is then sees if it can match arguments
            (i.e. do overload resolution). In this case it finds it can't so it gives up
            with an error message.

            It was designed for this way for reasons of simplicity, the rules for name
            lookup and overload resolution are complicated enough already. Seperating
            these rules makes things simpler.

            the answer from a c++ expert on the internet.