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
cout << endl;
for (int i = 2; i >= 0; --i)
reinterpret_cast
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started