有没有一种方法可以分组特定类的作用域方法,而不必每次都使用作用域运算符::?冒险在某些人中引起轻蔑,我可以将其粗略地类比为JavaScript的with语句;但是,这里它用于源代码,而不是执行。
一个简化的例子:想象一个Cheese类,其中weigh、shred和melt函数声明如下:
通常,函数定义如下所示:
或者,
一个简化的例子:想象一个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; }
}
kjm- kmiklas