why a const object of an empty class cant be creat

2019-06-15 13:37发布

#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 main()
{
  A a;
  const A ac;
  a.foo();
  ac.foo();
}

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

2条回答
小情绪 Triste *
2楼-- · 2019-06-15 14:09
欢心
3楼-- · 2019-06-15 14:14

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!

查看更多
登录 后发表回答