Incorrect values from operator casting with reference
I'm trying to make a float wrapper (which will also do some magic stuff in the constructor).
However I find myself with quite odd errors.
class Foo {
public:
Wrapper();
operator const float& () const { return m_bar; }
op开发者_如何学Cerator float& () { return m_bar; }
const float& Get() const { return m_bar; }
float& Get(){ return m_bar; }
private:
float m_bar;
};
if I use Get() then I get the correct value but if I just use the cast operator then I get random values.
Isn't it possible cast a reference to a member?
As you stated in your comment, you're passing the class to a vararg function (I guess it is vararg by the formating). The vararg passing does not work because it passes the address of the class and not a cast type (it doesn't know what it should cast it to when passing).
Using Get or an explicit cast is the only solution if you need to use varargs.
精彩评论