zoukankan      html  css  js  c++  java
  • C++ 小复习

    (1)当数组作为函数的参数进行传递时,该数组自动退化为同类型的指针。所以这里要注意函数里面的sizeof运算符。

    (2)打印出当前源文件的文件名以及源文件的当前行号: cout << __FILE__ ; cout<<__LINE__ ; __FILE__和__LINE__是系统预定义宏,这种宏并不是在某个文件中定义的,而是由编译器定义的。

    (3)如何判断一段程序是由C 编译程序还是由C++编译程序编译的?

    #ifdef __cplusplus
        cout << "C++";
    #else
        cout << "C";
    #endif // _cplusplus

    (4)C++虚析构函数的作用:当用一个基类的指针删除一个派生类的对象时,派生类的析构函数会被调用。

     1 #include <iostream>
     2 
     3 using std::cout;
     4 using std::endl;
     5 using std::cin;
     6 
     7 class Base
     8 {
     9 public:
    10     Base() {}
    11     virtual ~Base(){ cout << "base detruction"; }
    12 };
    13 
    14 class Derived : public Base
    15 {
    16 public:
    17     Derived() : Base() {}
    18     ~Derived() { cout << "derived destruct
    "; }
    19 };
    20 
    21 
    22 int main(void)
    23 {
    24     Base* pBase = new Derived;
    25     delete pBase;
    26 
    27     cin.get();
    28 }
    View Code
    derived destruct
    base detruction

    上面是输出,先调用派生类的析构函数,再调用基类的构造函数,满意。但是如果去掉了基类的virtual,那么输出为:

    base detruction

    可以看出没有调用派生类的析构函数,只调用了基类的析构函数。

  • 相关阅读:
    从string类的实现看C++类的四大函数 [写的很好]
    毕业5年决定你的命运
    git push 原因以及问题!
    poj 1195 Mobile phones 夜
    poj 2886 Who Gets the Most Candies 夜
    poj Asimple Problem With Integers 夜
    poj 2750 Potted Flower 夜
    poj 2528 Mayor's posters 夜
    poj 2777 Count Color 夜
    poj 2482 Stars in Your Window 夜
  • 原文地址:https://www.cnblogs.com/jiayith/p/3985363.html
Copyright © 2011-2022 走看看