Why virtual function can not be overloaded?

Discussion in 'C++' started by PengYu.UT@gmail.com, Sep 23, 2005.

  1. Guest

    The following program shows that virtual function can not be
    overloaded.

    Could you tell me the reasons from the C++ compiler point of view?
    Thanks!

    Peng

    #include <iostream>

    class B{
    public:
    virtual void doit(){
    std::cout << "in B" << std::endl;
    }
    void doit(int i){
    std::cout << i << std::endl;
    doit();
    }
    };

    class D: public B{
    public:
    virtual void doit(){
    std::cout << "in D" << std::endl;
    }
    };

    int main(int argc, char *argv[]) {
    D d;
    d.doit(1);
    }
     
    , Sep 23, 2005
    #1
    1. Advertising

  2. wrote:
    > The following program shows that virtual function can not be
    > overloaded.
    >
    > Could you tell me the reasons from the C++ compiler point of view?
    > Thanks!
    >
    > Peng
    >
    > #include <iostream>
    >
    > class B{
    > public:
    > virtual void doit(){
    > std::cout << "in B" << std::endl;
    > }
    > void doit(int i){
    > std::cout << i << std::endl;
    > doit();
    > }
    > };
    >
    > class D: public B{
    > public:


    using B::doit;

    > virtual void doit(){
    > std::cout << "in D" << std::endl;
    > }
    > };
    >
    > int main(int argc, char *argv[]) {
    > D d;
    > d.doit(1);
    > }


    You can also invoke d.doit() as follows

    B& b = d;
    b.doit(1);

    --
    Jonathan Turkanis
    www.kangaroologic.com
     
    Jonathan Turkanis, Sep 23, 2005
    #2
    1. Advertising

  3. * :
    > The following program shows that virtual function can not be
    > overloaded.
    >
    > Could you tell me the reasons from the C++ compiler point of view?
    > Thanks!
    >
    > Peng
    >
    > #include <iostream>
    >
    > class B{
    > public:
    > virtual void doit(){
    > std::cout << "in B" << std::endl;
    > }
    > void doit(int i){
    > std::cout << i << std::endl;
    > doit();
    > }
    > };
    >
    > class D: public B{
    > public:


    using B::doit;

    > virtual void doit(){
    > std::cout << "in D" << std::endl;
    > }
    > };
    >
    > int main(int argc, char *argv[]) {
    > D d;
    > d.doit(1);
    > }


    FAQ 23.7,
    <url: http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.7>

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Sep 23, 2005
    #3
  4. wrote:

    > The following program shows that virtual function can not be
    > overloaded.


    There's a subtle difference between "can not be overloaded" and "I
    don't understand what happens".

    > Could you tell me the reasons from the C++ compiler point of view?


    The compiler follows the C++ standard, thats its job.

    > #include <iostream>
    >
    > class B{
    > public:
    > virtual void doit(){
    > std::cout << "in B" << std::endl;
    > }
    > void doit(int i){
    > std::cout << i << std::endl;
    > doit();
    > }
    > };
    >
    > class D: public B{
    > public:
    > virtual void doit(){
    > std::cout << "in D" << std::endl;
    > }
    > };
    >
    > int main(int argc, char *argv[]) {
    > D d;
    > d.doit(1);
    > }


    D::doit() hides all methods with the same name for all base classes.
    You'd better write d.B::doit(1). You can easily import these methods
    using a "using" declaration. Try this:

    class D: public B{
    public:
    virtual void doit(){
    std::cout << "in D" << std::endl;
    }
    using B::doit;
    };

    The "using" imports all methods with name "doit" from B's into A's
    namespace.

    Mathias
     
    Mathias Waack, Sep 23, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nitin Bhardwaj
    Replies:
    8
    Views:
    889
    Andrey Tarasevich
    Jul 14, 2003
  2. A. Saksena
    Replies:
    1
    Views:
    479
    Alf P. Steinbach
    Jan 14, 2005
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,000
    Smokey Grindel
    Dec 2, 2006
  4. Replies:
    20
    Views:
    870
  5. Mark
    Replies:
    8
    Views:
    440
Loading...

Share This Page