2 개의 ctors가있는 일부 오디오 형식에 대한 추상 의사 기본 클래스가 있습니다. 하나는 파생 클래스에서 작동하지만 다른 하나는 해결할 수없는 오류를 제공합니다. MP3로 선언 된 보호 된 멤버에 액세스 할 수 없지만 하나의 ctor에 연결할 수는 있지만 다른 사람에게는 연결할 수없는 이유는 무엇입니까?파생 클래스에서 가상 기본 클래스의 protected ctor를 사용합니다.
class Audioformat
{
protected:
string song="";
Audioformat(string s) :song(s) {};//This ctor gives me the error
Audioformat() { song = "unknown";}
public:
virtual void play()=0;
virtual void info() = 0;
virtual ~Audioformat() = 0 {};
};
class MP3 : public Audioformat
{
public:
using Audioformat::Audioformat;
void play() { cout << "pseudo-play" << endl; }
void info() { cout << song << endl; }
~MP3() { cout << "MP3" << endl; delete this; }
};
Here's 내 주요 :
int main()
{
MP3 song1{};//WORKS
MP3 song2{ "random trash song" };//ERROR MP3::MP3(std::string) is inaccessible
play(song1);
info(song1);
getchar();
return 0;
}
OT : '이것을 지워 라, 무엇? –
'delete this'는 'MP3'의 모든 인스턴스가 new (예 : 생성자를 비공개로 만들고 정적 생성자 함수를 추가하는 등)로 생성 된 경우에만 작동합니다. 그리고 심지어 소멸자로부터 절대 호출해서는 안됩니다. 왜냐하면 그것은 분명히 무한 재귀를 생성하기 때문입니다. 그러나 'MP3'라는 클래스에서는 합리적인 유스 케이스를 상상할 수 없습니다. 이벤트 관리 시스템의 경우보다 일반적입니다. –
아,'string song = "";;은 필요 없습니다. 이것은 Java가 아닙니다. 그것을'std :: string song;'으로 만드십시오. –