【C++】 67_经典问题分析 五

发布时间:2019-06-15 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了【C++】 67_经典问题分析 五脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

面试问题

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

指针的判别

  • 拾遗

    • C++ 中仍然支持 C 语言中的可变函数参数
    • C++ 编译器的匹配调用优先级

      1. 重载函数
      2. 函数模板
      3. 变参函数

  • 思路

    • 将变量分为两类: 指针 VS 非指针
    • 编写函数:

      • 指针变量调用时返回 true
      • 非指针变量调用时返回 false

  • 函数模板与变参函数的化学反应
template
< typename T >
bool IsPtr(T* v)    // match pointer
{
    return true;
}

bool IsPtr(...)     // match non-pointer
{
    return false;
}

编程实验: 指针判断

#include @H_275_126@<iostream>

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 i = 0;
    int* p = &i;
    
    cout << "p is a pointer: " << IsPtr(p) << endl;
    cout << "i is a pointer: " << IsPtr(i) << endl;
    
    Test t;
    Test* pt = &t;
    
    cout << "pt is a pointer: " << IsPtr(pt) << endl;
    cout << "t is a pointer: " << IsPtr(t) << endl;      // 注意这里!对象传入变参函数

    return 0;
}
编译输出:
test.cpp: In functionint main()’:
test.cpp:40: warning: cannot pass objects of non-POD typeclass Test’ through ‘...’; call will abort at runtime

运行输出:
p is a pointer: 1
i is a pointer: 0
pt is a pointer: 1
非法指令
  • 存在的缺陷:

    • 变参函数无法解析对象参数,可能造成程序崩溃!!

  • 进一步的挑战:

    • 如何让编译器精准匹配函数,但不进行实际的调用?

编程实验: 指针判断改进

#include <iostream>

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 i = 0;
    int* p = &i;
    
    cout << "p is a pointer: " << ISPTR(p) << endl;
    cout << "i is a pointer: " << ISPTR(i) << endl;
    
    Test t;
    Test* pt = &t;
    
    cout << "pt is a pointer: " << ISPTR(pt) << endl;
    cout << "t is a pointer: " << ISPTR(t) << endl;

    return 0;
}
输出:【无警告,无错误】
p is a pointer: 1
i is a pointer: 0
pt is a pointer: 1
t is a pointer: 0

分析: sizeof(IsPtr(t)) 发生了什么?

  • sizeof 是编译器的内置指示符;
  • 在编译过程中所有的 sizeof 将被具体的数值所替换;
  • 程序的执行过程与 sizeof 没有任何关系;

编译器根据匹配规则匹配到具体的函数 int IsPtr(...);
sizeof(IsPtr(t)); 编译器将计算函数返回值类型所占用的大小;
使用计算出的数值整体替换 sizeof(IsPtr(t)),因此不会发生实际的运行调用。
不会出现变参函数解析对象参数而出现的程序运行时错误。

面试问题 二

如果构造函数中抛出异常会发生什么情况呢?

构造中的异常

  • 构造函数中抛出异常

    • 构造过程立即停止
    • 当前对象无法生成
    • 析构函数不会被调用
    • 对象所占用的空间立即收回

  • 工程中的建议

    • 不要在构造函数中抛出异常
    • 当构造函数可能产生异常时,使用二阶构造模式

编程实验: 构造中的异常

#include <iostream>

using namespace std;

class Test
{
public:
    Test()
    {
        cout << "Test() begin ..." << endl;
        
        throw 0;
        
        cout << "Test() end ..." << endl;
    }
    
    virtual ~Test()
    {
        cout << "~Test()" << endl;
    }
};

int main()
{
    Test* p = reinterpret_cast<Test*>(1);
    
    cout << "p = " << p << endl;

    try
    {
        p = new Test();
    }
    catch(...)
    {
        cout << "Exception..." << endl;
    }
    
    cout << "p = " << p << endl;

    return 0;
}
输出:
p = 0x1
Test() begin ...
Exception...
p = 0x1

注意:
"Test() end ..." 没有打印 ==> 证明构造过程停止;
"~Test()"        没有打印 ==> 证明析构函数不会被调用;
p 的值没有发生改变         ==> 证明对象没有生成;

内存分析:

g++ -g test.cpp
valgrind --tool=memcheck --leak-check=full ./a.out

输出:对象所占用的内存空间被收回

==28776== HEAP SUMMARY:
==28776==     in use at exit: 0 bytes in 0 blocks
==28776==   total heap usage: 2 allocs, 2 frees, 104 bytes allocated
==28776== 
==28776== All heap blocks were freed -- no leaks are possible

析构中的异常

  • 避免在析构函数中抛出异常!!
  • 析构函数的异常将导致: 对象所使用的资可能无法完全释放

编程实验: 析构中的异常

#include <iostream>

using namespace std;

class Test
{
private:
    int* m_pointer;
public:
    Test()
    {
        cout << "Test()" << endl;
        
        m_pointer = new int(0);
    }
    
    virtual ~Test()
    {
        cout << "~Test() begin ..." << endl;
        
        throw 0;
        
        delete m_pointer;
        
        cout << "~Test() end ..." << endl;
    }
};

int main()
{
    try
    {
        Test();
    }
    catch(...)
    {
        cout << "Exception..." << endl;
    }
    
    return 0;
}
输出:
Test()
~Test() begin ...
Exception...

小结

  • C++ 中依然支持变参函数
  • 变参函数无法很好的处理对象参数
  • 利用函数模板和变参函数能够判断指针变量
  • 构造函数和析构函数中不要抛出异常

以上内容参考狄泰软件学院系列课程,请大家保护原创

脚本宝典总结

以上是脚本宝典为你收集整理的【C++】 67_经典问题分析 五全部内容,希望文章能够帮你解决【C++】 67_经典问题分析 五所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。