日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

【C++ grammar】对象指针、对象数组、函数参数

發(fā)布時(shí)間:2023/12/1 c/c++ 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【C++ grammar】对象指针、对象数组、函数参数 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目錄

  • 1、Object Pointer & Dynamic Object
    • 1. Accessing Object Members via Pointers
    • 2. Creating Dynamic Objects on Heap
  • 2、Array of Objects
    • 聲明方式
  • 3、Passing Objects to Functions
    • 1、Objects as Function Arguments (對(duì)象作為函數(shù)參數(shù))
    • 2. Objects as Function Return Value(對(duì)象作為函數(shù)返回值)
    • 3. Objects Pointer as Function Return Value(對(duì)象指針作為函數(shù)返回值)
    • 4. Objects Reference as Function Return Value(對(duì)象引用作為函數(shù)返回值)

1、Object Pointer & Dynamic Object

1. Accessing Object Members via Pointers

Object pointers can be assigned new object names
Arrow operator -> : Using pointer to access object members

Circle circle1; Circle* pCircle = &circle1; cout << "The radius is " << (*pCircle).radius << endl; cout << "The area is " << (*pCircle).getArea() << endl; (*pCircle).radius = 5.5; cout << "The radius is " << pCircle->radius << endl; cout << "The area is " << pCircle->getArea() << endl;

2. Creating Dynamic Objects on Heap

Object declared in a function is created in the stack, When the function returns, the object is destroyed 。
To retain the object, you may create it dynamically on the heap using the new operator.

Circle *pCircle1 = new Circle{}; //用無參構(gòu)造函數(shù)創(chuàng)建對(duì)象 Circle *pCircle2 = new Circle{5.9}; //用有參構(gòu)造函數(shù)創(chuàng)建對(duì)象 //程序結(jié)束時(shí),動(dòng)態(tài)對(duì)象會(huì)被銷毀,或者 delete pObject; //用delete顯式銷毀

2、Array of Objects

聲明方式

1、

Circle ca1[10];

2、用匿名對(duì)象構(gòu)成的列表初始化數(shù)組

Circle ca2[3] = { // 注意:不可以寫成: auto ca2[3]= 因?yàn)槁暶鲾?shù)組時(shí)不能用autoCircle{3}, Circle{ }, Circle{5} };

3、聲明方式3
用C++11列表初始化,列表成員為隱式構(gòu)造的匿名對(duì)象

Circle ca3[3] { 3.1, {}, 5 }; Circle ca4[3] = { 3.1, {}, 5 };

4、 聲明方式4
用new在堆區(qū)生成對(duì)象數(shù)組

auto* p1 = new Circle[3]; auto p2 = new Circle[3]{ 3.1, {}, 5 }; //p1 p2都是指針,*在auto的時(shí)候會(huì)自動(dòng)處理 delete [] p1; delete [] p2; p1 = p2 = nullptr;

上述代碼第4行若是改為 delete [] p1,會(huì)發(fā)生什么情況?

3、Passing Objects to Functions

1、Objects as Function Arguments (對(duì)象作為函數(shù)參數(shù))

You can pass objects by value or by reference. (對(duì)象作為函數(shù)參數(shù),可以按值傳遞也可以按引用傳遞)

(1) Objects as Function Return Value(對(duì)象作為函數(shù)參數(shù)) // Pass by value void print( Circle c ) {/* … */ }int main() {Circle myCircle(5.0);print( myCircle );/* … */ }

(2) Objects Reference as Function Return Value(對(duì)象引用作為函數(shù)參數(shù)) void print( Circle& c ) {/* … */ }int main() {Circle myCircle(5.0);print( myCircle );/* … */ }

(3) Objects Pointer as Function Return Value(對(duì)象指針作為函數(shù)參數(shù)) // Pass by pointer void print( Circle* c ) {/* … */ }int main() {Circle myCircle(5.0);print( &myCircle );/* … *、 }


2. Objects as Function Return Value(對(duì)象作為函數(shù)返回值)

// class Object { ... }; Object f ( /*函數(shù)形參*/ ){// Do somethingreturn Object(args); } // main() { Object o = f ( /*實(shí)參*/ ); f( /*實(shí)參*/ ).memberFunction();

3. Objects Pointer as Function Return Value(對(duì)象指針作為函數(shù)返回值)

// class Object { ... }; Object* f ( Object* p, /*其它形參*/ ){// Do somethingreturn p; } // main() { Object* o = f ( /*實(shí)參*/ ); // 不應(yīng)該delete o

盡可能用const修飾函數(shù)返回值類型和參數(shù)除非你有特別的目的(使用移動(dòng)語義等)。
const Object* f(const Object* p, /* 其它參數(shù) */) { }

4. Objects Reference as Function Return Value(對(duì)象引用作為函數(shù)返回值)

// class Object { ... }; class X {Object o;Object f( /*實(shí)參*/ ){// Do somethingreturn o;} } 可行的用法2 // class Object { ... }; Object& f ( Object& p, /*其它形參*/ ){// Do somethingreturn p; } // main() { auto& o = f ( /*實(shí)參*/ ); f( /*實(shí)參*/ ).memberFunction();

用const修飾引用類型的函數(shù)返回值,除非你有特別目的(比如使用移動(dòng)語義)
const Object& f( /* args */) { }
關(guān)于指針與引用的差別,請(qǐng)看這篇文章:
C++中引用和指針的區(qū)別

大概來講:
1、因此如果你有一個(gè)變量是用于指向另一個(gè)對(duì)象,但是它可能為空,這時(shí)你應(yīng)該使用指針;如果變量總是指向一個(gè)對(duì)象,i.e.,你的設(shè)計(jì)不允許變量為空,這時(shí)你應(yīng)該使用引用。
2、引用不可以改變指向,但是指針可以改變指向,而指向其它對(duì)象。

#include<iostream> using namespace std; int main(int argc,char** argv) {int i=10;int& ref=i;ref++;cout<<"i="<<i<<endl;cout<<"ref="<<ref<<endl;int j=20;ref=j;ref++;cout<<"i="<<i<<endl;cout<<"ref="<<ref<<endl;cout<<"j="<<j<<endl;return 0; }

對(duì)ref的++操作是直接反應(yīng)到所指變量之上,對(duì)引用變量ref重新賦值"ref=j",并不會(huì)改變r(jià)ef的指向,它仍然指向的是i,而不是j。理所當(dāng)然,這時(shí)對(duì)ref進(jìn)行++操作不會(huì)影響到j(luò)。

總結(jié)

以上是生活随笔為你收集整理的【C++ grammar】对象指针、对象数组、函数参数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。