Question: 1) Create a simple multi-arg version of placement new ***an example is below, but need a different multi-arg version*** // A sample class to place

1) Create a simple multi-arg version of placement new

***an example is below, but need a different multi-arg version***

// A sample class to place in an array

// All its functions trace themselves to cout

class Thing {

int m, n;

public:

Thing(int i = 0, int j = 0) {

m = i;

n = j;

cout << "Thing(" << m << "," << n << ") initialized ";

}

~Thing() {

cout << "Thing(" << m << "," << n << ") destroyed ";

}

Thing(const Thing& t) : m(t.m), n(t.n) {

cout << "Thing(" << m << "," << n << ") copied ";

}

Thing& operator=(const Thing& t) {

m = t.m;

n = t.n;

cout << "Thing(" << m << "," << n << ") assigned ";

return *this;

}

void set_m(int new_m) {

m = new_m;

cout << "m assigned to " << m << endl;

}

void set_n(int new_n) {

n = new_n;

cout << "n assigned to " << n << endl;

}

friend ostream& operator<<(ostream& os, const Thing& t) {

return os << '(' << t.m << ',' << t.n << ')';

}

};

int main() {

char* p2 = new char[sizeof(Thing)*5];

for (int i = 0; i < 3; ++i)

new (p2 + i*sizeof(Thing)) Thing(i+1,i+2);

for (int i = 0; i < 3; ++i)

cout << *reinterpret_cast(p2 + i*sizeof(Thing)) << ' ';

cout << endl;

for (int i = 2; i >= 0; --i)

reinterpret_cast(p2 + i*sizeof(Thing))->~Thing();

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!