Confused with the "virtual base class" concept

Discussion in 'C++' started by mshetty, Aug 29, 2004.

  1. mshetty

    mshetty Guest

    Hi,

    I get an error "Warning: b::a_method hides the virtual function
    a::a_method()."

    on compiling the following code..

    #include <iostream.h>
    class a
    {
    public:
    virtual int a_method ()
    {
    cout << "a::a_method" << endl;
    return 0;
    }
    };

    class b : public a
    {
    public:
    int a_method (int x)
    {
    cout << "b::a_method" << endl;
    return x;
    }
    };

    class c : public a
    {
    public:
    int a_method ()
    {
    cout << "c::a_method" << endl;
    return 100;
    }
    };

    int main()
    {
    b b_var;
    b_var.a_method(10);
    return 0;
    }

    The warning goes off if done the following:
    Change class b : public a
    to
    class b : public virtual a

    Have not been to understand the reason why the warning is removed as
    "b::a_method still hides a::a_method()"

    Would help if I could get some input.

    Thanks and Regards,
    M Shetty

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    mshetty, Aug 29, 2004
    #1
    1. Advertising

  2. mshetty

    David Harmon Guest

    On 29 Aug 2004 18:46:06 -0400 in comp.lang.c++,
    (mshetty) wrote,
    >I get an error "Warning: b::a_method hides the virtual function
    >a::a_method()."


    This issue is covered in Marshall Cline's C++ FAQ. See the topic
    "[23.6] What's the meaning of, Warning: Derived::f(float) hides
    Base::f(int)?" It is always good to check the FAQ before posting.
    You can get the FAQ at:
    http://www.parashift.com/c -faq-lite/
     
    David Harmon, Aug 30, 2004
    #2
    1. Advertising

  3. mshetty

    Sharad Kala Guest

    "David Harmon" <> wrote in message
    news:...
    > On 29 Aug 2004 18:46:06 -0400 in comp.lang.c++,
    > (mshetty) wrote,
    > >I get an error "Warning: b::a_method hides the virtual function
    > >a::a_method()."

    >
    > This issue is covered in Marshall Cline's C++ FAQ. See the topic
    > "[23.6] What's the meaning of, Warning: Derived::f(float) hides
    > Base::f(int)?" It is always good to check the FAQ before posting.
    > You can get the FAQ at:
    > http://www.parashift.com/c -faq-lite/


    Probably you are missing his question. He does know that the function is
    being hidden, his question is about warnings getting removed after making
    class A a virtual base.
    To OP - Which compiler ? I get a warning with Comeau even after making it a
    virtual base - "warning: function "a::a_method()" is hidden by
    "b::a_method" -- virtual function override intended? int a_method (int x)"

    -Sharad
     
    Sharad Kala, Aug 30, 2004
    #3
  4. > I get an error "Warning: b::a_method hides the virtual function
    > a::a_method()."


    > class b : public a
    > {
    > public:
    > int a_method (int x)
    > {
    > cout << "b::a_method" << endl;
    > return x;
    > }
    > };


    I don't know why virtual inheritance removes this, but when you declare an
    overloaded function in derived class (b::a_method(int)) it hides all
    functions with the same name from base class (a::a_method()). For example,
    you can't call b.a_method(); If you include using a::a_method; in b
    definition, a::a_method won't become hidden. Btw, you probably want to
    declare a_method(int) as virtual, too, don't you?

    --
    Ivan

    e-mail me at: korotkov2 at ztel dot ru


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Ivan Korotkov, Aug 30, 2004
    #4
  5. mshetty

    Carl Barron Guest

    mshetty <> wrote:

    > Hi,
    >
    > I get an error "Warning: b::a_method hides the virtual function
    > a::a_method()."
    >
    > on compiling the following code..
    >
    > #include <iostream.h>
    > class a
    > {
    > public:
    > virtual int a_method ()
    > {
    > cout << "a::a_method" << endl;
    > return 0;
    > }
    > };
    >
    > class b : public a
    > {
    > public:
    > int a_method (int x)
    > {
    > cout << "b::a_method" << endl;
    > return x;
    > }
    > }

    note well a::a_method() is a function with no args retuning int, and
    b::a_method(int) is a function with an int arg returning int. Since b
    has no method a_method() but one of a_method(int); it will hide [not
    have available] the virtual function a_method(), if want the
    vritual-ness of a_method() you must have such a function in b, since you
    already have a a_method(int) which hides the defaulting a::method() of
    being used, If you really want b::a_method_a(int) as well, then you need
    a forwarding member function
    void a_method() {a::a_method();} as well.

    Virtual bases are used to provide just one base class of a type rather
    then multiples in cases like this:

    class Base{};
    class Inbetween_1:public virtual Base{};
    class Inbetween_2:public virtual Base{};
    class TheWinner:public Inbetween_1,public Inbetween_2{};

    this makes one instande of Base in an instance of TheWinner;

    if the Inbetween's above were to each inherit Base non-virtually then
    TheWinner would have two instances of Base namely
    Inbetween_1::Base and Inbetween_2::Base. The class diagram of the
    virtual inheritance case looks like a rhombus, and the non virtual
    inheritance diagram looks like a Y.



    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Carl Barron, Aug 30, 2004
    #5
  6. mshetty <> wrote:

    []

    > The warning goes off if done the following:
    > Change class b : public a
    > to
    > class b : public virtual a
    >
    > Have not been to understand the reason why the warning is removed as
    > "b::a_method still hides a::a_method()"


    It looks like poor diagnostic.

    --
    Maxim Yegorushkin

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Maxim Yegorushkin, Aug 31, 2004
    #6
  7. I have added a line in class b.
    read it.
    hth
    david michell

    #include <iostream.h>
    class a
    {
    public:
    virtual int a_method ()
    {
    cout << "a::a_method" << endl;
    return 0;
    }
    };

    class b : public a
    {
    public:
    virtual int a_method (){} /*ADD THIS LINE becaue
    class c has to know that this method is virtual. It will not be taken
    as default by inheritance*/


    int a_method (int x)
    {
    cout << "b::a_method" << endl;
    return x;
    }
    };

    class c : public a
    {
    public:
    int a_method ()
    {
    cout << "c::a_method" << endl;
    return 100;
    }
    };

    int main()
    {
    b b_var;
    b_var.a_method(10);
    return 0;
    }

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    David Michell, Aug 31, 2004
    #7
  8. (mshetty) wrote in message news:<>...
    > Hi,
    >
    > I get an error "Warning: b::a_method hides the virtual function
    > a::a_method()."
    >
    > on compiling the following code..
    >
    > #include <iostream.h>
    > class a
    > {
    > public:
    > virtual int a_method ()
    > {
    > cout << "a::a_method" << endl;
    > return 0;
    > }
    > };
    >
    > class b : public a
    > {
    > public:
    > int a_method (int x)
    > {
    > cout << "b::a_method" << endl;
    > return x;
    > }
    > };
    >
    > class c : public a
    > {
    > public:
    > int a_method ()
    > {
    > cout << "c::a_method" << endl;
    > return 100;
    > }
    > };
    >
    > int main()
    > {
    > b b_var;
    > b_var.a_method(10);
    > return 0;
    > }
    >
    > The warning goes off if done the following:
    > Change class b : public a
    > to
    > class b : public virtual a
    >
    > Have not been to understand the reason why the warning is removed as
    > "b::a_method still hides a::a_method()"
    >
    > Would help if I could get some input.


    Hi, mshetty. It is a silly warning, you can simply ignore it. For
    example the Visual Studio .NET with 4 level (highest) of warning
    messages did not generate it. But it is pretty-clear allusion to bad
    design of your program.

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Vladislav Lazarenko, Aug 31, 2004
    #8
  9. > public:
    > virtual int a_method (){} /*ADD THIS LINE becaue
    > class c has to know that this method is virtual. It will not be taken
    > as default by inheritance*/


    class C inherits A directly, or is it a typo? Anyway, overriding a_method
    as empty is not excellent, it's better to import it from A by using A::f;
    in class B. Btw, which compiler did you use? VC++ understands that
    a_method is virtual:

    #include <iostream>

    using namespace std;

    class A { public : virtual void f() { cout << "In A::f\n"; } };
    class B : public A { public: void f(int) {} };
    class C : public B { public: void f() { cout << "In C::f\n"; }};

    int main()
    {
    C c;

    static_cast<A *>(&c)->f();

    return 0;
    }

    yields

    In C::f

    --
    Ivan

    e-mail me at: korotkov2 at ztel dot ru

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Ivan Korotkov, Aug 31, 2004
    #9
  10. mshetty

    Mike Bland Guest

    (mshetty) wrote in message news:<>...
    > Hi,
    >
    > I get an error "Warning: b::a_method hides the virtual function
    > a::a_method()."
    >
    > on compiling the following code..


    [snip]

    > The warning goes off if done the following:
    > Change class b : public a
    > to
    > class b : public virtual a
    >
    > Have not been to understand the reason why the warning is removed as
    > "b::a_method still hides a::a_method()"


    As others have pointed out, b::a_method() does hide a::a_method(), and
    it seems it should hide it whether "a" is a virtual base of "b" or
    not.

    Intrigued by this, I tried your code under both GCC 3.4.1 and Sun CC
    5.3 (WorkShop 6 Update 2) on Solaris 8, and was astonished by the
    results:

    `g++ -Wall -ansi -pedantic` compiled both the virtual and nonvirtual
    versions without so much as a single warning (after changing
    iostream.h to iostream and adding "using std::cout" and "using
    std::endl" at the top). I tried this multiple times out of disbelief,
    but the result was indeed the same each time; no mistyping on my part.

    `CC +w2` produced the following results:

    --BEGIN--
    # CC +w2 virtual_base.cxx

    "virtual_base.cxx", line 25: Warning: b::a_method hides the virtual
    function a::a_method().
    1 Warning(s) detected.

    # vim virtual_base.cxx

    [change base to virtual]

    # CC +w2 virtual_base.cxx

    "virtual_base.cxx", line 25: Warning: b::a_method Hides the virtual
    function a::a_method() in a virtual base.
    1 Warning(s) detected.
    --END--

    So now I'm even more confused. The Sun compiler seems to be right on
    with this one, and GCC is oblivious, which is exactly backwards from
    what I might've expected. What compiler and platform are you using,
    M? Can anyone else provide some insight into what's going on?

    Mike

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Mike Bland, Sep 1, 2004
    #10
  11. mshetty

    Mike Bland Guest

    (Mike Bland) wrote in message news:<>...
    > So now I'm even more confused. The Sun compiler seems to be right on
    > with this one, and GCC is oblivious, which is exactly backwards from
    > what I might've expected.


    Whoops, after looking at the GCC info page, I realized that what I
    need to use is -Woverloaded-virtual. Wonder why it's not
    automatically a part of -Wall.

    Still, I'd be interested in knowing which compiler the OP was using.

    Mike

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Mike Bland, Sep 3, 2004
    #11
  12. mshetty

    mshetty Guest

    (Mike Bland) wrote in message news:<>...
    > (Mike Bland) wrote in message news:<>...
    > > So now I'm even more confused. The Sun compiler seems to be right on
    > > with this one, and GCC is oblivious, which is exactly backwards from
    > > what I might've expected.

    >
    > Whoops, after looking at the GCC info page, I realized that what I
    > need to use is -Woverloaded-virtual. Wonder why it's not
    > automatically a part of -Wall.
    >
    > Still, I'd be interested in knowing which compiler the OP was using.
    >


    Hi,

    Details of the compiler and system I am using
    Compiler : CC: Sun WorkShop 6 update 2 C++ 5.3 2001/05/15
    OS : SunOS pcspsun3 5.7 Generic_106541-34 sun4u sparc SUNW,Ultra-5_10

    Regards,
    M Shetty

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    mshetty, Sep 8, 2004
    #12
  13. mshetty

    Old Wolf Guest

    (Mike Bland) wrote:
    > (Mike Bland) wrote:
    > > So now I'm even more confused. The Sun compiler seems to be right on
    > > with this one, and GCC is oblivious, which is exactly backwards from
    > > what I might've expected.

    >
    > Whoops, after looking at the GCC info page, I realized that what I
    > need to use is -Woverloaded-virtual. Wonder why it's not
    > automatically a part of -Wall.


    No diagnostic is required by the Standard for the case of a function
    hiding a base function (it's merely a quality-of-implementation issue).

    Also -Wall is unfortunately-named, it should be -Wa-few-common-problems.
    You should probably read the g++ documentation and add in lots more
    -W settings to your Makefile.

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Old Wolf, Sep 9, 2004
    #13
  14. mshetty

    Mike Bland Guest

    (mshetty) wrote in message news:<>...
    > Details of the compiler and system I am using
    > Compiler : CC: Sun WorkShop 6 update 2 C++ 5.3 2001/05/15
    > OS : SunOS pcspsun3 5.7 Generic_106541-34 sun4u sparc SUNW,Ultra-5_10


    Interesting. We're using the same Sun compiler, but I'm on patch
    111685-20 dated 2004/03/19. You're using Solaris 7 and I'm on Solaris
    8. Could be an issue with the compiler patchlevel, too. Try hitting
    http://access1.sun.com/sundev/fdp6u2-patches.html to upgrade your
    compiler patchlevel and see what happens.

    Mike

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Mike Bland, Sep 10, 2004
    #14
  15. mshetty

    Mike Bland Guest

    (Old Wolf) wrote in message news:<>...
    > No diagnostic is required by the Standard for the case of a function
    > hiding a base function (it's merely a quality-of-implementation issue).


    That's kinda surprising. Do you know if there are any plans to add
    such a requirement?

    > Also -Wall is unfortunately-named, it should be -Wa-few-common-problems.
    > You should probably read the g++ documentation and add in lots more
    > -W settings to your Makefile.


    LOL That's how I realized, eventually, that -Wall didn't contain
    -Woverloaded-virtual. I mean, I've known for a long time that -Wall
    didn't really turn on *all* warnings, but I was quite suprised when I
    looked at the info page after my initial posting and realized it
    didn't include this one by default.

    Incidentally, I use a "pedantic" script that precompiles our sources
    with g++ with various warnings explicitly turned on before compiling
    them with Sun CC. It's worked out quite well for shaking out all
    sorts of nasty errors and warnings.

    Mike

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Mike Bland, Sep 10, 2004
    #15
    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. dumboo

    virtual base class

    dumboo, May 26, 2004, in forum: C++
    Replies:
    2
    Views:
    315
    Kench
    May 26, 2004
  2. Chunhui Han
    Replies:
    2
    Views:
    514
  3. Peter Hagenaar

    Templates and virtual base class

    Peter Hagenaar, Sep 15, 2004, in forum: C++
    Replies:
    5
    Views:
    775
    Peter Hagenaar
    Sep 16, 2004
  4. Replies:
    7
    Views:
    383
    Richard
    Aug 28, 2006
  5. Marcel Müller
    Replies:
    2
    Views:
    128
    Marcel Müller
    Mar 15, 2014
Loading...

Share This Page