-2

I am having kind of a trouble adapting a program from C# to C++. It's very commom given a class to assign it the null value. But C++ is not accepting the equivalent form 'nullptr'

class Point{
     public:
         int x,y;
}
//...
Point p = nullptr;

There is some way of solving it?

  • 6
    `p` is not a pointer in your example. Please use(refer to) a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Jason May 20 '22 at 14:32
  • You'll need a pointer to do that: `Point* p = nullptr;` But you shouldn't really do that in c++. The memory management is completely different from c#. – πάντα ῥεῖ May 20 '22 at 14:33
  • 4
    Classes are value types in C++. You are trying to do C++ with a c# mindset, which will and can not work – DownloadPizza May 20 '22 at 14:34
  • 3
    "Adapting" alternate language code from one to another is a recipe for bad code. The most success you'll have comes from (a) know *both* languages *well*, (b) understand the *problem(s)* being solved by the former, and finally (c) properly implementing a solution to those problems in the latter. I suspect the root problem here is (a), as class-types are handle fundamentally differently in C# compared to C++. – WhozCraig May 20 '22 at 14:36
  • If you intend to assign new objects to that pointer in the future, you must keep in mind that in C++ memory is unmanaged, you'll be the garbage collector, so to speak. – anastaciu May 20 '22 at 14:41
  • ... so better not to do that at all. Instead, read one of the books in @Anoop's link and then start over. – Paul Sanders May 20 '22 at 14:42
  • You might be interested in [std::optional](https://en.cppreference.com/w/cpp/utility/optional) – Nathan Pierson May 20 '22 at 15:31
  • General C++ consens is to not create a `Point` until you are ready to initialize it. Having such "null" objects is a strong indicator that you are trying to declare the variable too early. – Goswin von Brederlow May 20 '22 at 17:15

2 Answers2

1

The problem is that p is a non-pointer type in your example. This means that it cannot be initialized with a nullptr. The type of p is Point.

You can make p a pointer to a Point so that you can initialize it with a nullptr as shown below:

Point *p = nullptr; //p is a pointer to non-const Point object

Or

If you want to create an object of the class type Point then one way of doing so is as follows:

Point p{1, 3};

I would also recommend referring to a good C++ book for a better understanding of the concept.

Jason
  • 36,170
  • 5
  • 26
  • 60
1

You cannot assign nullptr to a class because a class is a type and not a variable. What you're doing by typing Point p = nullptr; is that you're asking the compiler to find a constructor of Point which accepts a nullptr.

You can either create an object of Point as an automatic variable by doing this:

Point p = Point{1, 2}; // sets x = 2 and y = 3

or simply like this:

Point p;

for which however you will need to define a constructor which initializes x and y because you haven't provided the member variables with default values.

Depending on the context in which this statement resides, p will either be allocated on the heap or on the stack. In case you're instantiating p in the main function, it will be the latter.

You can create a pointer to an object of Point by doing this:

Point* p = nullptr;

However, you will only have created a pointer. You will need to further take care of allocation, which you can do like this:

Point* p = new Point{1, 2};

in which case you should also free the allocated memory before your program ends like so:

delete p;
Nitin Malapally
  • 534
  • 2
  • 10