您的位置:首页 > 编程语言 > C语言/C++

C++创建对象的两种方式

2017-11-11 18:05 337 查看
C++创建对象有两种方式,在栈上创建对象(Objects on the Stack)和在堆上创建对象(Objects on the Heap)。

假设我们有以下的类:

1 #include <string>
2 using std::string;
3
4 class SpreadsheetCell{
5 public:
6     void setValue(double inValue);
7     double getValue();
8     void setString(string inString);
9     string getString();
10
11 protected:
12     string doubleToString(double inValue);
13     double stringToDouble(string inString);
14
15     double mValue;
16     string mString;
17 };


以及如下的cpp文件:

1 #include "stdafx.h"
2 #include "SpreadsheetCell.h"
3 #include <iostream>
4 #include <sstream>
5
6 using namespace std;
7
8 void SpreadsheetCell::setValue(double inValue){
9     mValue = inValue;
10     mString = doubleToString(mValue);
11 }
12
13 double SpreadsheetCell::getValue(){
14     return mValue;
15 }
16
17 void SpreadsheetCell::setString(string inString){
18     mString = inString;
19     mValue = stringToDouble(mString);
20 }
21
22 string SpreadsheetCell::getString(){
23     return mString;
24 }
25
26 string SpreadsheetCell::doubleToString(double inValue){
27     ostringstream ostr;
28     ostr<<inValue;
29     return ostr.str();
30 }
31
32 double SpreadsheetCell::stringToDouble(string inString){
33     double temp;
34
35     istringstream istr(inString);
36
37     istr>>temp;
38     if(istr.fail() || !istr.eof()){
39         return (0);
40     }
41
42     return temp;
43 }


1. 在栈上创建对象(Objects on the Stack):

语法:

1 ClassName ObjName1, ObjName2(parameter01)// But never OjbName()


顾名思义,用这种方法创建的对象,内存分配到栈里(Stack)。使用 “.” 非 “->” 调用对象的方法。当程度离开对象的使用范围(如方法结束,一个程度块的最后{}),范围内的栈中的对象会自动删除,内存自动回收。这是创建对象最简单的方式,与“int x = 0;”是一样的。如下面例子:

SpreadsheetCell myCell, anotherCell;
myCell.setValue(6);
anotherCell.setValue(myCell.getValue());
cout << “cell 1: “ << myCell.getValue() << endl;
cout << “cell 2: “ << anotherCell.getValue() << endl;

//destroy:
int main(int argc, char** argv)
{
SpreadsheetCell myCell(5);
if (myCell.getValue() == 5) {
SpreadsheetCell anotherCell(6);
} // anotherCell is destroyed as this block ends.
cout << “myCell: “ << myCell.getValue() << endl;
return (0);
} // myCell is destroyed as this block ends.

//destroy in reverse order:
{
SpreadsheetCell myCell2(4);
SpreadsheetCell anotherCell2(5); // myCell2 constructed before anotherCell2
} // anotherCell2 destroyed before myCell2


2.在堆上创建对象(Objects on the Heap):

语法:

ClassName *obj1 = new ClassName();

ClassName *obj2 = new ClassName(parameter);

delete obj1;

delete obj2;


用这种方法创建的对象,内存分配到堆里(Heap)。一般使用“->” 调用对象的方法。箭头操作符”->"将解引用(dereferencing*)和成员使用(member access.)结合起来,下例两个输出,效果等价:

1 int _tmain(int argc, _TCHAR* argv[])
2 {
3     SpreadsheetCell *myCellp = new SpreadsheetCell();
4
5     myCellp->setValue(3.7);
6
7     cout<<"cell 1: "<<myCellp->getValue()<<" "<<myCellp->getString()<<endl;
8
9     cout<<"cell 1: "<<(*myCellp).getValue()<<" "<<(*myCellp).getString()<<endl;
10
11     delete myCellp;
12     return 0;
13 }


在堆中的对象不会自动删除,内存不会自动回收,所以new一个对象使用完毕,必须调用delete,释放内存空间。也就是说,new和delete必须成对出现。

顺便提提,内存的分配方式有三种
(1)从静态存储区域分配。内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在。例如全局变量,static 变量。
(2) 在栈上创建。在执行函数时,函数内局部变量的存储单元都可以在栈上创建,函数执行结束后在将这些局部变量的内存空间回收。在栈上分配内存空间效率很高,但是分配的内存容量有限。
(3) 从堆上分配的。程序在运行的时候用 malloc 或 new 申请任意多少的内存,程序员自己负责在何时用 free 或 delete 释放内存。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: