c++中參考型別,參考是如何參考的呢?
- 參考
- 參考概念,給變數起個別名,本質是給記憶體空間取個別名
- 參考是c++的語法概念、參考的意義(好用)
- 參考本質:有地址、定義時必須初始化,c++編譯器內部按照指標常量
- 參考結論:間接賦值成立的三個條件的后兩步和二為一
- 參考使用原則:當用參考時,我們不去關心編譯器參考是怎么做的;當分析奇怪的語法現象時,才去考慮c++編譯器是怎么做的
- 函式回傳值是參考(若回傳堆疊變數,不能成為其他參考的初始化)
- 函式回傳值當左值,必須回傳一個參考
- 指標的參考int getT(Teacher * &myp )
- 常參考
- 用變數初始化const參考,然變數形參擁有只讀屬性
- 用字面量初始化const參考,額外的分配記憶體空間
- 參考概念,給變數起個別名,本質是給記憶體空間取個別名
- 參考是c++的語法概念、參考的意義(好用)
- 參考本質:有地址、定義時必須初始化,c++編譯器內部按照指標常量
- 參考結論:間接賦值成立的三個條件的后兩步和二為一
- 參考使用原則:當用參考時,我們不去關心編譯器參考是怎么做的;當分析奇怪的語法現象時,才去考慮c++編譯器是怎么做的
- 函式回傳值是參考(若回傳堆疊變數,不能成為其他參考的初始化)
- 函式回傳值當左值,必須回傳一個參考
- 指標的參考int getT(Teacher * &myp )
- 常參考
_ 用變數初始化const參考,然變數形參擁有只讀屬性
_ 用字面量初始化const參考,額外的分配記憶體空間
這與c大有不同
1 參考沒有定義, 是一種關系型宣告,宣告它和原有某一變數(物體)的關
系,故 而型別與原型別保持一致, 且不分配記憶體,與被參考的變數有相同的地
址,
2 宣告的時候必須初始化, 一經宣告, 不可變更,
3 可對參考, 再次參考,多次參考的結果, 是某一變數具有多個別名,
4 & 符號前有資料型別時, 是參考,其它皆為取地址
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
/*
*/
void change_value(int *p) // int p = a;
{
*p = 30;
}
void change_value2(int & r)//int &r = a
{
r = 30; //a = 30
}
int main(void)
{
int a = 20;
int b = 30;
int *p = &a;
*p = 30;
p = &b;
*p = 20;//b
int &re = a; //int & 使用參考資料型別, re就是a的別名
re = 50;
&re;
&a;
re = b; //讓re成為b的參考? a = b
re = 50;
cout << "a =" <<a << endl;
cout << "b = " << b << endl;
int & re2 = b; //參考一定要初始化,
re2 = a;
int &re3 = re;
re3 = 100;
cout << "a =" << a << endl;
cout << "re =" << re << endl;
cout << "re3 =" << re3 << endl;
cout << "-------------" << endl;
cout << "a =" << a << endl;
change_value(&a);//改變了值
cout << "a =" << a << endl;
a = 100;
cout << "-------------" << endl;
cout << "a =" << a << endl;
change_value2(a);//改變了值
cout << "a =" << a << endl;
return 0;
}
參考示例
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
struct student
{
int id;
char name[64];
};
void my_swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void my_swap2(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
void printS(struct student s) //student s = s1; 結構體整個值拷貝的動作
{
cout << s.id <<" "<< s.name << endl;
s.id = 100;
}
void printS1(struct student *sp)
{
cout << sp->id << " " << sp->name << endl;
sp->id = 100;
}
//參考在一定條件下能夠取代指標的作業,也能作為函式引數,
void printS2(struct student &s)//student &s = s1;
{
cout << s.id << " " << s.name << endl;
s.id = 300;
}
int main(void)
{
int a = 10;
int b = 20;
my_swap2(a, b);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
student s1 = { 10, "zhang3" };
printS(s1);
printS1(&s1);
printS2(s1);
cout << "si.id =" << s1.id << endl;
return 0;
}
參考的本質
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
struct typeA
{
int &a;
};
struct typeB
{
int *a;
};
struct student
{
int id;
char name[64];
};
void motify(int *const a)//int *const a = main::&a
{
*a = 300;
}
void motify2(int &a) //當我們將參考作為函式引數傳遞的時候,編譯器,會替我們將實參,取地址給參考
//int &a = main::&a
{
a = 300; //對一個參考操作 賦值的時候,編譯器提我們隱藏*操作,
}
//如果我們在去研究參考的時候,你可以將參考當做一個常指標去研究
//當你在使用參考編程的時候,你就把參考理解為變數的別名就可以了,
int main(void)
{
cout << "sizeof(struct typeA)" << sizeof(struct typeA) << endl;
cout << "sizeof(struct typeB)" << sizeof(struct typeB) << endl;
//參考所占用的大小 跟指標是相等的,
int a = 10;
int &re = a; //常量要初始化,參考也要初始化, 參考可能是一剛常量,
int *const p = &a;
//綜上兩點, 參考 可能是一個常指標,
motify(&a);
motify2(a);
return 0;
}
參考作為函式的回傳值
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
char * getmem(int num)
{
char *p = NULL;
p = (char*)malloc(num);//分配空間
return p;//ox11223344
}//0x1231321
int getmem2(char **pp, int num)
{
char *p = NULL;
p = (char*)malloc(num);
*pp = p;
return 0;
}//0
int getA1()
{
int a = 10;
return a;
}//a的值
void getA2(int *a)
{
*a = 10;
}
//參考作為回傳值,不要回傳區域變數的參考,
int& getA3()
{
int a = 10;
return a;
}//int &temp = a;
int &getA4()
{
static int a = 10;
return a;
}
int main(void)
{
int a = 0;
char *pp = NULL;
a = getA1();
pp = getmem(10);
cout << "-----------" << endl;
int main_a = 0;
main_a = getA3(); //main_a = temp; //數值拷貝
cout << "main_a " << main_a << endl;
cout << "-----------" << endl;
#if 0
int &main_a_re = getA3();
cout << "main_a_re " << main_a_re << endl;
cout << "main_a_re " << main_a_re << endl;
#endif
int &main_a_re = getA4();
cout << "main_a_re " << main_a_re << endl;
cout << "main_a_re " << main_a_re << endl;
//參考如果當函式回傳值的話,函式可以當左值,
getA4() = 1000;
return 0;
}
指標參考
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
struct teacher
{
int id;
char name[64];
};
int get_mem(struct teacher** tpp)
{
struct teacher *tp = NULL;
tp = (struct teacher*) malloc(sizeof(struct teacher));
if (tp == NULL) {
return -1;
}
tp->id = 100;
strcpy(tp->name, "li4");
*tpp = tp;
return 0;
}
void free_teacher(struct teacher **tpp)
{
if (tpp == NULL) {
return;
}
struct teacher *tp = *tpp;
if (tp != NULL) {
free(tp);
*tpp = NULL;
}
}
int get_mem2(struct teacher* &tp)
{
tp = (struct teacher*)malloc(sizeof(struct teacher));
if (tp == NULL) {
return -1;
}
tp->id = 300;
strcpy(tp->name, "wang5");
return 0;
}
void free_mem2(struct teacher * &tp)
{
if (tp != NULL) {
free(tp);
tp = NULL;
}
}
int main(void)
{
struct teacher *tp = NULL;
get_mem(&tp);
cout << "id =" << tp->id << ", name = " << tp->name << endl;
free_teacher(&tp);
cout << "00000000000" << endl;
get_mem2(tp);
cout << "id =" << tp->id << ", name = " << tp->name << endl;
free_mem2(tp);
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/80742.html
標籤:C++
上一篇:c++-const
