#include <iostream>
using namespace std;
class Test{
private:
Test(int a, int b=0)
{
cout << "private constructor\n";
}
public:
Test(int a)
{
cout << "public constructor\n";
}
};
int main()
{
Test t(1);
}
을에 후보로 개인 생성자를 제공 gcc
말한다 :컴파일러는 프로그램 코드
test.cpp: In function ‘int main()’:
test.cpp:20:10: error: call of overloaded ‘Test(int)’ is ambiguous
Test t(1);
^
test.cpp:12:2: note: candidate: Test::Test(int)
Test(int a)
^
test.cpp:7:2: note: candidate: Test::Test(int, int)
Test(int a, int b=0)
^
test.cpp:5:7: note: candidate: Test::Test(const Test&)
class Test{
^
및 clang
을 말한다 :
test.cpp:20:7: error: call to constructor of 'Test' is ambiguous
Test t(1);
^~
test.cpp:7:2: note: candidate constructor
Test(int a, int b=0)
^
test.cpp:12:2: note: candidate constructor
Test(int a)
^
test.cpp:5:7: note: candidate is the implicit copy constructor
class Test{
^
1 error generated.
이의 이유는 무엇입니까 모호? Test(int,int)
은 비공개이므로 Test t(1)
으로 전화 할 수 없어야합니다. 가능한 대답은 (내가 처음 생각한 것), 생성자에 대한 두 개의 동일한 서명을 가능하게합니다. 즉, Test()
은 개인 생성자에서 하나만 int
과 함께 호출 할 수 있습니다. 그러나 프로그램 코드 Test t(1)
은 공용 생성자에만 적합하므로 개인 생성자를 후보로 제공하면 안됩니다. 왜 그렇게 말하는거야?
동일한 이유 : http://stackoverflow.com/questions/39042240/why-is-a-public-const-method-not- 예를 들면, 클래스 구현 파일에 상수를 도입하여 non-const-one-is-private/39042574 # 39042574 – NathanOliver