// 1 filename:cpp2011-10-3.cpp // ver 0.1 June.15, 2014 // // 2 original examples and/or notes: // (c) ISO/IEC JTC1 SC22 WG21 N3242, April 12, 2011 // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf // > 10 Derived classes 10.3 Virtual functions // // 3 compile and output mechanism: // (c) Dr. OGAWA Kiyoshi, kaizen at gifu-u.ac.jp, // // 4 compile errors and/or warnings: // 4.1(c) Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn) // Target: x86_64-apple-darwin13.2.0, Thread model: posix // Command/Options: c++ -std=c++11 -stdlib=libc++ -Wall cpp2011-10-3.cpp // (c) LLVM 2003-2009 University of Illinois at Urbana-Champaign. // 4.2. g++-4.9 (GCC) 4.9.0 20131229 (experimental) // Copyright (C) 2013 Free Software Foundation, Inc. // This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. // http://gcc.gnu.org/onlinedocs/gcc/Standards.html // Command/Options: g++-4.9 -std=c++11 -Wall cpp2011-10-3.cpp // g++-4.9: error: unrecognized command line option '-stdlib=libc++' // Configuration:brew install gcc49 // // 4.3. Visual Studio Express 2013, // (c) Microsoft http://www.visualstudio.com/ // SPEC: // Windows 7, .NET Framework // (c) VMware, Inc. // VMWare fusion 6 // // 5. Hardware: MacBook Pro, //(c) Intel http://ark.intel.com/products/37006/ //Core 2 Duo 2.53GHz, 8GB, 1067MHz DDR3 // // 6. Special Thanks: Upper organizatios and // ITSCJ/IPSJ http://www.itscj.ipsj.or.jp/itscj_english/index.html // Renesas Electronics Corporation.http://www.renesas.com/ // NPO SESSAME project, http://www.sessame.jp/workinggroup/WorkingGroup3/ // Toyo Corporation, http://www.toyo.co.jp/English/ // Japan Standard Association, http://bit.ly/1lzykg1 // NPO TOPPERS project, https://www.toppers.jp/asp-d-download.html // Daido Universcity, http://www.daido-it.ac.jp/gakubugakka/computer/index.html // WITZ Co.Ltd., http://www.witz-inc.co.jp/products/solution/solution.html // SevenWise.co., http://www.7ws.co.jp/index.html // TOYOTA Motor Corporation, http://toyota.jp/ // IT planning Inc., http://www.itpl.co.jp/en/index.html // DENSO Corporation, http://www.globaldenso.com/en/ // Aisin Seiki co. Ltd., http://www.aisin.com/ // Spancion Inc., http://www.spansion.com/ // Yazaki Corporation, http://www.yazaki-group.com/global/ // Pananosic Corporation, http://www.panasonic.net/ // SWEST: Summer Workshop on Embedded System Technologies , http://swest.toppers.jp // CEST: Consortium for Embedded System Technology, http://www.ertl.jp/CEST/ // JUSE: Union of Japanese Scientists and Engineers, http://www.juse.or.jp/e/ // OSC:Open Source Conference, http://www.ospn.jp/ #include //#include //#include //#include //#include //#include //#include //#include //#include //#include //#include //#include using namespace std; struct A { virtual void f(); }; struct B : virtual A { virtual void f(); }; struct C : B , virtual A { using A::f; }; void foo() { C c; c.f(); // calls B::f, the final overrider c.C::f(); // calls A::f because of the using-declaration } // struct A1 { virtual void f(); }; struct B1 : A1 { }; struct C1 : A1 { void f(); }; struct D1 : B1, C1 { }; // OK: A::f and C::f are the final overriders // for the B and C subobjects, respectively // struct B0 { virtual void f(); }; struct D0 : B0 { void f(int); }; struct D2 : D0 { void f(); }; // struct B3 { virtual void f() const final; }; struct D3 : B3 { //error: void f() const; // error: D::f attempts to override final B::f }; // struct B4 { virtual void f(); }; struct D4 : B4 { //error: void f(long) override; // error: wrong signature overriding B::f void f(int) override; // OK }; // class B5 { }; class D5 : private B5 { friend class Derived; }; struct Base { virtual void vf1(); virtual void vf2(); virtual void vf3(); virtual B5* vf4(); virtual B5* vf5(); void f(); }; struct No_good : public Base { //error: D5* vf4(); // error: B (base class of D) inaccessible }; class A5; struct Derived : public Base { void vf1(); // virtual and overrides Base::vf1() void vf2(int); // not virtual, hides Base::vf2() //error: char vf3(); // error: invalid difference in return type only D5* vf4(); // OK: returns pointer to derived class //error: A5* vf5(); // error: returns pointer to incomplete class void f(); }; void g() { Derived d; Base* bp = &d; // standard conversion: // Derived* to Base* bp->vf1(); // calls Derived::vf1() bp->vf2(); // calls Base::vf2() bp->f(); // calls Base::f() (not virtual) B5* p = bp->vf4(); // calls Derived::pf() and converts the // result to B* Derived* dp = &d; D5* q = dp->vf4(); // calls Derived::pf() and does not // convert the result to B* //error: dp->vf2(); // ill-formed: argument mismatch } // struct A6 { virtual void f(); }; struct B6 : A6 { // note non-virtual derivation void f(); }; struct B7 : A6 { void f(); }; struct D6 : B6, B7 { // D has two separate A subobjects }; void foo2() { D6 d; // A* ap = &d; // would be ill-formed: ambiguous B6* b1p = &d; A6* ap = b1p; D6* dp = &d; ap->f(); // calls D::B1::f //error: dp->f(); // ill-formed: ambiguous } // struct A9 { virtual void f(); }; struct VB1 : virtual A9 { // note virtual derivation void f(); }; struct VB2 : virtual A9 { void f(); }; //error: struct Error : VB1, VB2 { // ill-formed}; struct Okay : VB1, VB2 { void f(); }; // struct VB1a : virtual A9 { // does not declare f }; struct Da : VB1a, VB2 { }; void foe() { VB1a* vb1ap = new Da; vb1ap->f(); // calls VB2::f } // class B9 { public: virtual void f(); }; class D9 : public B9 { public: void f(); }; void D9::f() { /* ... */ B9::f(); } int main() { cout << "10 Derived classes 10.3 Virtual functions" << std::endl; return 0; } // 1 error // 1.1 llvm: c++ -std=c++11 -stdlib=libc++ -Wall cpp2011-10-3.cpp cpp2011-10-3.cpp:99:6: warning: 'D0::f' hides overloaded virtual function [-Woverloaded-virtual] void f(int); ^ cpp2011-10-3.cpp:96:14: note: hidden overloaded virtual function 'B0::f' declared here: different number of parameters (0 vs 1) virtual void f(); ^ cpp2011-10-3.cpp:118:13: error: non-virtual member function marked 'override' hides virtual member function void f(int) override; // OK ^ cpp2011-10-3.cpp:114:14: note: hidden overloaded virtual function 'B4::f' declared here: different number of parameters (0 vs 1) virtual void f(); ^ cpp2011-10-3.cpp:135:1: warning: 'Derived' defined as a struct here but previously declared as a class [-Wmismatched-tags] struct Derived : public Base { ^ cpp2011-10-3.cpp:122:32: note: did you mean struct here? class D5 : private B5 { friend class Derived; }; ^~~~~ struct cpp2011-10-3.cpp:137:6: warning: 'Derived::vf2' hides overloaded virtual function [-Woverloaded-virtual] void vf2(int); // not virtual, hides Base::vf2() ^ cpp2011-10-3.cpp:125:14: note: hidden overloaded virtual function 'Base::vf2' declared here: different number of parameters (0 vs 1) virtual void vf2(); ^ cpp2011-10-3.cpp:155:5: error: too few arguments to function call, expected 1, have 0; did you mean 'Base::vf2'? dp->vf2(); // ill-formed: argument mismatch ~~~~^~~ Base::vf2 cpp2011-10-3.cpp:125:14: note: 'Base::vf2' declared here virtual void vf2(); ^ cpp2011-10-3.cpp:174:5: warning: unused variable 'dp' [-Wunused-variable] D6* dp = &d; ^ >4 warnings and 2 errors generated. // 1.2 gcc: g++-4.9 -std=c++11 -Wall cpp2011-10-3.cpp cpp2011-10-3.cpp:118:6: error: 'void D4::f(int)' marked override, but does not override void f(int) override; // OK ^ cpp2011-10-3.cpp: In function 'void g()': cpp2011-10-3.cpp:155:9: error: no matching function for call to 'Derived::vf2()' dp->vf2(); // ill-formed: argument mismatch ^ cpp2011-10-3.cpp:155:9: note: candidate is: cpp2011-10-3.cpp:137:6: note: void Derived::vf2(int) void vf2(int); // not virtual, hides Base::vf2() ^ cpp2011-10-3.cpp:137:6: note: candidate expects 1 argument, 0 provided cpp2011-10-3.cpp:150:5: warning: unused variable 'p' [-Wunused-variable] B5* p = bp->vf4(); // calls Derived::pf() and converts the ^ cpp2011-10-3.cpp:153:5: warning: unused variable 'q' [-Wunused-variable] D5* q = dp->vf4(); // calls Derived::pf() and does not ^ cpp2011-10-3.cpp: In function 'void foo2()': cpp2011-10-3.cpp:174:5: warning: unused variable 'dp' [-Wunused-variable] D6* dp = &d; ^