“const”的含义最后一个C ++方法声明? [英] Meaning of "const" last in a C++ method declaration?

查看:106
本文介绍了“const”的含义最后一个C ++方法声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

const 在这些声明中的含义是什么? const 困惑我。

  class foobar 
{
public:
operator int()const;
const char * foo()const;
};


解决方案

添加 const 指向一个方法的指针将基本上变成 const ,因此您不能更改任何成员数据。 (除非你使用 mutable ,稍后再说)。



code>关键字是函数签名的一部分,这意味着你可以实现两个类似的方法,一个是当对象是 const 时调用, t。

  #include< iostream> 

class MyClass
{
private:
int counter;
public:
void Foo()
{
std :: cout< Foo< std :: endl;
}

void Foo()const
{
std :: cout< Foo const< std :: endl;
}

};

int main()
{
MyClass cc;
const MyClass& ccc = cc;
cc.Foo();
ccc.Foo();
}

这将输出

  Foo 
Foo const

-const方法,你可以更改实例成员,你不能在 const 版本中做。如果您将上述示例中的方法声明更改为以下代码,您将会遇到一些错误。

  void Foo $ b {
counter ++; // this works
std :: cout<< Foo< std :: endl;
}

void Foo()const
{
counter ++; // this will not compile
std :: cout<< Foo const< std :: endl;
}

这不是完全正确的,因为你可以将一个成员标记为 mutable const 方法可以改变它。它主要用于内部计数器和东西。解决方案将是下面的代码。

  #include< iostream> 

class MyClass
{
private:
mutable int counter;
public:

MyClass():counter(0){}

void Foo()
{
counter ++;
std :: cout<< Foo< std :: endl;
}

void Foo()const
{
counter ++;
std :: cout<< Foo const< std :: endl;
}

int GetInvocations()const
{
return counter;
}
};

int main(void)
{
MyClass cc;
const MyClass& ccc = cc;
cc.Foo();
ccc.Foo();
std :: cout<< MyClass实例已被调用< ccc.GetInvocations()<< times<< endl;
}

pre> Foo
Foo const
MyClass实例已被调用2次


What is the meaning of const in declarations like these? The const confuses me.

class foobar
{
  public:
     operator int () const;
     const char* foo() const;
};

解决方案

When you add the const keyword to a method the this pointer will essentially become const, and you can therefore not change any member data. (Unless you use mutable, more on that later).

The const keyword is part of the functions signature which means that you can implement two similar methods, one which is called when the object is const, and one that isn't.

#include <iostream>

class MyClass
{
private:
    int counter;
public:
    void Foo()
    { 
        std::cout << "Foo" << std::endl;    
    }

    void Foo() const
    {
        std::cout << "Foo const" << std::endl;
    }

};

int main()
{
    MyClass cc;
    const MyClass& ccc = cc;
    cc.Foo();
    ccc.Foo();
}

This will output

Foo
Foo const

In the non-const method you can change the instance members, which you cannot do in the const version. If you change the method declaration in the above example to the code below you will get some errors.

    void Foo()
    {
        counter++; //this works
        std::cout << "Foo" << std::endl;    
    }

    void Foo() const
    {
        counter++; //this will not compile
        std::cout << "Foo const" << std::endl;
    }

This is not completely true, because you can mark a member as mutable and a const method can then change it. It's mostly used for internal counters and stuff. The solution for that would be the below code.

#include <iostream>

class MyClass
{
private:
    mutable int counter;
public:

    MyClass() : counter(0) {}

    void Foo()
    {
        counter++;
        std::cout << "Foo" << std::endl;    
    }

    void Foo() const
    {
        counter++;
        std::cout << "Foo const" << std::endl;
    }

    int GetInvocations() const
    {
        return counter;
    }
};

int main(void)
{
    MyClass cc;
    const MyClass& ccc = cc;
    cc.Foo();
    ccc.Foo();
    std::cout << "The MyClass instance has been invoked " << ccc.GetInvocations() << " times" << endl;
}

which would output

Foo
Foo const
The MyClass instance has been invoked 2 times

这篇关于“const”的含义最后一个C ++方法声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆