c ++中this关键字_C ++中的关键字

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 ++程序的许多上下文中使用。 它可以与:

  1. Variables

    变数

  2. Pointers

    指针

  3. Function arguments and return types

    函数参数和返回类型

  4. Class Data members

    类数据成员

  5. Class Member functions

    类成员函数

  6. 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)

  1. For built in datatypes, returning a const or non-const value, doesn't make any difference.

    对于内置数据类型,返回const或非const值没有任何区别。

    const int h()
    {
        return 1;
    }
    
    int main()
    {
        const int j = h();
        int k = h();
    }
    Both j and jk will be assigned the value k都将被赋值为1. No error will occur.1 。 不会发生错误。
  2. For user defined datatypes, returning const, will prevent its modification.

    对于用户定义的数据类型,返回const将阻止对其进行修改。

  3. Temporary objects created while program execution are always of const type.

    程序执行时创建的临时对象始终为const类型。

  4. If a function has a non-const parameter, it cannot be passed a const argument while making a call.

    如果函数具有非const参数,则在调用时不能将其传递给const参数。

    void t(int*) 
    { 
        // function logic
    }
    If we pass a const int* argument to the function const int*参数传递给函数t, it will give error.t ,它将产生错误。
  5. But, a function which has a const type parameter, can be passed a const type argument as well as a non-const argument.

    但是,具有const类型参数的const可以传递const类型参数以及非const参数。

    void g(const int*) 
    {
        // function logic
    }
    This function can have a int* as well as int*以及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();
}

翻译自: https://www.studytonight.com/cpp/const-keyword.php

c ++中this关键字

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值