因?yàn)槁?tīng)不懂封裝,刪掉了monodevelop筆記代碼全沒(méi)了,為了找回?zé)崆樗詠?lái)學(xué)c++
2017-07-14
&符號(hào)就是取地址符號(hào), 含義只這么一個(gè); *符號(hào)就是創(chuàng)建一個(gè)指針, 或者做一次地址跳轉(zhuǎn);
int a = 10; // 給a分配一個(gè)內(nèi)存邏輯地址0x100001, 這個(gè)地址存放了值10;
int *p = &a; //創(chuàng)建變量p, 給p分配地址0x100002, 這個(gè)地址存放的值是"0x100001"(a的邏輯地址值);
int *&q = p; //創(chuàng)建變量q, 給q分配地址也是0x100002, 因此這個(gè)地址存放的值還是a的邏輯地址值;
*q = 20; //訪問(wèn)存放在q變量地址下的值, 獲得了a的地址值, 再訪問(wèn)一下a的地址值, 修改上面的內(nèi)容為20;
int a = 10; // 給a分配一個(gè)內(nèi)存邏輯地址0x100001, 這個(gè)地址存放了值10;
int *p = &a; //創(chuàng)建變量p, 給p分配地址0x100002, 這個(gè)地址存放的值是"0x100001"(a的邏輯地址值);
int *&q = p; //創(chuàng)建變量q, 給q分配地址也是0x100002, 因此這個(gè)地址存放的值還是a的邏輯地址值;
*q = 20; //訪問(wèn)存放在q變量地址下的值, 獲得了a的地址值, 再訪問(wèn)一下a的地址值, 修改上面的內(nèi)容為20;
2017-07-10
正確答案:
#include <string.h>
#include <iostream>
using namespace std;
int main(void)
{
//在堆中申請(qǐng)100個(gè)char類型的內(nèi)存
char *str = new char[100];
//拷貝Hello C++字符串到分配的堆中的內(nèi)存中
strcpy(str, "Hello imooc");
//打印字符串
cout <<str<<endl;
//釋放內(nèi)存
delete []str;
str=NULL;
return 0;
}
#include <string.h>
#include <iostream>
using namespace std;
int main(void)
{
//在堆中申請(qǐng)100個(gè)char類型的內(nèi)存
char *str = new char[100];
//拷貝Hello C++字符串到分配的堆中的內(nèi)存中
strcpy(str, "Hello imooc");
//打印字符串
cout <<str<<endl;
//釋放內(nèi)存
delete []str;
str=NULL;
return 0;
}
關(guān)鍵字 inline 必須與函數(shù)定義體放在一起才能使函數(shù)成為內(nèi)聯(lián),僅將 inline 放在函數(shù)聲明前面不起任何作用。
2017-07-09
跟著老師學(xué),一步一步來(lái),別嫌簡(jiǎn)單,自己寫的:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(void)
{
int x = 10;
int &y = x;
cout << "x:" << x << endl << "y:" << y << endl;
y = 20;
cout << "After x:" << x << endl << "After y:" << y << endl;
system("pause");
return 0;
}
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(void)
{
int x = 10;
int &y = x;
cout << "x:" << x << endl << "y:" << y << endl;
y = 20;
cout << "After x:" << x << endl << "After y:" << y << endl;
system("pause");
return 0;
}
int getMax(int *&a,int n)
{
//定義一個(gè)變量并獲取數(shù)組的第一個(gè)元素
int max=a[0];
for(int i = 1; i < n; i++)
{
//比較變量與下一個(gè)元素的大小
if(max<a[i])
{
//如果數(shù)組中的元素比maxNum大,則獲取數(shù)組中的值
max=a[i];
}
}
return max;
}
{
//定義一個(gè)變量并獲取數(shù)組的第一個(gè)元素
int max=a[0];
for(int i = 1; i < n; i++)
{
//比較變量與下一個(gè)元素的大小
if(max<a[i])
{
//如果數(shù)組中的元素比maxNum大,則獲取數(shù)組中的值
max=a[i];
}
}
return max;
}