有没有一种方法可以将方法范围分组到特定的类,而无需每次都使用范围操作符操作符?在某些情况下可能会引起蔑视,我可以粗略地类比JavaScript with
声明;然而,在这里它被用在源代码中,并没有被执行。C++范围运算符分组?
简化示例:想象一个Cheese
类,与weigh
,shred
,和melt
功能声明如下:
class Cheese {
public:
Cheese();
... // other constructors, copy, etc.
~Cheese();
int weigh();
int shred();
int melt();
}
典型地,该功能定义如下:
Cheese::Cheese() { //constructor stuff };
... // other constructors, copy, etc.
Cheese::~Cheese() { //destructor stuff };
int Cheese::weigh() { return weighed; }
int Cheese::shred() { return shredded; }
int Cheese::melt() { return melted; }
是否有一种说法,“嘿编译器,所有这些定义的范围是Cheese
类。”
也许是这样?
scope::Cheese {
Cheese() { //constructor stuff };
... // other constructors, copy, etc.
~Cheese() { //destructor stuff };
int weigh() { return weighed; }
int shred() { return shredded; }
int melt() { return melted; }
}
,或者
Cheese:: {
Cheese() { //constructor stuff };
... // other constructors, copy, etc.
~Cheese() { //destructor stuff };
int weigh() { return weighed; }
int shred() { return shredded; }
int melt() { return melted; }
}
不,不知道如何处理模板奶酪,即使没有。 – xaxxon
对于它的价值,我认为这是C++中最愚蠢/令人讨厌的方面之一。 –
@xaxxon为什么模板会成为问题? –