在C 中,在此上下文中使用``''
When to use `this` in this context in C++
本文关键字:上下文 更新时间:2023-10-16
我试图修改面向对象的编程概念。在此处浏览此基本C 示例时,我发现在为成员变量设置值时尚未使用this->
关键字。然后,我修改了此程序以使用this
关键字。令人惊讶的是,这两个工作(这个和这个)。
#include <iostream> // for cout and cin
class Cat // begin declaration of the class
{
public: // begin public section
Cat(int initialAge); // constructor
Cat(const Cat& copy_from); //copy constructor
Cat& operator=(const Cat& copy_from); //copy assignment
~Cat(); // destructor
int GetAge() const; // accessor function
void SetAge(int age); // accessor function
void Meow();
private: // begin private section
int itsAge; // member variable
char * string;
};
// constructor of Cat,
Cat::Cat(int initialAge)
{
itsAge = initialAge;
string = new char[10]();
}
//copy constructor for making a new copy of a Cat
Cat::Cat(const Cat& copy_from) {
itsAge = copy_from.itsAge;
string = new char[10]();
std::copy(copy_from.string+0, copy_from.string+10, string);
}
//copy assignment for assigning a value from one Cat to another
Cat& Cat::operator=(const Cat& copy_from) {
itsAge = copy_from.itsAge;
std::copy(copy_from.string+0, copy_from.string+10, string);
}
Cat::~Cat() // destructor, just an example
{
delete[] string;
}
// GetAge, Public accessor function
// returns value of itsAge member
int Cat::GetAge() const
{
return itsAge;
}
// Definition of SetAge, public
// accessor function
void Cat::SetAge(int age)
{
// set member variable its age to
// value passed in by parameter age
itsAge = age;
}
// definition of Meow method
// returns: void
// parameters: None
// action: Prints "meow" to screen
void Cat::Meow()
{
cout << "Meow.n";
}
// create a cat, set its age, have it
// meow, tell us its age, then meow again.
int main()
{
int Age;
cout<<"How old is Frisky? ";
cin>>Age;
Cat Frisky(Age);
Frisky.Meow();
cout << "Frisky is a cat who is " ;
cout << Frisky.GetAge() << " years old.n";
Frisky.Meow();
Age++;
Frisky.SetAge(Age);
cout << "Now Frisky is " ;
cout << Frisky.GetAge() << " years old.n";
return 0;
}
所以我的问题是,我们是否应该在此上下文中(设置成员变量的值时)使用this
关键字?谢谢!
编辑:或,此处提到的这是A 个人喜好?
在示例的上下文中,关键字this
通常用于自我文档,以将类的非静态成员与本地变量区分开。
构造函数:
// constructor of Cat,
Cat::Cat(int initialAge)
{
itsAge = initialAge;
string = new char[10]();
}
您应该将其写为:
// constructor of Cat,
Cat::Cat(int itsAge)
: itsAge(itsAge),
string(new char[10])
{
}
复制构造函数:
//copy constructor for making a new copy of a Cat
Cat::Cat(const Cat& copy_from) {
itsAge = copy_from.itsAge;
string = new char[10]();
std::copy(copy_from.string+0, copy_from.string+10, string);
}
您应该将其写为:
Cat::Cat(const Cat& copy_from)
: itsAge(copy_from.itsAge),
string(new char[10])
{
std::copy(copy_from.string, copy_from.string+sizeof string, string);
}
复制分配:
//copy assignment for assigning a value from one Cat to another
Cat& Cat::operator=(const Cat& copy_from) {
itsAge = copy_from.itsAge;
std::copy(copy_from.string+0, copy_from.string+10, string);
}
您应该将其写为:
Cat& Cat::operator=(const Cat& copy_from)
{
this->itsAge = copy_from.itsAge;
std::copy(copy_from.string, copy_from.string + sizeof string, string);
}
突变器:
// Definition of SetAge, public
// accessor function
void Cat::SetAge(int age)
{
// set member variable its age to
// value passed in by parameter age
itsAge = age;
}
您应该将其写为:
void Cat::SetAge(int itsAge)
{
this->itsAge = itsAgege;
}
换句话说:
- 在可能的情况下使用会员初始化列表
- 使用与他们要参与的成员相同的论点,以节省标识符并提高清晰度
- 在必要时使用
this->
消除歧义或错误。
相关文章:
- #为""定义宏;静态";针对不同的上下文
- 与互斥锁相比,旋转锁可以保证上下文切换
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- 为什么我不能使用 EGL 创建无头 OpenGl 上下文?
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 为什么在逗号分隔符上下文中将预增量的结果强制转换为void
- 错误"Could not find Boost"(缺少:上下文标头)
- 使用 mod_gsoap 部署服务时,如何在 Gsoap 中更改 soap 上下文的模式?
- 将有状态的 lambda 传递到 C 样式函数中,而无需上下文参数
- 安排带有上下文的协同程序
- 我的文件无法正常工作,无法从C++文件中读取上下文
- DCMTK 了解"DIMSE 没有有效的演示上下文 ID"错误
- 具有std::initializer_list参数的非成员函数(/non构造函数上下文)的重载解析
- antlr 规则上下文是否可以独立于目标
- OpenGL 调试上下文警告 -"将使用视频内存作为缓冲区异议的来源
- MSVC 在不知道类型的情况下评估上下文(和错误)
- 为什么 Perf 具有如此高的上下文切换?
- 非推导上下文,如标准库中的"boost::mpl::identity:<T>:type"?
- 使用 std::size 来自非 const 上下文
- 如何使用IExecuteCommand和动词在上下文菜单外壳扩展中显示本地化文本和自定义图标?