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
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.
(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
Try compiling with -Woverloaded-virtual (C++ only). That catches the situation you describe. (I don't think it's enabled by -Wall or -Wextra.)
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. >
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.