re: 猜猜看,id變成9了嗎? foobar 2008-04-02 16:16
@夢在天涯
這個有道理,有人驗證過嗎?
re: c++晦澀語法背后的故事(一) foobar 2007-11-15 23:00
@愛上龍卷風
汗
不要感謝foobar,還是感謝google和c++編程思想吧。。。
re: c++晦澀語法背后的故事(一) foobar 2007-11-11 17:10
@lovedday
多態是c++的一個特性 但是 c++遠比這個復雜
你看看下面的代碼,希望能明白我的意思
#include <iostream>
class Base
{
public:
virtual void foo() { std::cout << "Base::foo\n"; }
void foo(int) { std::cout << "Base::foo(int)\n"; }
};
class Derived : public Base
{
public:
void foo() { std::cout << "Derived::foo\n"; }
};
int main()
{
Base b;
b.foo(); // Base's foo() is called
b.foo(3); // Base's foo(int) is called
Derived d;
d.foo(); // Derived's foo() is called. The one in Base is hidden
d.foo(3); // error, that function is also hidden
Base &ref = d;
ref.foo(); // Derived's foo() is called. It overrides the Base version
ref.foo(3); // Base's foo(int) is called
}
re: c++晦澀語法背后的故事(一) foobar 2007-11-11 17:07
@lovedday
上面說的不夠確切
你c++學的好,知道多態這個概念,但是c++有很多缺點
樓主的那個例子,你在理解上是有誤的
子類的確實用函數簽名hide了父類的函數
所以你調用f的時候,肯定調用了f(double)
但是用dynamic type的時候,情況就不是這樣了
nt main()
{
Base* pb = new Derived;
pb->f(10.5);
return 0;
}
輸出當然是base
但是絕對不是子類有個函數輸出base
而確實調用了父類的函數
re: c++晦澀語法背后的故事(一) foobar 2007-11-11 16:47
@lovedday
pb是個父類指針,所以調用父類的f
Derived* pb = new Derived;
pb->f(10);
re: c++晦澀語法背后的故事(一) foobar 2007-11-11 16:20
@lovedday
class Base
{
public:virtual void f(int x);
};
class Derived: public Base
{
public:virtual void f(double* pd);
};
int main()
{
Derived* pd = new Derived();
pd->f(10); //compile error!!!
}
這個例子只有聲明沒有定義,編譯肯定有錯
但是
在c++里,子類通過函數名字隱藏父類函數,而不是通過函數簽名!
這個分析應該沒什么問題
子類確實通過函數名字隱藏了父類的函數
re: msdn也有錯? foobar 2007-06-04 11:52
@Mickey Mouse
應該不是const int吧
10 20 30 都是臨時變量
re: msdn也有錯? foobar 2007-06-04 11:22
@Mickey Mouse
vector 中存放const沒有意義?
vector中沒有const變量吧
re: msdn也有錯? foobar 2007-06-04 11:15
@Mickey Mouse
我是感覺msdn在stl方面不如其他文檔好
re: msdn也有錯? foobar 2007-06-04 11:09
看stl的話,就是到cppreference也比到msdn好吧