get object idetifier

Dec 25, 2017 at 3:53am
i want to get the identifier of a object

class placeholder {
string identifier = identifier
public:
void print(){
cout << identifier << endl;
}
}
int main() {
placeholder name;
name.print();
}

output:
name

so if that's the code it should give me the identifier and output the identifier
im looking for any help anyone can give
Last edited on Dec 25, 2017 at 3:54am
Dec 25, 2017 at 5:05am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>

struct some_class
{
    std::string identifier ;

    some_class( std::string id = "no_name" ) : identifier(id) {}

    std::string unique_id() const
    { return identifier + '@' + std::to_string( std::uintptr_t(this) ) ;}
};

#define OBJECT_OF_SOME_CLASS(identifier) some_class identifier{#identifier}

int main()
{
    OBJECT_OF_SOME_CLASS( object_orange ) ;

    struct A
    {
        OBJECT_OF_SOME_CLASS( object_orange ) ; // same id, different scope
    };

    static A a ;

    // both objects have the same identifier
    std::cout << object_orange.identifier << '\n' // object_orange
              << a.object_orange.identifier << '\n' ; // object_orange

    // but they have different unique_ids
    std::cout << object_orange.unique_id() << '\n' // object_orange@xxxxxxx
              << a.object_orange.unique_id() << '\n' ; // object_orange@yyyyyyy

}

http://coliru.stacked-crooked.com/a/2f4dd38bd0b7c231
http://rextester.com/LFT59113
Topic archived. No new replies allowed.