Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Linked Data A Geographic Perspective

Authors: Glen Hart, Catherine Dolbear

1st Edition

1000218910, 9781000218916

More Books

Students also viewed these Databases questions

Question

What does Processing of an OLAP Cube accomplish?

Answered: 1 week ago