<分区>
Possible Duplicate:
Why is my log in the std namespace?
基于 Overload a C++ function according to the return value ,我做了以下实验:
#include <cmath>
class myType
{
private:
double value;
myType(double value) : value(value) {}
public:
myType& operator= (const myType& other) {
if (this != &other) value = other.value;
return *this;
}
static myType test(double val) { return myType(val); }
friend std::ostream& operator<<(std::ostream& target, const myType& A);
};
std::ostream& operator<<(std::ostream& target, const myType& A){
target << A.value;
return target;
}
class asin {
private:
double value;
public:
asin(double value)
: value(std::asin( (value<-1.0) ? -1.0 : (value>1.0?1.0:value) ))
{}
operator double() { return value; }
operator myType() { return myType::test(value); }
};
int main(int argc, char *argv[])
{
myType d = asin(1.0);
std::cout << d << std::endl;
return 0;
}
结果是
error: ‘myType::myType(double)’ is private
在 main()
的第一行。更多的实验表明,当我将类名 asin
更改为 Asin
(或与此相关的任何其他内容)时,这工作正常并且符合预期。显然,我不允许调用我的类 asin
,而定义它(而不是使用它)的行为不会给我任何警告/错误。
现在我知道所有这些都是不好的做法,所以不要因此而责备我。我问这个纯粹是出于学术兴趣:为什么我不能称我的类为 asin
、acos
或 atan
或类似的名称?我的印象是 cmath
隐藏了 std
命名空间中的所有内容,因此在全局命名空间中定义此类不会引起这个特殊问题。
谁能解释一下这是怎么回事?