那是 in 还是 in/out 参数?吸氧剂,C++

Is that an in or in/out parameter? Doxygen, C++

如果一个指针被传递给一个只读的函数,那么这个指针就是一个 IN 参数。

如果一个指针被传递给一个只读函数,但是这个函数复制了一个指针以便在模块相关函数中访问它以进行只读操作,这个指针仍然是IN。

如果函数仍然使用只读指针,但其他模块相关函数使用指针进行写操作,那么指针是什么? 一个 IN 参数,但没有 const?一个 in/out 参数?

我的意思的例子:

class SteeringWheel {
        public: float rotation;
        public: SteeringWheel(void) {
                this->rotation = 0.f;
        }
};

class Car {
        private: SteeringWheel *steeringWheel;
        public:

        /**
         * @param[?] steeringWheel Is the steering wheel in or in/out? 
         */
        Car (SteeringWheel *steeringWheel) {
                this->steeringWheel = steeringWheel;
        }

        /**
         * @param[in] degree Steering amount in degrees.
         */
        void steer(float degree) 
        {
                this->steeringWheel->rotation += degree;
        }
};

int main(int argc, char **argv)
{
        SteeringWheel steeringWheel();

        /* car() uses steeringWheel as read only. */
        Car car(&steeringWheel);

        /* steer() uses steeringWheel from car() to write. */
        car.steer(50.f);

        return 0;
}

我认为 inout 说明符并不完全符合您的想法。来自 doxygen documentation of the param tag:

The \param command has an optional attribute, (dir), specifying the direction of the parameter. Possible values are "[in]", "[in,out]", and "[out]", note the [square] brackets in this description. When a parameter is both input and output, [in,out] is used as attribute.

参数的方向通常表示如下:

  • in:参数作为输入注入到函数中,但没有写入。
  • out:参数被注入到函数中,但不作为输入。相反,它是由函数写入的。
  • in, out:参数作为输入注入到函数中,最终被函数写入。

在你的例子中:

/**
* @param[?] steeringWheel Is the steering wheel in or in/out? 
*/
Car (SteeringWheel *steeringWheel) {
    this->steeringWheel = steeringWheel;
}

我认为 steeringWheel 参数是 in 因为你注入它并在你的方法中使用它。但是,您永远不会写入它(即写入参数本身),所以它不是 out。换句话说,你只使用你的方法来为你的函数注入一个地址,没有别的。这同样适用于你的第二种方法,你在其中注入 degree 参数,但从不写入它。

为了进一步阐明 inout 的含义,这里有一个 out 参数的示例:

/**
 * @param[out] p_param We write to the parameter!
 */
void makeFour(int * p_param)
{
    *p_param = 4; // Out because of this line!
}

请注意,我们直接将新值写入参数。这就是out的意思:信息通过参数从方法中出来。你现在可以写:

int main()
{
    int myInt = 0;
    std::cout << myInt;    // prints 0.

    makeFour(&myInt); // p_param == &myInt here.
    std::cout << myInt;    // prints 4: the method wrote directly 
                           // in the parameter (out)!

    return 0;
}

希望对您有所帮助!

很难决定,但我还是会把你的参数标记为in,out(或out),因为它是一个指向非常量对象的指针,而你可能以后直接或间接地改变那个外部对象的状态——就像你的例子一样。

标记它 in 隐藏了指向的 SteeringWheel 对象可能会在使用 Car.

后更改的细节

此外,它会让用户感到困惑,为什么一个仅供输入的指针参数没有被标记为 const

使其in,out可能不完全准确,但肯定更容易出错。

替代方案可能如下所示(无论如何,关于 SteeringWheel 生命周期的说明在这里应该派上用场):

    /**
     * @param[in] steeringWheel Pointer to the SteeringWheel object.
     * @warning The memory address of the pointed object is saved.
     * It must outlive this object, and can change upon usage of this object.
     */
    Car (SteeringWheel *steeringWheel) {
            this->steeringWheel = steeringWheel;
    }

但我可能会坚持标记它 in,out

在C++中指定参数的方向可能会很复杂,坦率地说,我不太赞成它们,因为指针、引用的标记和constness的关键字在签名中提供了足够的信息如何使用参数。因此,在 DoxyPress 文档中标记它有点多余,表现力不够(如您的示例所示),并且可能与实现不同步。如果其他语言在函数签名中缺少这些附加结构,那么记录参数方向可能会发挥更大的作用。