Not possible; or, at least, not in any convenient way that would accomplish the spirit of what you want. You're accessing the data members of the structure "foo" so you need to specify that that's what you're doing. If you were to just write "x" "y" or "z" then you'd be accessing a variable in the local scope called that, rather than the variables that belong to the object foo.
Consider if you wrote
triple foo, foo2;
Each one of foo and foo2 have their own x, y and z. If you were to write just x y or z there would be no distinguishing which one you're referring to.
In short, you'll just have to learn to use (and appreciate) C++'s method of distinguishing variables, or forgo the structure altogether and just declare them as local variables. (Something which would only be situationally appropriate.)
While it's far too trivial to actually use in this case, you can use a reference. This is something I do use when dealing with over large, deelply nested structures which require me to use some or other subelement multiple times for a calculation. Not nice... (When I code, I use classes and let them do their own calculations!)
#include <iostream>
usingnamespace std;
inlineint sqr(int val) {
return val * val;
}
class triple {
private:
int m_x;
int m_y;
int m_z;
public:
triple(int x, int y, int z)
: m_x(x), m_y(y), m_z(z) {
}
int doOwnCalc() const {
int res = 0;
res = sqr(m_x) + sqr(m_y) + sqr(m_z);
return res;
}
};
int main() {
triple bar(3, 2, 1);
int ret = bar.doOwnCalc();
cout << "ret = " << ret << endl;
return 0;
}