开发者

why a const object of an empty class cant be created

#include <iostream>

class A {
   public:
      void foo() const {
          std::cout << "const version of foo" << std::endl;
      }
      void foo() {
          std::cout << "none const version of foo" << std::endl;
      }
};

int ma开发者_运维百科in()
{
  A a;
  const A ac;
  a.foo();
  ac.foo();
}

The above code can't be compiled, could anyone of you tell me why?


You need to initialize it. This is a known problem with the spec.


Initialize it as:

const A ac = A();

Working code : http://www.ideone.com/SYPO9


BTW, this is not initializaiton : const A ac(); //deceptive - not an initializaiton!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜