0
struct Buffer{
int* p;
unsigned n;
mutable std::string toString;
mutable bool toStringDirty;
Buffer (void); // implement it with correct initialization
Buffer (unsigned m, int val = 0); // inits with m ints with val
Buffer (const Buffer& a) {
Buffer::Buffer();
if (a.n) {
unsigned m = (n = a.n) * sizeof(int);
memcpy(p = (int*) malloc(m), a.p, m);
}
toString = a.toString;
toStringDirty = a.toStringDirty;
Buffer (const Buffer&&);
int& operator[](unsigned i);
const Buffer& operator=(const Buffer&&);
const Buffer& operator=(const Buffer&);
const Buffer& operator=(int val); //sets all with val
const Buffer operator+(const Buffer& a) const; // concatenates
// appends ‘a.first’ ints with an ‘a.second’ value
const Buffer operator+(const std::pair<unsigned,int>& a) const;
const Buffer operator-(unsigned m) const; // drops m ints at end
// converts to string with caching support, format as [%d] per integer
const std::string ToString (void) const;
};
我被给了这个结构来转换成一个Buffer类。任何做和不要因为这是有史以来第一次遇到这个问题?任何意见,将不胜感激。 我知道我必须使用正确的封装以及一些移动构造函数。有什么建议?谢谢。从结构到C++中的类
'struct'和'class'实际上是完全相同的东西,唯一的区别是,如果你没有指定'struct'的所有成员都是'public',和'class'是'private'。如果您在第一个属性之前添加“public:”,您将注意到没有区别。虽然我会建议实际上通过并故意决定什么应该是'公共'与'私人' – CoryKramer 2015-02-09 18:41:15
一个评论 - 摆脱'int * p'成员并使用'std :: vector'代替。 –
PaulMcKenzie
2015-02-09 18:42:56