Using variables inside an object as locals?
Hi, lets say you have this class:
class A{
public:
float a,b,c;
};
And i have the following function:
void DoAnything(A *data){
//...
}
Is there an official way to use the variables in data as local variables:
a=0;
b=1;
c=2;
instead of:
data->a=0;
data->b=1;
data->c=2;
?
Any help is appreciated.
ps. Just to be clear, of course i know i could make it a member function of class A but that's not what i'm looking for :).
The only way you could do that (that I can think of) is by making it a member function. Why wouldn't you want to do that?
Because i have to use many variables of 2 classes in that function.
I just wondered if there's a way to save me a lot of unneccessary typing :).
I just wondered if there's a way to save me a lot of unneccessary typing :).
at first glance i'd recommend passing in a ref instead of a pointer...
not what you wanted but a bit more terse and much safer.
struct foo { int a, b, c; };void bar ( foo & f ) { f.a = 5; f.b = f.c; // etc...}
not what you wanted but a bit more terse and much safer.
a freind function will just give you private access. what he is really after i think is making do_anything a member function.
Quote:Original post by DrEvil
friend function?
I can already acces them, i just hoped there was a way to save me some time by accessing them differently.
Quote:Original post by DrEvila freind function will just give you private access. what he is really after i think is making do_anything a member function.
Well, unfortunately it isn't possible to have a 'shared' member function or anything.
Too bad, thanks anyway.
Couldn't you just at the top of the function do this..
float &fA = &object->a;
float &fB = &object->b;
and then access them like normal?
float &fA = &object->a;
float &fB = &object->b;
and then access them like normal?
Quote:Original post by segt
Couldn't you just at the top of the function do this..
float &fA = &object->a;
float &fB = &object->b;
and then access them like normal?
Yeah, but still a lot of work, nvm.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement