1

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();
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 11
    In C++ (and unlike in some other OO languages), you don't *have* to use pointers: `employee Steve;` is valid. – dan04 Jan 24 '23 at 16:55
  • 3
    We do that only if we learned some other language first, like Java, or C# and have a mistaken impression that C++ works the same way, where each object must be a pointer. It doesn't, in C++ objects do not have to be always referenced indirectly, via a pointer. – Sam Varshavchik Jan 24 '23 at 17:01
  • 1
    Read about [dynamic memory allocation](https://cplusplus.com/doc/tutorial/dynamic/). When you use `A* a = new A();`, `a` points to an object, allocated on the heap. `A a;` creates an object on the stack. – Ivan Venkov Jan 24 '23 at 17:02
  • 1
    To add to @dan04's comment, in C++ if you write `new` stop and look for a better way - standard library collections, smart pointers etc. Manually allocating (managing) memory should be a last resort. – Richard Critten Jan 24 '23 at 17:02
  • 2
    @IvanVenkov Minor niggle: [Heap and stack are an implementation details](https://stackoverflow.com/q/9181782/4581301). As far as C++ is concerned dynamic allocations can come from Care Bear blood. – user4581301 Jan 24 '23 at 17:05
  • 2
    Keep in mind that `new` and `delete` are advanced tools. You still have to know how they work, but you won't use them often in everyday programming. – HolyBlackCat Jan 24 '23 at 17:09
  • Good question. I've often wondered why many people post code on this site that is using pointers and doesn't **need** to. – Thomas Matthews Jan 24 '23 at 18:04
  • *Why do we use a pointer of object when we want to create a new object like this?* That is a good question. For an **owning** pointer, it'd be better to use a `std::unique_ptr`. The reason to use a dynamic object with an owning smart pointer is because it will outlive the context of its scope. An automatic object gets destroyed at the end of its scope. The code provided creates a dynamic object, but does not destroy that dynamic object... so it has a leak. And the object does not need to outlive its scope. – Eljay Jan 24 '23 at 18:15
  • @HolyBlackCat Part of the problem is quite a few beginners to C++ with a background in other languages (C#, Java, etc) assume that C++ works like those languages so believe (almost) everything has to be initialialised with `new` - so start using pointers prematurely. Another part of the problem is that a *lot* of (poor quality) introductory texts on C++ introduce and encourage usage of `new` and `delete` before they introduce other facilities of C++ (standard containers, smart pointers, etc) [or never introduce much, if anything, of the standard library beyond iostreams]. – Peter Jan 26 '23 at 01:05

1 Answers1

7

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.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185