Dereferencing a function in the rvalue context and in the lvalue context - function-pointers

I was reading this post explaining what happens when we dereference a pointer to function but I don't understand what is meant by
What happens if you dereference a function pointer in an lvalue
context—the left-hand side of an assignment. (The answer will be about
what you expect, if you keep in mind that functions are immutable.)
Just for completeness, I was trying to dereference a function in what I understand is a lvalue context
#include<iostream>
#include<vector>
using namespace std;
void invoker(int x, int& (*f)(int))
{
(**********f)(x)=5;
}
int& f(int x)
{
int& rx = x;
return rx;
}
int main()
{
int x = 3;
invoker(x, f);
cout << f(x) << endl;
}
But it seems to have the same effect as in a rvalue context...

Related

Binary Operator overloading for a member enum class (with non static members)

The following code doesn't compile.
error: invalid use of non-static data member 'data'
#include <iostream>
#include <unordered_map>
#include <vector>
class Domain {
public:
enum class fieldname { x_, y_ };
std::unordered_map<fieldname, std::vector<double>> data;
// default constructor. Hard coding is only for this test!
Domain() {
data[Domain::fieldname::x_] = std::vector<double>{1, 23, 4};
data[Domain::fieldname::y_] = std::vector<double>{1, 23, 4};
}
// operator overloading
friend std::vector<double> operator+(fieldname one, fieldname two) {
std::vector<double> result = data[one]; // so we get the right size
for (int i = 0; i < result.size(); ++i) {
result[i] = data[one][i] + data[two][i];
}
return result;
}
};
int main() {
Domain d;
std::vector<double> temp = Domain::fieldname::x_ + Domain::fieldname::y_;
for (auto item : temp) std::cout << item << std::endl;
return 0;
}
I think it is evident from the code what I am trying to accomplish. Could someone suggest how the operator + can be overloaded so that the enum classes can be used as a proxy for vectors which are members of a class?

Destructor trouble

I've recently read that if you use an object of a class as a reciving parameter of a function, a copy of an object has to be created automaticly. Therefore, if the destructor is included in the class, both original object and it's copy will be vanished automaticly. However, when I tried to make a small code with the same conception destructor only activated once. What can cause the problem? Thanks in advance!
#include "stdafx.h"
#include <iostream>
using namespace std;
class MyClass {
int val;
public:
MyClass(int i)
{
val = i;
cout << "Constructor is in progress" << endl;
}
void SetVal(int i)
{
val = i;
}
int GetVal()
{
return val;
}
~MyClass()
{
cout << "Destructer is in progress" << endl;
}
};
void Display(MyClass obj)
{
cout << obj.GetVal();
}
int main()
{
MyClass a(10);
cout << "Before display()" << endl;
Display(a);
cout << "After display()" << endl;
system("pause");
return 0;
}
It is called after the return statement. The first message you are seeing is from the copied object. When you get to system("pause") your original object is still in scope, so the destructor is not called. It is called after the return statement is evaluated.
Is destructor called at the end of main(); strange behavior

How Do I Make A Bool Value Change In C++?

So I've been trying to figure out the bool function for a while now.
I'm trying to make it so after a specific value is entered, the bool will become true or false.
#include <iostream>
using namespace std;
int a;
bool dead;
void Life() {
if (dead == true) {
cout << "You ded.";
}
else {
cout << "You not ded.";
}
}
int main()
{
cin >> a;
if (a == 10)
{
bool dead = true;
}
Life();
return 0;
}
This is what I currently have, however it isn't changing the value of the bool. Any ideas?
You're redeclaring the variable 'dead' in main's if block. So now you have two 'bool dead', a global one, and a local one in main's if block. The statement 'bool dead = true' sets the local one, but the function 'Life()' uses the global one. Just remove the 'bool' from the latter and you will always be using the global one.
#include <iostream>
using namespace std;
int a;
bool dead;
void Life() {
if (dead == true) {
cout << "You ded.";
}
else {
cout << "You not ded.";
}
}
int main()
{
cin >> a;
if (a == 10)
{
dead = true;
}
Life();
return 0;
}
There are two variables called dead.
- a global
- a local which lifetime is the if block
You set only the local variable inside the if block. So you do not change the global variable. Remove the type inside the if block and your program may run as you intended. And don't forget to initialize your global dead variable. Depending on the OS and state your global variable may start with a true value.

How to define the operator= in a class to make a variable in it to be assigned to an outside variable

I have example code and classs:
class a{
int x;
a(){
this->x = 335; /* example number*/
}
public:
void operator=(int);
};
void a::operator=(int source){
this->x = source;
}
main(){
int i = 100;
a example_class;
example_class = i; //works fine!
i = example_class; /*this is what I want to do.*/
}
the problem with this hole thing is that I can't
make the operator= be a friend function
therefore the command: "i = example_class"
can't be done because I can't create a function in //the the int class like I normally would with my own classes.
Finally:
How can I complete the command:
"i = example_class" when the
operator= can't have more than 1
parameter?
notes:
I know the code doesn't do anything.
And is only an example. The point
Is what actually matters.
Also, I need to make it clear that I
cannot create any functions in the
Target class(in this case int). Only in
the source class(in this case a).
I also want to make clear that I know
that it's impossible to declare the
operator= as a friend function.
I know that I could just create a function
to get a reference to int x or make
int x public but I didn't want to do that
because the real code involves complex
functions for converting between types
so it's vary important to me to be able
to write: "i = example_class;".
Thanks,
Ronen.
Working example.
#include <iostream>
class a {
int x = 355;
public:
void operator=(int);
operator int();
};
void a::operator=(int source){
x = source;
}
a::operator int() {
return x;
}
int main(int, char**) {
int i = 100;
a example_class;
example_class = i;
int j = example_class;
std::cout << j << std::endl;
}

member function as callback

I would like to pass a member function of an instantiated object to another function. Example code is below. I am open for any strategy that works, including calling functional() from another function inside memberfuncpointertestclass using something like lambda or std::bind. Please note that I did not understand most of the threads I found with google about lambda or std::bind, so please, if possible, keep it simple. Also note that my cluster does not have C++ 11 and I would like to keep functional() as simple as it is. Thank you!
int functional( int (*testmem)(int arg) )
{
int a = 4;
int b = testmem(a);
return b;
}
class memberfuncpointertestclass
{
public:
int parm;
int member( int arg )
{
return(arg + parm);
}
};
void funcpointertest()
{
memberfuncpointertestclass a;
a.parm = 3;
int (*testf)(int) = &a.member;
std::cout << functional(testf);
}
int main()
{
funcpointertest();
return 0;
}
You cannot invoke a method on an object without an instance to refer to. So, you need to pass in both the instance as well as the method you want to invoke.
Try changing functional to:
template <typename T, typename M>
int functional(T *obj, M method)
{
int a = 4;
int b = (obj->*(method))(a);
return b;
}
And your funcpointertest to:
void funcpointertest()
{
memberfuncpointertestclass a;
a.parm = 3;
std::cout << functional(&a, &memberfuncpointertestclass::member);
}
This is a job for std::function, a polymorphic function wrapper. Pass to functional(...) such a function object:
#include <functional>
typedef std::tr1::function<int(int)> CallbackFunction;
int functional(CallbackFunction testmem)
{
int a = 4;
int b = testmem(a);
return b;
}
then use std::bind to create a function object of the same type that wraps memberfuncpointertestclass::method() of instance a:
void funcpointertest()
{
memberfuncpointertestclass a;
a.parm = 3;
CallbackFunction testf = std::bind(&memberfuncpointertestclass::member, &a, std::placeholders::_1);
std::cout << functional(testf);
}
Check this item for more details.