C++ 是否有识别局部 class 变量的方法?
Does C++ have a way of identifiying local class variables?
在C++中,局部class变量是如何声明的?我是 C++ 新手,但有一些 python 经验。我想知道 C++ classes 是否有识别其局部变量的方法,例如,在 python 中,您的 class' 局部变量标有 self。所以他们会像:
self.variable_name
对于局部变量,C++ 有类似的东西还是有完全不同的东西?在伪代码中,我认为 class' 变量看起来像这样:
class Code:
public:
<some code>
private:
int self.variable
double self.other_variable
<more code>
但是,我可能完全错了。提前致谢。
当你阅读 Effective C++(作者:Scott Meyers)时,成员变量在 ctor 初始化时是 init。 ctor之后,所有赋值都是assignment,不是init。你可以这样写ctor。
Circle(double value, bool isTrueFalse, <More Variables>) : class.variable(value), class.othervariable(isTrueFalse), ..<More Variables> //this is init.
{
class.variable = value; //this is assignment. not init.
}
C++ 初始化顺序是颠倒的,而不是 ctor 初始化顺序。
private:
double class.variable; //First init;
bool class.variables;//Second init;
如果你想要局部变量 init,你将值传递给 ctor。
在 C++ 中。 assignment 和 init 是不同的。 class 本地成员仅在 ctor Initializer 处初始化。 init 比赋值快。因为 init 只是一个调用 ctor,然后结束。但是赋值是调用ctor,而赋值操作符又是一个。你应该使用 ctor Initializer 来执行。
非常接近!然而,在 class 中,人们会提到 class 变量只是使用它们自己的名称,因此是 variable
而不是 class.variable
。
(另外,请注意,您的函数后面需要有一个分号,并且按照惯例,这些函数往往在 class 本身或单独的文件中定义)
class Circle {
public:
Circle(double var, bool otherVar); //constructor
double getVariable(); //getter
void setVariable(double var); //setter
// you can put more functions here
private:
double variable;
bool otherVariable;
//you can put more functions here
};
Circle::Circle(double var, bool otherVar){
variable = var;
otherVariable = otherVar;
}
Circle::getVariable(){
return variable;
}
Circle::setVariable(double var){
variable = var
}
为了更好地了解这个主题,请查看此 similar question/answer.,或者如评论中所述,考虑阅读有关 C++ 的教科书...
编辑:我根据 "identifying" 变量的问题标题写了这个答案,注意到问题很可能是代码无法编译,因为 class.variable
不是事物的引用方式在 C++ 中。但是,我不确定问题是否涉及初始化、声明等。
在C++中,局部class变量是如何声明的?我是 C++ 新手,但有一些 python 经验。我想知道 C++ classes 是否有识别其局部变量的方法,例如,在 python 中,您的 class' 局部变量标有 self。所以他们会像:
self.variable_name
对于局部变量,C++ 有类似的东西还是有完全不同的东西?在伪代码中,我认为 class' 变量看起来像这样:
class Code:
public:
<some code>
private:
int self.variable
double self.other_variable
<more code>
但是,我可能完全错了。提前致谢。
当你阅读 Effective C++(作者:Scott Meyers)时,成员变量在 ctor 初始化时是 init。 ctor之后,所有赋值都是assignment,不是init。你可以这样写ctor。
Circle(double value, bool isTrueFalse, <More Variables>) : class.variable(value), class.othervariable(isTrueFalse), ..<More Variables> //this is init.
{
class.variable = value; //this is assignment. not init.
}
C++ 初始化顺序是颠倒的,而不是 ctor 初始化顺序。
private:
double class.variable; //First init;
bool class.variables;//Second init;
如果你想要局部变量 init,你将值传递给 ctor。
在 C++ 中。 assignment 和 init 是不同的。 class 本地成员仅在 ctor Initializer 处初始化。 init 比赋值快。因为 init 只是一个调用 ctor,然后结束。但是赋值是调用ctor,而赋值操作符又是一个。你应该使用 ctor Initializer 来执行。
非常接近!然而,在 class 中,人们会提到 class 变量只是使用它们自己的名称,因此是 variable
而不是 class.variable
。
(另外,请注意,您的函数后面需要有一个分号,并且按照惯例,这些函数往往在 class 本身或单独的文件中定义)
class Circle {
public:
Circle(double var, bool otherVar); //constructor
double getVariable(); //getter
void setVariable(double var); //setter
// you can put more functions here
private:
double variable;
bool otherVariable;
//you can put more functions here
};
Circle::Circle(double var, bool otherVar){
variable = var;
otherVariable = otherVar;
}
Circle::getVariable(){
return variable;
}
Circle::setVariable(double var){
variable = var
}
为了更好地了解这个主题,请查看此 similar question/answer.,或者如评论中所述,考虑阅读有关 C++ 的教科书...
编辑:我根据 "identifying" 变量的问题标题写了这个答案,注意到问题很可能是代码无法编译,因为 class.variable
不是事物的引用方式在 C++ 中。但是,我不确定问题是否涉及初始化、声明等。