如果将指针传递给只读函数,则该指针是 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;
}
最佳答案
我相信 in
和 out
说明符并不完全符合您的想法。来自 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
:参数作为输入注入(inject)到函数中,但不写入。out
:参数被注入(inject)到函数中,但不作为输入。相反,它是由函数写入的。in, out
:参数作为输入注入(inject)到函数中,并最终由函数写入。
在您的示例中:
/**
* @param[?] steeringWheel Is the steering wheel in or in/out?
*/
Car (SteeringWheel *steeringWheel) {
this->steeringWheel = steeringWheel;
}
我认为 steeringWheel
参数是 in
因为您注入(inject)它并在您的方法中使用它。但是,您永远不会写入它(即参数本身),因此它不是out
。换句话说,您只使用您的方法将地址注入(inject)到您的函数中,仅此而已。这同样适用于您的第二种方法,您在其中注入(inject) Degree
参数,但从不写入它。
为了进一步阐明 in
和 out
的含义,下面是 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;
}
希望这有帮助!
关于documentation - 这是一个 in 或 in/out 参数吗? Doxygen,C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47732665/