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

时间:2022-05-14 17:48:09
 
int main() {
int k = ;//整数k,地址为x00CFFCA4
int &c = k;//引用参数c,c及&c的值与k及&k相同
int *w = &k;//指针参数指向k的地址,其本身地址为x00CFFC8C,指向k的地址为x00CFFCA4
*w = ;
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 = ;//通过改变c改变与之绑定的k的值
cout << "c = " << c << " *c = " << &c << endl;
cout << "k3 = " << k << " *k3 = " << &k << endl; system("pause");
return ;
}

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

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

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

constexpr:

constexpr int Inc(int i) {
return i + ;
}
int main()
{
constexpr int a = Inc(); // ok constexpr int c = a * + ; // ok cout << a << c << endl; system("pause"); return ;
}

输出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 ;
}

纯虚函数声明方式:

virtual void funtion1()=

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