c++ oop program doesn't give expected result [duplicate] - c++

This question already has answers here:
Returning a reference to a local variable in C++
(3 answers)
Closed 6 years ago.
Consider the following piece of program:
class cls
{
int vi;
public:
cls(int v=37)
{
vi=v;
}
friend int& f(cls);
};
int& f(cls c)
{
return c.vi;
}
int main()
{
const cls d(15);
f(d)=8;
cout<<f(d);
return 0;
}
When I run it, the output is
15
but I don't understand why 15, because I thought it should've outputed 8, because of the
f(d)=8
function, which from what I understand makes the c.vi=8, but I might be wrong and the function probably does something else entirely, so then I ask, what is the purpose or what does the
friend int& f(cls);
function do?

Your program has Undefined Behavior - you are returning a dangling reference to local variable of a function (argument is a local variable as well).

Related

How does char pointer reference to function name in C++ [duplicate]

This question already has answers here:
How to call a function by its name (std::string) in C++?
(4 answers)
Closed 1 year ago.
I am very kindergartener to C++. Hope someone can help me out with my problem.
Assume there is a function defined in one class.
void __foo__(int x, int y){
//do something
}
In another class, there is a char pointer that holds the value of the function name.
static const char *func = "__foo__";
How do I call the function by using the "func" like func(0, 0)?
C++ doesn't have reflection, so you have to write the code to call the function based on the name yourself
void fall_function_based_on_name(const char* func_name, classWithMethod* self, lint x, int y) {
if (strcmp(func_name, "__foo__")==0)
self->__foo__(x, y);
else
throw std::logic_error("method name not found");
}

Why does calling a functor with an undeclared variable work? [duplicate]

This question already has answers here:
Why does C++ allow us to surround the variable name in parentheses when declaring a variable?
(2 answers)
Closed 5 years ago.
class foo {
public:
bool operator () (int & i) {
return true;
}
};
int main() {
foo(WhyDoesThisCompile);
return 0;
}
When passing WhyDoesThisCompile (without spaces) to the functor, the program compiles.
Why is this? I tested it on clang 4.0.0.
You are not invoking the functor.
You are declaring a foo, called WhyDoesThisCompile.
Yes, despite the parentheses.
I guess you meant this:
foo()(WhyDoesThisCompile);
// ^^^^^
// temp ^^^^^^^^^^^^^^^^^^^^
// of invocation of op()
// type
// `foo`
… which doesn't.

Why does bind not work with pass by reference? [duplicate]

This question already has answers here:
std::bind lose reference when delivered as rvalue reference
(2 answers)
Closed 7 years ago.
I find pass by reference tends not to work when using std::bind. Here's an example.
int test;
void inc(int &i)
{
i++;
}
int main() {
test = 0;
auto i = bind(inc, test);
i();
cout<<test<<endl; // Outputs 0, should be 1
inc(test);
cout<<test<<endl; // Outputs 1
return 0;
}
Why isn't the variable incrementing when called via the function created with std bind?
std::bind copies the argument provided, then it passes the copy to your function. In order to pass a reference to bind you need to use std::ref:auto i = bind(inc, std::ref(test));

C++: member call to non-static member function pointer [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C++ member-function pointer
How to invoke pointer to member function when it's a class data member?
I've only recently started using C++, so I apologize if the following contains any trivial mistakes, or if I missed an easier solution. I would like to achieve something like this:
class ClassA {
typedef double (ClassA::*CondFunc)();
public:
ClassA(int x, int y) {
value_ = x;
switch (y) {
case 0:
condFunc_ = &ClassA::condA;
break;
case 1:
condFunc_ = &ClassA::condB;
default:
break;
}
}
~ClassA();
int value_;
CondFunc condFunc_;
double condA() { return 2.0*value_; }
double condB() { return 4.0*value_; }
void Test() {
int a = condFunc_(); // compile error
}
};
but get a compile error in Test(). Please note that this is a vastly simplified function and is not supposed to make any sense. I've searched this forum and elsewhere for answers, but am still not sure whether defining/calling such non-static member function pointers is even possible. The only plausible hint/solution I've come across employs a static wrapper function to achieve something similar. I'd be grateful for any help/clarifications.
You have to call the member pointer function like this:
int a = (this->*condFunc_)();

Adding const-ness after the fact in C++ [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Is there some ninja trick to make a variable constant after its declaration?
Consider the following minimal example:
void MutateData(std::string&);
int main()
{
std::string data = "something that makes sense to humans.";
::MutateData(data); // Mutates 'data' -- e.g., only changes the order of the characters.
// At this point, 'data' should never be changed.
// Mixing the above with const-correct code seems ugly.
}
Currently, I'm doing:
namespace
{
std::string PrepareData(std::string data)
{
::MutateData(data);
return data;
}
}
int main()
{
const std::string data = ::PrepareData("something that makes sense to humans.");
}
What are some elegant solutions to simulating const beyond the point of declaration?
EDIT: I forgot to clarify that I can't easily (not my code) change MutateData.
You can use a const reference.
Take a look at http://herbsutter.com/2008 for an explanation about why it works.
What about:
string MakeData(string const&)
{
...
return string(...); // for return value optimization
}
followed by
int main()
{
string const& str = MakeData("Something that makes sense to humans");
}
The difference with what you do is using a const reference, and only one function. If you cannot change MutateData, do what you suggested (with the const reference though)