#ifndef __USING_STD_NAMES__
using namespace std;
#endif
三 此程式在 G++ 中獲得警告訊息如下:
t1.cpp:36: warning: initialization of non-const reference
'class Rect &' from rvalue 'Rect'
t1.cpp:22: warning: in passing argument 2 of
'operator <<(ostream &, Rect &)'
其道理與修改方法,和上述二相同:將 min() 改名為 mymin() 即可。
心得:程式中不要出現任何命名與 C++ standard library 內的 components 同名。
■Effective C++ 2/e p70
主題:如果 base class 的 operator= 係由編譯器產生,亦即所謂
default operator=,某些編譯器會拒絕讓你明白喚起 base class operator=,
這不是好現象。
測試結果:VC6[o] BCB4[x] G++[o] (BCB4 表現不佳,拒絕我們喚起上述 operator=)
實例:
#001 class Base {
#002 ...
#003 // no defined operator=
#004 };
#005
#006 class Derived : public Base {
#007 ...
#008 Derived& operator=(const Derived& rhs);
#009 };
#010
#011 Derived& Derived::operator=(const Derived& rhs)
#012 {
#013 if (this == &rhs) return *this;
#014
#015 // explicitly invoke Base operator=。VC6[o] BCB4[x] G++[o]
#016 Base::operator=(rhs);
#017 ...
#018 }
以下是 2001/03/03 新增:
■C++ Primer p688
主題:local class
測試結果:VC6 不符 C++ Standard
(01) // VC6[x] CB4[x] G++[x]
(02)
(03) int a, val;
(04)
(05) void foo( int val )
(06) {
(07) static int si;
(08) enum Loc { a = 1024, b };
(09) class Bar {
(10) public:
(11) Loc locVal; // ok;
(12) int barVal;
(13) void fooBar( Loc l = a ) { // ok: Loc::a
(14) barVal = val; // error: local object
(15) barVal = ::val; // ok: global object
(16) barVal = si; // ok: static local object
(17) locVal = b; // ok: enumerator
(18) }
(19) };
(20) // ...
(21) }
(22)
(23) int main()
(24) {
(25) foo(5);
(26) }
編譯結果
[VC6]:
..\688.cpp(16) : error C2065: 'si' : undeclared identifier
..\688.cpp(17) : error C2065: 'b' : undeclared identifier
..\688.cpp(17) : error C2440: '=' : cannot convert from 'int' to
'enum foo::Loc'
Conversion to enumeration type requires an explicit cast (static_cast,
C-style cast or function-style cast)
結論:VC6 不符合 C++ standard.
[BCB4]:
Error E2451 ..\688.CPP 14: Undefined symbol 'val' in function Bar::fooBar(Loc)
結論:BCB4 符合 C++ standard.
[GCC]:
..\\688.cpp: In method `void foo(int)::Bar::fooBar(enum foo(int)::Loc = a)':
..\\688.cpp:14: use of parameter from containing function
..\\688.cpp:6: `int val' declared here
結論:GCC 符合 C++ standard.
歡迎光臨 中央論壇 - CENTER BBS (https://mail.centerbbs.com/)