【*和&】复习-杂七杂八代码收集

 
int main() { 
    int k = 12;//整数k,地址为x00CFFCA4
    int &c = k;//引用参数c,c及&c的值与k及&k相同
    int *w = &k;//指针参数指向k的地址,其本身地址为x00CFFC8C,指向k的地址为x00CFFCA4
    *w = 13;
    cout << "k1 = " << k << "           *k1 = " << &k << endl;
    cout << "w = " << w << "           *w = " << &w << endl;
    cout << "c = " << c << "           *c = " << &c << endl;
    cout << "k2 = " << k << "           *k2 = "<< &k << endl;
    c = 233;//通过改变c改变与之绑定的k的值
    cout << "c = " << c << "           *c = " << &c << endl;
    cout << "k3 = " << k << "           *k3 = " << &k << endl;

    system("pause");
    return 0;
}

 

应用举例】引用形参函数可避免不必要的拷贝。

 

 

 

  constexpr:

constexpr int Inc(int i) {
    return i + 1;
}
int main()
{
    constexpr int a = Inc(1); // ok
    
    constexpr int c = a * 2 + 1; // ok

    cout << a << c << endl;

    system("pause");

    return 0;
}

输出2和5.

 

 

定义在类内部的函数是隐式函数。3

 

C++ virtual函数

#include<iostream>
using namespace std;
class A
{
public:
    virtual void foo1()
    {
        cout << "A::foo() is called" << endl;
    }
    //如果不加virtual,a指向foo2会直接调用A中的foo2()
    virtual void foo2()
    {
        cout << "A::foo() is called" << endl;
    }
};
class B :public A
{
public:
    void foo2()
    {
        cout << "B::foo() is called" << endl;
    }
};
int main(void)
{
    A *a = new B();
    a->foo2();   // 在这里,a虽然是指向A的指针,但是被调用的函数(foo)却是B的!  
    getchar();
    return 0;
}

 纯虚函数声明方式:

virtual void funtion1()=0

没遇到过,暂时没看懂= - = 

posted @ 2017-05-19 13:41  Liez  阅读(178)  评论(0编辑  收藏  举报