3. Template Class, Reference, Memory Management A) Consider the following class: #include using namespace std

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

3. Template Class, Reference, Memory Management A) Consider the following class: #include using namespace std

Post by answerhappygod »

3 Template Class Reference Memory Management A Consider The Following Class Include Iostream Using Namespace Std 1
3 Template Class Reference Memory Management A Consider The Following Class Include Iostream Using Namespace Std 1 (71.3 KiB) Viewed 10 times
3. Template Class, Reference, Memory Management A) Consider the following class: #include <iostream> using namespace std; class Ptr { public: Ptr (int p): curr (p) {} int operator* () const { return *curr; } private: T* curr; The class just represents an int pointer and cannot be used for much, but the following will at least work: int main() { int x[] = {1, 2, 3); Ptr p = x; cout << *p << endl; } i) Reading via the Ptr object works but writing (example, *p = 3;) does not. Why? Modify the class such that this will work as well. [3 marks] ii) Modify the class in part A) such that the following code works: [4 marks] int main() { int x[] = {1, 2, 3); for (Ptr<int> p = x; p != x + 3; ++p) { cout << *p << " "; } cout << endl; string y[] = {"Welcome", "to", "UNMC", "Malaysia"); for (Ptr<string> p = y; p != y + 4; ++p) { cout << *p << " "; } cout << endl; }
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply