![]() |
|
C++ Challenge - Local Reference - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: C, C++, & Obj-C (https://sinister.ly/Forum-C-C-Obj-C) +--- Thread: C++ Challenge - Local Reference (/Thread-C-Challenge-Local-Reference) |
C++ Challenge - Local Reference - ArkPhaze - 06-29-2014 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: tring 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. ![]() NOTE: Because of the context of the given code, this is a C++11 challenge... Good luck. RE: C++ Challenge - Local Reference - ArkPhaze - 06-29-2014 Those of you who think C++ is nice and straightforward, I'll guarantee that this one will rack your brain. ![]() edit: I see how this is going to go... Boring. RE: C++ Challenge - Local Reference - lady_godiva - 07-08-2014 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! RE: C++ Challenge - Local Reference - ArkPhaze - 07-18-2014 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;
}RE: C++ Challenge - Local Reference - lady_godiva - 07-18-2014 You are right but when i made this i thought i was only allowed to use the obj variable RE: C++ Challenge - Local Reference - ArkPhaze - 07-20-2014 (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 RE: C++ Challenge - Local Reference - lady_godiva - 07-20-2014 (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 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
RE: C++ Challenge - Local Reference - ArkPhaze - 07-21-2014 (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 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... RE: C++ Challenge - Local Reference - lady_godiva - 07-21-2014 (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 oooo my bad hahaha, forgive me, i didn't see that single line in your post when you replied me! |