c ++中this关键字
Constant is something that doesn't change. In C language and C++ we use the keyword const
to make program elements constant. const
keyword can be used in many contexts in a C++ program. It can be used with:
常数是不变的。 在C语言和C ++中,我们使用关键字const
使程序元素恒定。 const
关键字可以在C ++程序的许多上下文中使用。 它可以与:
Variables
变数
Pointers
指针
Function arguments and return types
函数参数和返回类型
Class Data members
类数据成员
Class Member functions
类成员函数
Objects
对象
1)C ++中的常量变量 (1) Constant Variables in C++)
If you make any variable as constant, using const
keyword, you cannot change its value. Also, the constant variables must be initialized while they are declared.
如果使用const
关键字将任何变量设为常量,则无法更改其值。 另外,常量变量必须在声明时进行初始化。
int main
{
const int i = 10;
const int j = i + 10; // works fine
i++; // this leads to Compile time error
}
In the above code we have made i
as constant, hence if we try to change its value, we will get compile time error. Though we can use it for substitution for other variables.
在上面的代码中,我们将i
为常数,因此,如果尝试更改其值,则会出现编译时错误。 虽然我们可以用它代替其他变量。
2)在C ++中使用const
关键字的指针 (2) Pointers with const
keyword in C++)
Pointers can be declared using const keyword too. When we use const
with pointers, we can do it in two ways, either we can apply const
to what the pointer is pointing to, or we can make the pointer itself a constant.
指针也可以使用const关键字声明。 当我们将const
与指针一起使用时,我们可以通过两种方式做到这一点:要么可以将const
应用于指针所指向的对象,要么可以使指针本身成为常量。
指向const
变量的指针 (Pointer to a const
variable)
This means that the pointer is pointing to a const
variable.
这意味着指针指向const
变量。
const int* u;
Here, u
is a pointer that can point to a const int
type variable. We can also write it like,
在这里, u
是一个可以指向const int
类型变量的指针。 我们也可以这样写
char const* v;
still it has the same meaning. In this case also, v
is a pointer to an char
which is of const
type.
仍然具有相同的含义。 同样在这种情况下, v
是指向const
类型的char
的指针。
Pointers to a const
variable is very useful, as this can be used to make any string or array immutable(i.e they cannot be changed).
指向const
变量的指针非常有用,因为它可用于使任何字符串或数组不可变(即,它们不能更改)。
const
指针 (const
Pointer)
To make a pointer constant, we have to put the const
keyword to the right of the *
.
为了使指针恒定,我们必须将const
关键字放在*
的右边。
int x = 1;
int* const w = &x;
Here, w
is a pointer, which is const
, that points to an int
. Now we can't change the pointer, which means it will always point to the variable x
but can change the value that it points to, by changing the value of x
.
在这里, w
是一个指针,它是const
,它指向一个int
。 现在,我们不能改变指针,这意味着它将始终指向变量x
,但它指向,通过改变值可以改变价值x
。
The constant pointer to a variable is useful where you want a storage that can be changed in value but not moved in memory. Because the pointer will always point to the same memory location, because it is defined with const
keyword, but the value at that memory location can be changed.
在需要可以更改值但不能在内存中移动的存储的情况下,指向变量的常量指针很有用。 由于指针将始终指向相同的存储位置,因为它是用const
关键字定义的,但是可以更改该存储位置上的值。
NOTE: We can also have a const
pointer pointing to a const
variable.
注意 :我们也可以有一个指向const
变量的const
指针。
const int* const x;
3) const
函数参数和返回类型 (3) const
Function Arguments and Return types)
We can make the return type or arguments of a function as const
. Then we cannot change any of them.
我们可以将函数的返回类型或参数设为const
。 然后,我们将无法更改其中任何一个。
void f(const int i)
{
i++; // error
}
const int g()
{
return 1;
}
注意事项 (Some Important points to Remember)
For built in datatypes, returning a
const
or non-const value, doesn't make any difference.对于内置数据类型,返回
const
或非const值没有任何区别。
Bothconst int h() { return 1; } int main() { const int j = h(); int k = h(); }
j
andj
和k
will be assigned the valuek
都将被赋值为1
. No error will occur.1
。 不会发生错误。For user defined datatypes, returning
const
, will prevent its modification.对于用户定义的数据类型,返回
const
将阻止对其进行修改。Temporary objects created while program execution are always of
const
type.程序执行时创建的临时对象始终为
const
类型。If a function has a non-const parameter, it cannot be passed a
const
argument while making a call.如果函数具有非const参数,则在调用时不能将其传递给
const
参数。
If we pass avoid t(int*) { // function logic }
const int*
argument to the functionconst int*
参数传递给函数t
, it will give error.t
,它将产生错误。But, a function which has a
const
type parameter, can be passed aconst
type argument as well as a non-const argument.但是,具有
const
类型参数的const
可以传递const
类型参数以及非const参数。
This function can have avoid g(const int*) { // function logic }
int*
as well asint*
以及const int*
type argument.const int*
类型的参数。
4)将类数据成员定义为const
(4) Defining Class Data members as const
)
These are data variables in class which are defined using const
keyword. They are not initialized during declaration. Their initialization is done in the constructor.
这些是使用const
关键字定义的类中的数据变量。 它们在声明期间未初始化。 它们的初始化在构造函数中完成。
class Test
{
const int i;
public:
Test(int x):i(x)
{
cout << "\ni value set: " << i;
}
};
int main()
{
Test t(10);
Test s(20);
}
In this program, i
is a constant data member, in every object its independent copy will be present, hence it is initialized with each object using the constructor. And once initialized, its value cannot be changed. The above way of initializing a class member is known as Initializer List in C++.
在此程序中, i
是一个常量数据成员,在每个对象中都会存在其独立副本,因此将使用构造函数对每个对象进行初始化。 并且一旦初始化,其值就无法更改。 初始化类成员的上述方式在C ++中称为“ 初始化器列表” 。
5)将类对象定义为const
(5) Defining Class Object as const
)
When an object is declared or created using the const
keyword, its data members can never be changed, during the object's lifetime.
当使用const
关键字声明或创建对象时,在其生存期内,其数据成员将永远无法更改。
Syntax:
句法:
const class_name object;
For example, if in the class Test
defined above, we want to define a constant object, we can do it like:
例如,如果在上面定义的Test
类中,我们想要定义一个常量对象,则可以像这样进行操作:
const Test r(30);
6)将类的Member函数定义为const
(6) Defining Class's Member function as const
)
A const
member function never modifies data members in an object.
const
成员函数从不修改对象中的数据成员。
Syntax:
句法:
return_type function_name() const;
const对象和const成员函数示例 (Example for const Object and const Member function)
class StarWars
{
public:
int i;
StarWars(int x) // constructor
{
i = x;
}
int falcon() const // constant function
{
/*
can do anything but will not
modify any data members
*/
cout << "Falcon has left the Base";
}
int gamma()
{
i++;
}
};
int main()
{
StarWars objOne(10); // non const object
const StarWars objTwo(20); // const object
objOne.falcon(); // No error
objTwo.falcon(); // No error
cout << objOne.i << objTwo.i;
objOne.gamma(); // No error
objTwo.gamma(); // Compile time error
}
Falcon has left the Base Falcon has left the Base 10 20
猎鹰离开了基地猎鹰离开了基地10 20
Here, we can see, that const
member function never changes data members of class, and it can be used with both const
and non-const objecta. But a const
object cannot be used with a member function which tries to change its data members.
在这里,我们可以看到const
成员函数从不更改类的数据成员,并且可以与const
和非const objecta一起使用。 但是const
对象不能与试图更改其数据成员的成员函数一起使用。
mutable
关键字 (mutable
Keyword)
mutable
keyword is used with member variables of class, which we want to change even if the object is of const
type. Hence, mutable
data members of a const
objects can be modified.
mutable
关键字与class的成员变量一起使用,即使对象为const
类型,我们也要对其进行更改。 因此,可以修改const
对象的mutable
数据成员。
class Zee
{
int i;
mutable int j;
public:
Zee()
{
i = 0;
j = 0;
}
void fool() const
{
i++; // will give error
j++; // works, because j is mutable
}
};
int main()
{
const Zee obj;
obj.fool();
}
c ++中this关键字