简体   繁体   中英

Determining the type of a variable

You are given a variable p defined in C++ program. Is it possible to find out if it's a pointer to something or a normal variable? specifically, suppose there is a class Test

    class Test
    {
    public:
        int test1(int i)
        {
            
        }
        static int test2(int i)
        {
        }
const int test3(int i)
    {
    }

}

Now `this` is a variable accessible inside test1 and test2 and test3, But I want to determine the type of `this` inside each of these functions. It could be `Test,` `Test const,` `Test* const` etc...
How can I figure this out myself, or is there any alternate documentation for the same?

You can use, having written #include<type_traits>

std::is_const<decltype(this)>::value

within a member function. If that member function is const then this value will be true , else it will be false .

Note there is no this in a static member function, and that this is always a pointer type in C++.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM