代码之家  ›  专栏  ›  技术社区  ›  Xzhsh

为什么这个函数没有被编译器捕获,它做了什么?

  •  0
  • Xzhsh  · 技术社区  · 16 年前

    我发现在一些遗留代码中,我正在处理这个函数(C++)

    Vec3d Minimum()
    {
        if(this->valid)
        {
            return minBB;
        }
        else
        {
            return NULL;
        }
    }
    

    好吧,你不能为用户定义的对象返回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();
    };
    
    1 回复  |  直到 16 年前
        1
  •  5
  •   GManNickG    16 年前

    0可以在指针上下文中用作空指针常量。也就是说,它进入了这里:

    Vec3d(Vec3d* v); 
    

    注意注释是不正确的 复制构造函数。

    set 函数,通常非变异运算符应该是自由函数。最重要的是,拥有这样一个构造函数是一种浪费和混乱。如果有指向向量的指针,则应执行以下操作:

    Vec3d v = *other;