【发布时间】:2015-02-09 18:39:25
【问题描述】:
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 类。我第一次遇到这个问题有什么注意事项吗?任何意见,将不胜感激。 我知道我必须使用正确的封装以及一些移动构造函数。有什么建议吗?谢谢。
【问题讨论】:
-
struct和class实际上是完全相同的东西,唯一的区别是,如果您不指定,则struct的所有成员都是public和class是private。如果您在第一个属性之前添加public:,您会发现没有区别。虽然我会建议实际通过并故意决定应该是public与private -
一条评论 - 删除
int *p成员并改用std::vector<int>。