[英]What do you call a member function that follows a member function and how do I write one?
What do you call function that follows a member function and modifies the return value and how do I write one? 您在成员函数之后调用函数并修改返回值以及如何编写函数?
In other words how do I successfully write: 换句话说,我怎么写成功:
std::cout << box.getVolume().inCubicFeet();
std::cout << box.getVolume().inCubicCentimeters();
For that to work getVolume()
needs to return an object of type Volume
( or even a reference to an object of type Volume &
), so that whatever method follows you are able to invoke it on said object. 为了实现这一点,
getVolume()
需要返回一个Volume
类型的对象(甚至是对Volume &
类型的对象的引用),这样无论你遵循什么方法,你都可以在所述对象上调用它。 For instance: 例如:
class Volume{
...
int inCubicFeet() const {
//convert it and return it
}
int inCubicCentimeters() const {
//convert it and return it
}
};
class Box{
Volume v; //volume object that is initialized somewhere
//(either in the constructor of Box or in a method like setVolume)
...
Volume const& getVolume() const {
return v;
}
};
box.getVolume()
needs to return an object of a class
for which a function inCubicFeet()
is defined which returns a value for which std::ostream
has an overloaded <<
operator (although you are allowed to define your own overloads). box.getVolume()
需要返回一个class
的对象 ,其中定义了一个函数inCubicFeet()
,该函数返回一个值, std::ostream
有一个重载的<<
运算符(尽管你可以定义自己的重载)。 For that second part, a double
would suffice. 对于第二部分,
double
就足够了。
It is called "member function of the return type". 它被称为“返回类型的成员函数”。 There is nothing special about those methods.
这些方法没有什么特别之处。 The code could be written like this:
代码可以这样写:
const Volume& v = box.getVolume(); // my guess on what the return type is
std::cout << v.inCubicFeet();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.