嘗試爲類內的私有結構編寫setter函數。還沒有找到允許訪問結構的聲明方法。使用類成員函數訪問類內的私有結構
該結構可能不能移出課程。該函數必須是該類的成員。前向聲明可能不被使用。
class Editor
{
public:
void setName (string s);
private:
struct Object
{
string name;
}Instance;
}Ed;
void Editor::setName (string s)
{
name = s; // no access
}
==================================
class Editor
{
public:
friend void setName (Editor &m , string s);
private:
struct Object
{
int name;
}Instance;
}Ed;
void setName (Editor &m , string s)
{
name = s; // no access
}
== ================================
class Editor
{
public:
friend void setName (Object &m , string s); //invalid declaration ( Object is undefined)
private:
struct Object
{
string name;
}Instance;
}Ed;
請張貼實際的代碼。 – juanchopanza