'Accessing static members inside property class

It is very difficult for me to formulate what I need - sorry - so please make some changes to the question if you think it might make it better understandable for others.

I will create a short example of what I would like to do:

I have a class that has a static function and an object member:

class A
{
public:
    Member x;

    static std::string getName()
    {
        return "Name of A";
    }
};

This is the definition of Member:

class Member
{
public:
    void printSomething()
    {
        cout<<"I am a member of "<<ClassWhereIAmAMember::getName()<<endl; //what can I use instead of ClassWhereIAmAMember??
    }
};

My final result should be that I can use Member like this:

int main()
{
    A a;
    a.x.printSomething();
}

And the final result should be obviously

I am a member of Name of A

I know I could change Member to a template class and declaring it in A like Member<A> x but I would like to keep this as simple and as clean as possible. And in my opininion the compiler should be able to deduce it from the context.

Unfortunately I can't think of any method/paradigm how to accomplish this. Is there a way?



Solution 1:[1]

Thanks for all your comments, especially the one about reflection. I did some research and came up with the following solution. It is not perfect since it uses macros, but it does exactly what I needed. So if you know a solution without macros please post an answer.

My solution is to define Member as a macro:

#define Member struct Member_ \
{ \
    void printSomething() \
    { \
        std::string name = getName();  \
        cout<<"I am a member of "<<name<<endl; \
    } \
}

This way i can use it like

int main()
{
    A a;
    a.x.printSomething();
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Thomas Sparber