【问题标题】:How does "const" differ in C and C++?C 和 C++ 中的“const”有何不同?
【发布时间】:2011-05-28 01:14:22
【问题描述】:

变量的 const 限定在 C 和 C++ 中有何不同?

来自:Does "const" just mean read-only or something more?

“引发这个问题的是这个答案:https://stackoverflow.com/questions/4024318#4024417 他声明 const “just”在 C 中表示只读。我认为这就是 const 的全部含义,无论它是 C 还是 C++。他是什么意思? "

【问题讨论】:

标签: c++ c constants


【解决方案1】:

C 中的const 不能用于构建常量表达式。

例如:

#include <stdio.h>
int main()
{
   int i = 2;
   const int C = 2;
   switch(i)
   {
      case C  : printf("Hello") ;
      break;

      default : printf("World");
   }
}

在 C 中不起作用,因为 case 标签不会减少为整数常量。

【讨论】:

  • 我可能记错了,但我认为这在 C99 中有所改变。
  • @ephemient :不。再次查看我的答案。
【解决方案2】:

const 表示承诺不会改变变量。还是可以改的。

class A {
  public:
    A(const int& a);
    int getValue() const;
    void setValue(int b);
  private:
    const int& a;
};
A::A(a) : a(a) {}
int A::getValue() const {
    return a;
}
void A::setValue(int b) {
    a = b;  // error
}

int main() {
    int my_a = 0;
    A a(my_a);
    std::cout << a.getValue() << std::endl;  // prints 0
    my_a = 42;
    std::cout << a.getValue() << std::endl;  // prints 42
}

没有方法A::* 可以改变a,但main 可以。这在 C 和 C++ 之间是相同的。


C++ 确实有几种(有限的)绕过const 的方法,它们应该阻止程序员不恰当地丢弃const

参加这样的课程。

class A {
  public:
    A();
    int getValue();
  private:
    static int expensiveComputation();
    int cachedComputation;
};

A::A() : cachedComputation(0) {}

A::getValue() {
    if (cachedComputation == 0)
        cachedComputation = expensiveComputation();
    return cachedComputation;
}

cachedComputation 隐含地表示this-&gt;cachedComputation。请记住这一点。

int main() {
    A a1;
    const A a2;
    std::cout << a1.getValue() << std::endl;
    std::cout << a2.getValue() << std::endl;  // error
}

a2.getValue() 是非法的,因为在 const A a2 上调用了非const 方法。可以抛弃const-ness……

    std::cout << ((A&)a2).getValue() << std::endl;            // C-style cast
    std::cout << const_cast<A&>(a2).getValue() << std::endl;  // C++-style cast

第二个是首选,因为编译器将检查只有const-ness 被强制转换,没有别的。然而,这仍然不理想。相反,应该在类中添加一个新方法。

class A {
  public:
    int getValue() const;
};

A::getValue() const {
    if (cachedComputation == 0)
        cachedComputation = expensiveComputation();  // error
    return cachedComputation;
}

现在有一个const 方法,所以a2.getValue() 没问题。但是,结尾的 const 意味着该方法被赋予了一个 const A *this 指针,而不是像往常一样的 A *this 指针,这使得 this-&gt;cachedComputation 成为一个不能被变异的 const int &amp;

const_cast 可以应用在方法内部,但最好更改此成员的声明。

class A {
  private:
    mutable int cachedComputation;
};

现在,即使使用 const A *thisthis-&gt;cachedComputation 也可以在不强制转换的情况下进行变异。

【讨论】:

  • 我认为你的帖子没有回答他的问题。你还没有在 C 中写过任何关于 const 的内容。
  • @ephemient:如果您在答案中添加@Prasoon 的内容,我会接受。不幸的是,在这一点上,没有一个答案完全回答了这个问题,我相信你的答案更完整。 (仅供参考,我确实用 gcc -std=c99 测试了 Prasoon 的答案,以确保他是正确的。)
  • @Prasoon:他表明 C++ 提供了一个可变关键字,而 C 没有。我认为这与语言中 const 的使用直接相关,我错了吗?我相信你的回答也说明了一个重要的区别。
  • @Kim:但你的问题是关于const 而不是mutable。 AFAIK C 和 C++ 中 const 的唯一区别是我在帖子中提到的。
  • 您使用const_cast&lt;A&amp;&gt;(a2).getValue() 显示的调用和含义是不合法的。它们会导致未定义的行为。否则一个很好的答案。
猜你喜欢
  • 1970-01-01
  • 2019-08-20
  • 2014-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多