• 如何判断一个变量是不是指针


    1.编写一个程序判断一个变量是不是指针?

    拾遗
    -C++中仍然支持C语言中的可变参数函数
    -C++编译器的匹配调用优先级
    1.重载函数
    2.函数模板
    3.变参函数

     

    复制代码
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Test
    {
    public:
        Test()
        {
        }
        virtual ~Test()
        {
        }
    };
    
    template
    <typename T>
    bool IsPtr(T* v) // match pointer
    {
        return true;
    }
    
    bool IsPtr(...)  // match non-pointer
    {
        return false;
    }
    
    
    int main(int argc, char *argv[])
    {
        int i = 0;
        int* p = &i;
    
        cout << "p is a pointer: " << IsPtr(p) << endl;    // true
        cout << "i is a pointer: " << IsPtr(i) << endl;    // false
    
        Test t;
        Test* pt = &t;
    
        cout << "pt is a pointer: " << IsPtr(pt) << endl;    // true
        cout << "t is a pointer: " << IsPtr(t) << endl;    // false
    
        return 0;
    }
    复制代码

     

    复制代码
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Test
    {
    public:
        Test()
        {
        }
        virtual ~Test()
        {
        }
    };
    
    template
    <typename T>
    char IsPtr(T* v) // match pointer
    {
        return 'd';
    }
    
    int IsPtr(...)  // match non-pointer
    {
        return 0;
    }
    
    #define ISPTR(p) (sizeof(IsPtr(p)) == sizeof(char))
    
    int main(int argc, char *argv[])
    {
        int i = 0;
        int* p = &i;
    
        cout << "p is a pointer: " << ISPTR(p) << endl;    // true
        cout << "i is a pointer: " << ISPTR(i) << endl;    // false
    
        Test t;
        Test* pt = &t;
    
        cout << "pt is a pointer: " << ISPTR(pt) << endl;    // true
        cout << "t is a pointer: " << ISPTR(t) << endl;    // false
    
        return 0;
    }
    复制代码
     
  • 相关阅读:
    requirejs 加载其它js
    springmvc 国际化
    企业QQ客服的添加
    js验证身份证号码
    JQUERY获取当前页面的URL信息
    lnmp、lamp、lnmpa一键安装包(Updated: 2015-10-25)
    php生成代金券码
    JS控制文本框textarea输入字数限制的方法
    ps 换图片的背景颜色
    读取数据库配置文件
  • 原文地址:https://www.cnblogs.com/bruce1992/p/14322437.html
Copyright © 2020-2023  润新知