Is there any possible way to determine whether a reference variable refers to a class member (and then determine which class it belongs to) instead of an ordinary variable? Here's a simple example that hopefully shows what I'm getting at:
class A
{
private:
unsigned int x;
public:
A() : x(15) { }
unsigned int& GetX() { return x; }
};
int main( void )
{
A a;
unsigned int y = 12;
unsigned int& yr = y;
unsigned int& xr = a.GetX();
// Is there anyway of identifying, using typeid() or something
// similar, whether xr refers to x inside class A or is its type
// completely indistinguishable from that of yr? i.e. does its type
// information contain anything along the lines of
// typeid(xr).name() == "A::unsigned int&" that identifies it as
// belonging to class A?
return 0;
}
EDIT:
No, there's no way to so distinguish. Why would you want to? Sounds suspiciously like an XY problem.
OK, perhaps my example was too simplistic and maybe I'm not asking the right the question so let me give you a more detailed example:
Considering class A above. In most cases I would want to use the getter method to just query the value of x thus returning a constant reference is usually what is needed (in my real code x is actually a very large vector or matrix so returning by value might be slow). But in some special cases I may want to be able to change the value of x - i.e. inside of a user specified function that is binded to a function wrapper as per a previous question: C++: Generic function wrapper class as a member of a non-template class. So, when binding the function to the function wrapper, the argument that will be modified by the user function is supplied using the getter method and a helper class to remove the const qualifier, e.g.
A.BindFunc( UserFunc, WriteAccess::GetNonConstRef( A.GetX() ) );
where the WriteAccess
helper class is as follows:
class WriteAccess
{
public:
template <typename T>
static T& GetNonConstRef( const T& x )
{
return const_cast<T&>(x);
}
};
And the user supplied function might be:
void UserFunc( unsigned int& X )
{
X = somethingelse;
}
However, I would like to prevent the WriteAccess::GetNonConstRef()
method from being used with any old class member, and only allow it to be used with members of class A (and derived classes). So I was wondering if there is anyway to determine, inside WriteAccess::GetNonConstRef()
, which class the supplied reference belongs to so that either it doesn't compile or execution is terminated if a different class is used.
So I was thinking if there is some way to distinguish between references to ordinary variables and those of class members (and indeed references to members of class A and references to members of other classes) then this might help me.
How to distinguish between a reference to a class member and a reference to an ordinary variable?
In general, this is impossible. A reference doesn't hold the information of an objects container. A sub-object is indistinguishable from a stand alone object.
typeid
will not be of any help. The fully qualified name of a variable is not part of the type name of the variable.
However, it is possible to test whether a reference (or a pointer) refers to a particular member of a particular container instance:
bool xr_refers_to_x_of_a = &xr == &a.GetX(); // true