为什么std::unique_ptr<A> pA1(new A);
中的main()
定义可以,但是在B类定义中却产生错误?
#include <iostream>
#include <string>
#include <memory>
class A {
public:
A() { std::cout << "A::A\n"; }
~A() { std::cout << "A::~A\n"; }
};
class B
{
public:
int i = 12;
std::unique_ptr<A> pA(new A); // Fail
//std::unique_ptr<A> pA = new A(); // Also fail
virtual ~B() noexcept {
std::cout << "B::~B\n";
}
void f1() {
std::cout << i << std::endl;
}
};
int main()
{
std::unique_ptr<A> pA1(new A); // Ok
B b = B();
b.f1();
}
g ++编译器实际说的是:g++ -std=c++11 example_1.cpp
example_1.cpp:15:27: error: expected parameter declarator
std::unique_ptr<A> pA(new A); // Fail
^
example_1.cpp:15:27: error: expected ')'
example_1.cpp:15:26: note: to match this '('
std::unique_ptr<A> pA(new A); // Fail
^
2 errors generated.
好像第一个错误说它将pA
解释为函数?