This question already has answers here:
How to call through a member function pointer?
(2 answers)
Closed 8 years ago.
I have a C++ class which contains the following definition:
class SomeClass:
public BaseClass
{
public:
SomeClass();
bool SomeClass::MyFunc( Json::Value& jsonRoot)
typedef bool(SomeClass::*PFUNC)(Json::Value&);
std::map<std::string, PFUNC> m_map;
}
later on in the c++ code i add values to the map variable using the following line:
SomeClass::SomeClass()
{
m_map["func"] = &SomeClass::MyFunc;
}
and for execution within one of SomeClass's methods:
std::map<std::string, PFUNC>::iterator itFunction = m_map.find("func");
if (itFunction != m_map.end())
{
PFUNC pfParse = m_map["func"];
Json::Value x;
this->*pfParse(x);
}
I end up getting the following compilation error:
error C2064: term does not evaluate to a function taking 1 arguments
I even tried using the iterator explicitly - this->*iterator->second(...) but ended up with the same error.
what am i doing wrong?
thanks
() has higher precedence than ->* so pfParse(x) is evaluated first. You need to use parenthesis to sequence the evaluation:
(this->*pfParse)(x);
Related
This question already has answers here:
Function overloading by return type?
(14 answers)
Closed 5 years ago.
Following is a c++ code for copying a binary tree. I am trying to overload copy function. I think it should work because return type of these two functions is different.
node* copy(node *onode,node *cnode)
{
if(root==NULL)
root=onode;
if(onode)
{
cnode=new node;
cnode->data=onode->data;
cnode->left=copy(onode->left,cnode->left);
cnode->right=copy(onode->right,cnode->right);
return cnode;
}
return cnode;
}
void copy(node *onode,node* cnode)
{
onode=copy(onode,cnode);
}
However, I get following error at compilation.
error: ‘void tree::copy(node*, node*)’ cannot be overloaded
void copy(node onode,node cnode)
error: with ‘node* tree::copy(node*, node*)’
node* copy(node *onode,node *cnode)
Thanks.
Return types can be different only if parameters are different according to rules of overloading
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.
This question already has answers here:
How to call through a member function pointer?
(2 answers)
Closed 5 years ago.
I'm trying to call a pointer to a member function, which I retrieve from a map. The call th.fpObjHandler(md, evd, tokenIdx) seems erroneous, I've tried various different syntaxes (e.g. .* and ->*), but I cannot seem to get it right. Hopefully somebody here is able to help.
struct TokenHandler
{
int tokenIdx;
eventDataStatus_t (Schema::*fpObjHandler)(MessageData &md, EventDataImpl &evd, int &tokenIdx);
};
Schema::event(MessageData &md, EventDataImpl &evd)
{
int tokenIdx = 0;
std::map<std::string, TokenHandler> tokenHandlerMap;
tokenHandlerMap["timeInterval"] = { -1, &Schema::timeInterval };
// ..
// ....
TokenHandler th = tokenHandlerMap.at(key);
if (th.fpObjHandler != NULL) {
th.fpObjHandler(md, evd, tokenIdx); // THIS RESULTS IN ERROR
//..
//...
}
}
eventDataStatus_t Schema::timeInterval(MessageData &md, EventDataImpl &evd, int &tokenIdx)
{
//..
//...
return OK;
}
Schema.cpp:111:54: error: must use ‘.’ or ‘->’ to call
pointer-to-member function in ‘th.TokenHandler::fpObjHandler (...)’,
e.g. ‘(... ->* th.TokenHandler::fpObjHandler) (...)’
th.fpObjHandler(md, evd, tokenIdx);
^
First you need an instance of the class Schema, then use .* or ->*, something like:
Schema schema;
(schema.*th.fpObjHandler)(md, evd, tokenIdx);
Or, as you are already in a method of Schema:
(this->*th.fpObjHandler)(md, evd, tokenIdx);
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).
This question already has answers here:
Do class functions/variables have to be declared before being used?
(5 answers)
Closed 6 years ago.
is it possible to use a member variable of a class before declaring it?
here is my code.
using namespace std;
class Computer
{
public:
Computer()
{
processor_speed = 0;
}
~Computer();
void setspeed (int);
int getspeed (void);
private:
int processor_speed;
};
/*Computer::Computer()
{
processor_speed = 0;
} */
Computer::~Computer()
{
cout<<"this is a destructor"<<endl;
}
void Computer:: setspeed(int p)
{
processor_speed = p;
}
int Computer::getspeed(void)
{
return processor_speed;
}
int main(void)
{
Computer obj;
cout<<"processor speed is "<<obj.getspeed()<<endl;
obj.setspeed(100);
cout<<"processor speed is now "<<obj.getspeed()<<endl;
return 0;
}
as u can see here i was able to use variable processor_speed before declaring it.
i saw a similar question here: Do class functions/variables have to be declared before being used?
but i was not able to understand the reason why this code work.
Thanks
Yes, you can do it.
A member variable is in scope for member functions of your class even if it is textually after its first point of use. The compiler translates your code in several "passes". One could think of it as getting all member variables first, and only then translating member functions, with all declarations in place.
Note that this is not allowed for "free-standing" global and static variables inside translation units: a declaration must precede the first use, otherwise you get an error.