jeudi 9 mars 2017

C++: How to distinguish between a reference to a class member and a reference to an ordinary variable?

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;
}

Aucun commentaire:

Enregistrer un commentaire