根本没有太多人回答我的问题。我想做的是使用私有拷贝构造函数并将它们作为字符串或二进制文件分配给变量。
这是代码。
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class President
{
private:
President() {};
President(const President&);
const President& operator=(const President&);
string Name;
public:
static President& GetInstance()
{
static President OnlyInstance;
return OnlyInstance;
}
string GetFile()
{
cout<<"Enter the name of the file you want to protect: ";
cin>>Name;
ofstream fsOut;
fsOut.open(Name, ios_base::out, ios_base::binary);
if(fsOut.is_open())
{
cout<<"Writing to binary file."<<endl;
fsOut<<Name<<endl;
cout<<"File open successful."<<endl;
fsOut.close();
}
return Name;
}
void SetFile(string InputFile)
{
Name=InputFile;
}
};
int main()
{
string Name;
President& OnlyPresident=President::GetInstance();
OnlyPresident.SetFile(Name);
cout<<President::GetInstance().GetFile()<<endl;
return 0;
}
我编了它,很好。但是,它没有正确运行,或者它与我输入的文件不对应。如何使用私有拷贝构造函数正确保护文件?
John P。