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


     
    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

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

  • 相关阅读:
    [转]MyBatis传入多个参数的问题
    【转】赶集网mysql开发36军规
    C#套接字和windowsAPI套接字
    java中的注解
    java中的枚举类型
    过去的汇编代码
    近日错误集锦
    java swing模仿随机频谱
    java泛型中的对象
    XML-RPC远程方法调用
  • 原文地址:https://www.cnblogs.com/liez/p/6877945.html
Copyright © 2020-2023  润新知