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_)();
Related
Is there any technique or compiler extension keyword to declare class member variables inside class member functions? Something like
struct test_t{
void operator ()(){
instance_local int i = 0;
}
};
The best that came in my mind was using thread_local and then executing the member function inside another thread, but this would be too ugly to be useful.
EDIT: example
Well I'm really sorry for the following probably confusing example (it is related to my question yesterday Is there any problem in jumping into if(false) block?). I really tried to make a less confusing up...
#include <iostream>
#define instance_local thread_local
struct A{
A(int i) :
i(i)
{
}
void dosomethinguseful(){
std::cout << i << std::endl;
}
int i;
};
struct task1{
int part;
task1() : part(0){}
void operator ()(){
int result_of_calculation;
switch (part) {
case 0:{
//DO SOME CALCULATION
result_of_calculation = 5;
instance_local A a(result_of_calculation);
if(false)
case 1:{ a.dosomethinguseful();}
part++;
}
default:
break;
}
}
};
int main(){
task1 t;
t();
t();
return 0;
}
instance_local A a(result_of_calculation); that is what i could get from such a keyword instead of making a smart pointer for a.
You're describing a coroutine. Here a rough draft of what it could look like (I'm not an expert in coroutine)
auto task1() -> some_awaitable_type {
result_of_calculation = 5;
A a(result_of_calculation);
co_yield;
a.dosomethinguseful();
}
This could be called like this:
some_awaitable_type maybe_do_something = task1();
// calculation done here
// dosomethinguseful called here
co_await maybe_do_something();
There is not. The compiler needs to know the structure of the class without compiling all the method implementations. If you could slip instance_local int foo into a method body, that would make the size of the data structure 4 bytes larger.
On a more principled level, it's not good to hide data. The equivalent feature for global variables that you might be thinking of, static local variables, is a carryover from C that is widely considered to be an anti-pattern:
Why are static variables considered evil?
Not directly, no.
You could define a:
static std::map<test_t*, int> is;
…where the first part of each element is a this pointer.
But, why?
Make a member variable.
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
I have a function like this:
void functiont(int a, int b)
{
if(playingnumber=="T")
{
returntransfer=1;
struct A
{
~A() // Destructor to run after returning variable
{
void cardselecth(int playingcolorh, int playingnumberh);
}
}
return returntransfer;
}
}
Anyway I need to grab that returned variable without calling on int a or int b. So in another function I write
newvar=functiont(int a, int b);
And it gives a compile error. I don't know how else to do this. I could write functiont(a,b); and I get an error; I write functiont(int,int); and get an error. I've tried writing functiont(); but then it assumes the function's in the same file and I haven't defined it, which it isn't (I'm transferring across files here, so I need to define any parameters so it knows to refer to another file).
I'm not sure what you want to do there, but at first this function is a void function so it can only return but not return a value. So your signature, respectively the void must be changed according to the type you want to return.
In this case
int functiont(int a, int b)
would be appropriate.
When calling the function you have to pass it two arguments of type integer.
newvar = function(1, 2);
It's a good idea to post the error, too. Best regards
If you prefer to return an int, return type of your function should be int not void. I corrected the code as follows. Note the changes with comment
int functiont(int a, int b)
{
int returntransfer=0; //change , declaration out of if block
// to maintain the scope of variable
if(playingnumber=="T")
{
returntransfer =1;
}
return returntransfer;//change
}
You can declare a struct variable inside a function, but it don't mean just by declaration it will be get called.
struct A
{
~A() // Destructor to run after returning variable
{
void cardselecth(int playingcolorh, int playingnumberh);
}
};//change added ; at end of declaration.
EDIT
Adding complete program for answer to further questions:
#include<iostream>
#include<string>
int functiont(int a, int b)
{
int returntransfer=0; //change , declaration out of if block
// to maintain the scope of variable
std::string playingnumber ="T"; //Added again for completness
if(playingnumber=="T")
{
returntransfer =1;
}
struct A
{
~A() // Destructor to run after returning variable
{
void cardselecth(int playingcolorh, int playingnumberh);
}
};//change added ; at end of declaration.
return returntransfer;//change
}
int main()
{
std::cout<<functiont(2,3);
}
I'm looking into the LLVM source code and I never encountered the following syntax:
class BasicBlock {
public:
typedef iplist<Instruction> InstListType;
private:
InstListType InstList;
static iplist<Instruction> BasicBlock::*getSublistAccess(Instruction*) {
return &BasicBlock::InstList;
}
}
what does the above define? At first it seemed a normal static function but I don't understand the BasicBlock::* part. Seems like a static function which returns a member function pointer and that directly executes that member function's code.
The return type of static member function getSublistAccess is
iplist<Instruction> BasicBlock::*
that is, a pointer to a non-static data member of class BasicBlock, where the data type is iplist<Instruction>.
What getSublistAccess actually returns is &BasicBlock::InstList, that is exactly a non-static data member of class BasicBlock, where the data type is InstListType. i.e., iplist<Instruction>.
Is a function pointer.
You can read this article for detail.
Thanks to iavr for the answer. I'm awarding the answer to him but I'd like to add some details here which will hopefully help someone reading this post.
What I asked and as iavr explained to me, might be understood with the following code:
#include <iostream>
using namespace std;
struct customType {
int b;
};
struct Instruction {};
class BasicBlock {
public:
BasicBlock(int a) { InstList.b = a; }
customType InstList;
static customType BasicBlock::*getSublistAccess(Instruction*) {
return &BasicBlock::InstList;
}
};
int main() {
BasicBlock bb(90);
Instruction justForSignature;
// Get a pointer to a member of type customType through the static function
customType BasicBlock::* ptrToMember = BasicBlock::getSublistAccess(&justForSignature);
cout << (bb.*ptrToMember).b; // Parenthesis are necessary, '.' has higher precedence on *
// Output: 90
return 0;
}
Try it out: http://ideone.com/hYgfh8