Two ways to create objects in C + +

Source: Internet
Author: User

C + + creates objects in two ways, creating objects on a stack (Objects on the stack) and creating objects on a heap (Objects on the heap).

Let's say we have the following classes:

1 #include <string> 2 using std::string; 3  4 class spreadsheetcell{5 public:6     void SetValue (double invalue); 7     double GetValue (); 8     void Setstri Ng (string instring); 9     String getString (), Protected:12     string doubletostring (double invalue), and     double stringtodouble ( String instring);     double mvalue;16     string mstring;17};

As well as the following CPP files:

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) ;}12 double Spreadsheetcell::getvalue () {     mvalue;15}16-void spreadsheetcell::setstring (string instring) {     mstring = instring;19     mvalue = stringtodouble (mstring);}21 string Spreadsheetcell:: GetString () {     mstring;24}25-string Spreadsheetcell::d oubletostring (double invalue) {     Ostringstream ostr;28     ostr<<invalue;29     return ostr.str (); +}31 double Spreadsheetcell:: Stringtodouble (String instring) {     double temp;34     istringstream istr (instring); Notoginseng     istr>> temp;38     if (istr.fail () | |!istr.eof ()) {$         return (0); +     }41     return temp;43}

1. Create the object on the stack (Objects on the stack):

Grammar:

1 ClassName ObjName1, ObjName2 (PARAMETER01)//But never ojbname ()

As the name implies, objects created in this way, memory is allocated to the stack (stack). Use "." Not "--" method to invoke the object. When the extent of the use of the object is left (such as the end of the method, the last {} of a degree block), the objects in the range stack are automatically deleted and the memory is automatically reclaimed. This is the simplest way to create an object, with an "int x = 0;" is the same. As in the following example:

Spreadsheetcell MyCell, Anothercell;mycell.setvalue (6); Anothercell.setvalue (Mycell.getvalue ()); cout << "cell 1: "<< mycell.getvalue () << endl;cout <<" Cell 2: "<< anothercell.getvalue () << endl;//des Troy: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. Create objects on the heap (Objects on the heap):

Grammar:

ClassName *obj1 = new ClassName (); ClassName *obj2 = new ClassName (parameter);d elete Obj1;delete obj2;

Objects created in this way, memory is allocated to the heap (heap). The method of invoking an object is generally used. The arrow operator, dereferencing*, and the member use (Member access.) Combine, the following example two output, the effect is equivalent:

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     Delete mycellp;12     return 0;13}

Objects in the heap are not automatically deleted, the memory is not automatically reclaimed, so the new object is used, you must call delete and free up memory space. In other words, new and delete must appear in pairs.

By the way, there are three ways to allocate memory
(1) Allocation from a static storage area. Memory is allocated at the time of program compilation, and the entire running period of the program is present in this block. For example, global variables, static variables.
(2) Create on the stack. When executing a function, the storage unit of the local variable within the function can be created on the stack, and the memory space of those local variables is reclaimed after the function execution ends. Allocating memory space on the stack is highly efficient, but allocates a limited amount of memory.
(3) allocated from the heap. When the program is running, it uses malloc or new to request any amount of memory, and the programmer is responsible for freeing the memory with free or delete.

Two ways to create objects in C + +

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.