Xzh*_*hsh 0 c++ casting compiler-warnings
我在一些遗留代码中发现我正在处理这个函数(在C++中)
Vec3d Minimum()
{
if(this->valid)
{
return minBB;
}
else
{
return NULL;
}
}
Run Code Online (Sandbox Code Playgroud)
其中Vec3d是一个对象,它基本上是一个带有x,y,z和一些运算符重载的结构(下面的代码).
AFAIK,你不能为用户定义的对象返回0 ...或者是否有一些我不知道的自动转换为零?这只是出于好奇:p
谢谢
class Vec3d
{
public:
double x,y,z;
/// \brief Default constructor initializes x and y to 0
Vec3d();
/** \brief Constructor initializies vector to input parameters x and y and z
*
* \param x Double value that initializes x value of vector
* \param y Double value that initializes y value of vector
* \param z Double value that initializes z value of vector
*/
Vec3d(double x, double y, double z);
/** \brief Copy constructor
*
* \param v Pointer to another vec3i with which to initialize current vec3i
*/
Vec3d(Vec3d* v);
/**\brief Sets a vector (already instantiated) to the input parameters (x,y,z)
*
* \param x Double value that initializes x value of vector
* \param y Double value that initializes y value of vector
* \param z Double value that initializes z value of vector
*
* This method is just so you can change the value of an already instantiated vector
*/
void set(double xi, double yi, double zi);
const Vec3d operator -(const Vec3d &other) const;
const Vec3d operator +(const Vec3d &other) const;
const Vec3d operator *(const double num) const;
const double operator *(const Vec3d &other) const;
const Vec3d operator /(const double num) const;
double magnitude();
};
Run Code Online (Sandbox Code Playgroud)
0可以在指针的上下文中用作空指针常量.也就是说,它会进入这里:
Vec3d(Vec3d* v);
Run Code Online (Sandbox Code Playgroud)
请注意注释不正确,因为它不是复制构造函数.
代码有点粗制滥造.不需要是set函数,通常非变异运算符应该是自由函数.而且最重要的是,拥有像它这样的构造函数是一种浪费和混乱.如果你有一个指向矢量的指针,你应该这样做:
Vec3d v = *other;
Run Code Online (Sandbox Code Playgroud)
不提供指针的隐式转换.
| 归档时间: |
|
| 查看次数: |
263 次 |
| 最近记录: |