Login Register


C++ Challenge - Local Reference filter_list
Author
Message
C++ Challenge - Local Reference #1
Code:
#include <iostream> std::string get_string() { return std::string("My String"); } int main() { /* declare 'obj' as a reference to the temporary string object returned by get_string() below... (remember: references use &, i.e. std::string &var_name, for instance)*/ // -- CODE HERE -- std::cout << "Before: " << obj << std::endl; obj = "Changed!"; std::cout << "Modified: " << obj << std::endl; }

Description is commented within the above code. You can't change any of the existing code, you can only add code where specified (// -- CODE HERE -- ). You must create a std:Confusedtring object which is a reference to the local variable returned by the get_string() function, and the code must compile, containing all of the existing code. You can't modify the function get_string() at all either. Smile

NOTE: Because of the context of the given code, this is a C++11 challenge...

Good luck.
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: C++ Challenge - Local Reference #2
Those of you who think C++ is nice and straightforward, I'll guarantee that this one will rack your brain. Wink

edit: I see how this is going to go... Boring.
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: C++ Challenge - Local Reference #3
Here's the solution:

Code:
#include <iostream> std::string get_string() { return std::string("My String"); } int main() { /* declare 'obj' as a reference to the temporary string object returned by get_string() below... (remember: references use &, i.e. std::string &var_name, for instance)*/ // -- CODE HERE -- std::string&& obj = const_cast<std::string&&>(get_string()); std::cout << "Before: " << obj << std::endl; obj = "Changed!"; std::cout << "Modified: " << obj << std::endl; }

Nice one!
Everything is relative

Reply

RE: C++ Challenge - Local Reference #4
Or..
Code:
std::string&& obj = std::move(get_string());

Example:
Code:
#include <iostream> class MyClass { public: MyClass() : x_(0) {} MyClass(int x) : x_(x) {} const int &get_x() const { return x_; } void set_x(int x) { x_ = x; } private: int x_; }; MyClass f(int x) { return MyClass(x); } int main() { using namespace std; MyClass obj1 = f(1); cout << "Object 1 constructed with x = " << obj1.get_x() << endl; MyClass &&obj2 = std::move(obj1); cout << "Object 2 created from an rvalue reference to Object 1" << endl; int value = 99; cout << "Setting Object 2's x value to " << value << endl; obj2.set_x(value); std::cout << "Object 1's x value is " << obj1.get_x() << std::endl; }
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: C++ Challenge - Local Reference #5
You are right but when i made this i thought i was only allowed to use the obj variable
Everything is relative

Reply

RE: C++ Challenge - Local Reference #6
(07-18-2014, 07:20 AM)lady_godiva Wrote: You are right but when i made this i thought i was only allowed to use the obj variable

My solution is only using the obj variable too...? :S
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: C++ Challenge - Local Reference #7
(07-20-2014, 08:49 PM)ArkPhaze Wrote:
(07-18-2014, 07:20 AM)lady_godiva Wrote: You are right but when i made this i thought i was only allowed to use the obj variable

My solution is only using the obj variable too...? :S

In your code you use both obj1, obj2 and you defined your own class. What i meant is that i was thinking i had too use a single line of code to solve it Wink
Everything is relative

Reply

RE: C++ Challenge - Local Reference #8
(07-20-2014, 09:28 PM)lady_godiva Wrote:
(07-20-2014, 08:49 PM)ArkPhaze Wrote:
(07-18-2014, 07:20 AM)lady_godiva Wrote: You are right but when i made this i thought i was only allowed to use the obj variable

My solution is only using the obj variable too...? :S

In your code you use both obj1, obj2 and you defined your own class. What i meant is that i was thinking i had too use a single line of code to solve it Wink

That was an added example of std::move().... I think you are confused here. This is one line:
Code:
std::string&& obj = std::move(get_string());

Is it not?

That example code isn't even the same as the challenge code. I provided that for demonstration only...
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: C++ Challenge - Local Reference #9
(07-21-2014, 05:50 AM)ArkPhaze Wrote:
(07-20-2014, 09:28 PM)lady_godiva Wrote:
(07-20-2014, 08:49 PM)ArkPhaze Wrote:
(07-18-2014, 07:20 AM)lady_godiva Wrote: You are right but when i made this i thought i was only allowed to use the obj variable

My solution is only using the obj variable too...? :S

In your code you use both obj1, obj2 and you defined your own class. What i meant is that i was thinking i had too use a single line of code to solve it Wink

That was an added example of std::move().... I think you are confused here. This is one line:
Code:
std::string&& obj = std::move(get_string());

Is it not?

That example code isn't even the same as the challenge code. I provided that for demonstration only...

oooo my bad hahaha, forgive me, i didn't see that single line in your post when you replied me!
Everything is relative

Reply







Users browsing this thread: 1 Guest(s)