I'm trying to use output streams to output biunary data, with some of that data being private class variables. I'm atrying to code it in a way where I can cut out having to copy the values to local variables before doing so, because it might be more efficient
This is the only way I know that works:
I've tried numerous things without success:Code:class A { private: int _x; public: void setX(int val) { _x = val; } int x() { return _x; } }; int main() { A a; a.setX(89); int x = a.x(); // trying to see if I can avoid making this copy file.write(reinterpret_cast<const char*>(&x), sizeof(int)); return 0; }
- returning int & doesn't work like this: char * p = reinterpret_cast<char*>(a.x()) because C++ interprets it as a value for some reason
- returning const int * doesn't work either
Is the only way to do this to make a wrapper function of some sort, expose the private vars, or copy them to local vars?