C :方法中的字符串成员别名
C++: String member alias in method
我的构造函数:
bnf::bnf(string encoded)
{
this->encoded = encoded;
}
将字符串数据复制到成员。(或者是..?)
我将有一个递归解码方法,但希望始终避免编写this->encoded
。
我如何有效地创建对方法中成员的别名/引用?
这是否可以最好地避免开销?
您只需传递其他命名参数即可。这是假设encoded
是您的bnf
类的私有字符串成员
bnf::bnf(string en)
{
encoded = en;
}
在您的其他功能中,如果您不想:
,您仍然不需要编写this
void bnf::printCode(){
cout << encoded << endl;
}
假设您的班级看起来像这样:
class bnf{
public:
bnf(string en};
void printCode();
//<some other functions>
private:
string encoded;
}
您现在正在做的事情没有错。表达,清晰和正确。不要试图破坏它。
如果您担心使用this
指针"开销",则不要:它已经尽可能高效。从字面上看,没有办法更快。
如果您的问题有点错了,您要做的就是提及成员函数中的成员变量,则:
struct MyClass
{
int x;
void myFunction();
};
void MyClass::myFunction()
{
this->x = 4;
}
该功能等效于:
void MyClass::myFunction()
{
x = 4;
}
相关文章:
- 使用 const char* 初始化 const ref 字符串成员时幕后会发生什么
- 具有字符串成员的结构的动态数组
- 为什么在类体中无法启动静态常量字符串成员
- 引用字符串成员变量
- 不能将类模板中使用的对象的字符串成员变量分配给C++
- 在C#中,是否有与INT和字符串成员变量相当的C 联合
- 清理所拥有的(!)字符串成员时,析构函数偶尔崩溃
- C :方法中的字符串成员别名
- 在 C++ 中初始化静态字符串成员
- C++如何访问结构列表的字符串成员
- 如何引用结构字符串成员
- const字符串成员的错误:没有适当的默认构造函数可用
- C 字符串成员函数subtr使用
- 使用字符串成员函数格式化SSN
- C++ vector 中不存在的字符串成员变量
- 如何在 c++ 中的类中声明字符串成员
- 使用函数调用初始化字符串成员c++
- C++ 不能将公共 const 字符串成员作为参数传递给同一类的成员函数
- 类中包含的 STL 字符串成员包含在 std::vector 中
- 如何从 char 数组中提取数字字符串成员