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;

};

I was given this struct to convert to a Buffer class. Any do's and dont's cuz this the first time ever i have encountered this problem? any advice would be appreciated. I know i have to use the right encapsulation as well as some move constructors. any advice? thank you.

Pavlos Panteliadis
  • 1,495
  • 1
  • 15
  • 25
  • 1
    `struct` and `class` are literally the exact same thing, the only difference is that if you don't specify otherwise all members of a `struct` are `public`, and `class` are `private`. If you add `public:` before the first attribute, you will notice no difference. Although I would recommend actually going through and deliberately deciding what should be `public` vs `private` – Cory Kramer Feb 09 '15 at 18:41
  • One comment - get rid of the `int *p` member and use `std::vector` instead. – PaulMcKenzie Feb 09 '15 at 18:42

1 Answers1

0

struct and class are identical in c++ - except that the members default to public in struct and private in class. So you just need to decide which data and functions need to be public and put them in a public: section. The classic implementation is that no data is public

pm100
  • 48,078
  • 23
  • 82
  • 145