Bug 32934 - No warning when creating a non const derived function from a const virtual function
Summary: No warning when creating a non const derived function from a const virtual fu...
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.0.3
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-07-30 01:48 UTC by Andrew
Modified: 2007-07-31 21:49 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew 2007-07-30 01:48:14 UTC
Maybe this isn't a bug, but I can't see why this shouldn't at least be a warning since it changes the output of the program...


==============C++ Source with B.print not const====================
#include <iostream>
class A {
public: 
    virtual char * print() const { return "A\n";}
    virtual ~A(){};
};
class B : public A {
public:
    virtual char * print() { return "B\n";}
    virtual ~B(){};
};

int main ()
{    
    B b;
    A & a = b;
    std::cout << a.print() << b.print();
}

aewhite@aewhite-laptop:~/working$ g++ -Wall -o gcc_bug gcc_bug.cpp
aewhite@aewhite-laptop:~/working$ ./gcc_bug
A
B

=================C++ Source with B.print const=================

#include <iostream>
class A {
public: 
    virtual char * print() const { return "A\n";}
    virtual ~A(){};
};
class B : public A {
public:
    virtual char * print() const { return "B\n";}
    virtual ~B(){};
};

int main ()
{    
    B b;
    A & a = b;
    std::cout << a.print() << b.print();
}

aewhite@aewhite-laptop:~/working$ g++ -Wall -o gcc_bug gcc_bug.cpp
aewhite@aewhite-laptop:~/working$ ./gcc_bug
B
B

What am I missing?

Andrew
Comment 1 Andrew Pinski 2007-07-30 02:19:32 UTC
Well it is valid as B::print hides A::print.
Try doing:
#include <iostream>
class A {
public:
    virtual char * print() const { return "A\n";}
    virtual ~A(){};
};
class B : public A {
public:
    virtual char * print() { return "B\n";}
    using A::print;
    virtual ~B(){};
};

int main ()
{
    B b;
    A & a = b;
    const B &b1 = b;
    std::cout << a.print() << b1.print();
}

Which shows that b1.print will call A::print.  If you don't include using A::print, then A::print wil be hidden in B.
Comment 2 Andrew 2007-07-30 12:04:59 UTC
(In reply to comment #1)
> Well it is valid as B::print hides A::print.
> Try doing:
> #include <iostream>
> class A {
> public:
>     virtual char * print() const { return "A\n";}
>     virtual ~A(){};
> };
> class B : public A {
> public:
>     virtual char * print() { return "B\n";}
>     using A::print;
>     virtual ~B(){};
> };
> 
> int main ()
> {
>     B b;
>     A & a = b;
>     const B &b1 = b;
>     std::cout << a.print() << b1.print();
> }
> 
> Which shows that b1.print will call A::print.  If you don't include using
> A::print, then A::print wil be hidden in B.
> 

The problem I am seeing is that I want to inherit from a virtual const function but I don't want MY derived function to be const. Just because my parent class thought the member should be const doesn't mean I agree, or am I simply restricted? 

Thanks, and again I am sorry if this is simply a lack of my understanding of C++.

Andrew
Comment 3 David Fang 2007-07-31 21:32:24 UTC
Try compiling with -Woverloaded-virtual (C++ only).  That catches the situation you describe.  (I don't think it's enabled by -Wall or -Wextra.)
Comment 4 Andrew 2007-07-31 21:41:00 UTC
Subject: Re:  No warning when creating a non const derived function from a const virtual function

Wow, thanks. I thought that -Wall was ALL warnings. grumble grumble...

Andrew

On 31 Jul 2007 21:32:32 -0000, fang at csl dot cornell dot edu
<gcc-bugzilla@gcc.gnu.org> wrote:
>
>
> ------- Comment #3 from fang at csl dot cornell dot edu  2007-07-31 21:32 -------
> Try compiling with -Woverloaded-virtual (C++ only).  That catches the situation
> you describe.  (I don't think it's enabled by -Wall or -Wextra.)
>
>
> --
>
> fang at csl dot cornell dot edu changed:
>
>            What    |Removed                     |Added
> ----------------------------------------------------------------------------
>                  CC|                            |fang at csl dot cornell dot
>                    |                            |edu
>
>
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32934
>
> ------- You are receiving this mail because: -------
> You reported the bug, or are watching the reporter.
>
Comment 5 Andrew Pinski 2007-07-31 21:49:22 UTC
http://gcc.gnu.org/onlinedocs/gcc-4.2.1/gcc/Warning-Options.html
-Wall
    All of the above `-W' options combined. This enables all the warnings about constructions that some users consider questionable, and that are easy to avoid (or modify to prevent the warning), even in conjunction with macros. This also enables some language-specific warnings described in C++ Dialect Options and Objective-C and Objective-C++ Dialect Options.

And:
http://gcc.gnu.org/onlinedocs/gcc-4.2.1/gcc/C_002b_002b-Dialect-Options.html
Under:
The following -W... options are not affected by -Wall. 
....

-Woverloaded-virtual (C++ only)
    Warn when a function declaration hides virtual functions from a base class. For example, in:

          struct A {
            virtual void f();
          };
          
          struct B: public A {
            void f(int);
          };
     

    the A class version of f is hidden in B, and code like:

          B* b;
          b->f();
     

    will fail to compile.