gogoWebsite

Create an object in C++

Updated to 8 hours ago

In C++, when creating any object (even if we create an object without any member variable), it requires a certain amount of memory space.

The application will divide the available memory (excluding the memory running in the source code, etc.) into two parts: stack and heap. So—there are two ways to create objects in C++: create objects on the stack and create objects on the heap.

Objects created on the stack have an automatic life cycle, and their life cycle is determined by the scope it declares. In other words, as long as the variable exceeds its scope, the memory of the object is freed.

Creating objects on the heap is different. The object created on the heap will stay there until you decide to free it and free its corresponding memory.

  —— "talk is cheap, show me the code."——

#include<iostream>#include<string>using String = std::string;
class Person{private:  String m_Name;public:  Person() :m_Name("Unknown") {}  Person(const String& name) :m_Name(name) {}  const String& GetName() const { return m_Name; }};

Suppose there is a class like Person, which has a private member variable m_Name and a public method GetName. The subsequent articles will explain the initialization list and other knowledge points, which will not affect the reading code for the time being.

Create object personOnStack on the stack.

int main(){  {// Create an object on the stack// Can be written as Person personOnStack = Person("person1");    Person personOnStack("person1"); //Print name person1    std::cout << () << std::endl;} // When the code runs to this line, personOnStack will be recycled}

Create object personOnHeap on the heap. ​​​​​

int main(){  {// Create an object on the heap Keyword new    Person *personOnHeap = new Person("person2");  }// Print name person1    std::cout << personOnHeap->GetName() << std::endl;// Objects allocated on the heap must be manually freed// That is, new and delete are used together  delete personOnHeap;  return 0;}

Let’s take the difficulty of “Yidiudiu” (involving the C++ pointer part, the so-called pointer, is essentially a piece of memory address)—

int main(){// Create a pointer of type Person  Person *p1, *p2;  {// Create an object on the stack// Can be written as Person personOnStack = Person("person1");    Person personOnStack("person1"); // Create an object on the heap Keyword new    Person *personOnHeap = new Person("person2");/*                                                                                                                              �* After braces, personOnStack will be recycled* Objects with m_name = person1 will not be obtained after braces     */    p1 = &personOnStack;    /* personOnHeap assigns value to p2* After braces, personOnHeap will not be recycled* After braces, the object of m_name = person2 will be obtained     */    p2 = personOnHeap;  }  // The print result is empty  std::cout << p1->GetName() << std::endl;//Print name person2  std::cout << p2->GetName() << std::endl;    // Objects allocated on the heap must be manually freed// That is, new and delete are used together  delete p2;// The reason why delete p1 is not needed here?  return 0;}

As shown in the above code, when the code is run into braces in the main function, the personOnStack object is released, so the m_Name obtained by p1 is empty; while the personOnHeap object created on the heap is not released, so p2 can obtain m_Name normally.

Finally, the reason why p1 does not need delete - after braces in the main function, the personOnStack object is released, so the address of the personOnStack object is also released, and the p1 pointer does not need to be deleted.

Today's content about creating objects in C++ ends here, I hope it will be helpful to everyone.