那是输入还是输入/输出参数?Doxygen,C++

aga*_*anm 4 documentation doxygen inout

如果将指针传递给只读函数,则该指针是 IN 参数。

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

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

我的意思的例子:

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;
}
Run Code Online (Sandbox Code Playgroud)

Bob*_*ane 14

我相信inout说明符并不完全代表您的想法。从标签doxygen 文档中param

\param 命令有一个可选属性 (dir),用于指定参数的方向。可能的值为“[in]”、“[in,out]”和“[out]”,请注意本说明中的 [方] 括号。当参数既是输入又是输出时,[in,out] 用作属性。

参数方向通常表示如下:

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

在你的例子中:

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

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

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

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

请注意,我们将新值直接写入参数中。这就是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;
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!