Home > database >  Why do we use pointer of object when we create a object?
Why do we use pointer of object when we create a object?

Time:01-25

Why do we use a pointer of object when we want to create a new object like this?

employee *Steve = new employee();

In this case, I want to create an object named Steve.

#include <iostream>
#include <string.h>
using namespace std;

class employee{
private:
    char name[40];
    int salary;
public:
    employee(){
        strcpy(name, "unnamed");
        salary = 0;
    }
    char *getName(){
        return name;
    }
};

int main(){
    employee *Steve = new employee();
    cout<<"Name: "<< Steve->getName();
}

CodePudding user response:

We do not use pointers when we do not need pointers. When we want to create an object we call a constructor to create an object, but we do not use new nor raw pointers.

employee Steve;
std::cout << "Name: " << Steve.getName() << "\n";

If we dynamically allocate the object then we still do not use new or raw poitners, but container or smart pointers.

In bad tutorials and questionable teaching they use pointers a lot. Often that is because they do not actually aim to teach C , but rather to teach pointers. In modern C the use of raw pointers is limited to rare cases.

There would be more to say about the code you wrote, but I'll leave it at that and refer you to The Definitive C Book Guide and List.

CodePudding user response:

One reason to use dynamic allocation (i.e. new in your example, although please don't use raw new, use safer options like std::make_unique instead) is for situations where the object (and therefore its size) to be created is not known at compile time.

For example:

struct Person {
   std::string m_strName;
   virtual void print() const { 
      std::cout << "Name: "<< m_strName << "\n"; 
   }
};

struct Employee : Person {
   int m_nSalary{};
   void print() const override { 
      Person::print();
      std::cout << "Salary: "<< m_nSalary << "\n"; 
   }
};

Now let's say which of the two classes is to be instantiated depends on the value of some variable i at run time:

Person* p = nullptr;
if (i > 10)
   p = new Person{};
else
   p = new Employee{};

In such cases we must use dynamic allocation.

If you don't use dynamic allocation, an object is placed on the stack (as opposed to the heap, where dynamic allocation puts them). Since stack space is limited, another reason to use dynamic allocation is the creation of very big objects. Yet another reason would be for objects that must persist beyond the current scope.

As a rule of thumb, if an object is only needed locally inside a function (and the concrete type is known at compile time), there's generally no need to use new (or one of the safer, modern alternatives like std::unique_ptr in conjunction with std::make_unique).

Following from that, it would be best to write your main function like so:

int main(){
    employee Steve{};
    cout<<"Name: "<< Steve.getName();
}

Again, if you have to use dynamic allocation, prefer std::unique_ptr:

auto pSteve = std::make_unique<employee>();
cout<<"Name: "<< pSteve->getName();
  • Related