這很奇怪,我試圖找到爲什麼第一次調用Draw
的對象shape
沒有問題,而第二次調用「text」字段上的Draw只在提供名稱空間的前綴時才起作用? (即Shapes::Draw
):命名空間中的C++函數將不起作用?
#include <iostream>
namespace Shapes
{
class Shape {
public:
Shape() {}
virtual void InnerDraw() const {
std::cout << "Shape\n";
}
};
class Square : public Shape {
public:
void InnerDraw() { std::cout << "Square\n"; }
};
void Draw(char* text) { std::cout << text; }
void Draw(const Shape& shape) {
Draw("Inner: ");
shape.InnerDraw();
}
Shape operator+(const Shape& shape1,const Shape& shape2){
return Shape();
}
}
int main() {
const Shapes::Square square;
Shapes::Shape shape(square);
Draw(shape); // No need for Shapes::
Draw("test"); // Not working. Needs Shapes:: prefix
return 0;
}
GoogleKönig-lookup - sorry,Koenig-lookup to be precise – Csq 2013-02-15 23:34:56
[Argument Dependent Name Lookup](http://en.wikipedia.org/wiki/Argument-dependent_name_lookup) – hmjd 2013-02-15 23:36:16
Oh,_that_ call。 – 2013-02-15 23:37:55