-5

Example code:

    bool running = true;
    while (running == true) {
        int x;
        cout << "Enter an integer: ";
        cin >> x;
        string y;
        cout << "Enter a string: ";
        cin >> y;
        first.S_Return(x);
        first.S_Return(y);
        x=null; // null is not a recognised integer
        y=null;
        system("PAUSE");
       }

What I'm wondering is,

If I wanted to re-initialize any data types to a value of null, such as when they are being declared is there any way to do this like a clear, delete, set to null method ect. Or would do I need to delcare a pointer and place them on the heap using new and delete?

TrevorLee
  • 231
  • 2
  • 13
  • `x = 0; y = 0;` ought to do it. – alter_igel Jul 08 '18 at 21:12
  • 2
    There is no _initialization to null_ in c++. – πάντα ῥεῖ Jul 08 '18 at 21:12
  • 5
    "_If I wanted to re-initialize any data types to a value of null, such as when they are being declared_" There is no null-value for such types. When you declare variables, the value of built-in types are indeterminate, while for class types (such as `std::string`) - the default constructor is invoked. So one cannot re-initialize to null, since they were not initialized as null, in the first place. Consider learning from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Algirdas Preidžius Jul 08 '18 at 21:13
  • Only pointers are nullable, and the value you set them to is `nullptr`, not `null`. And if you think C++ is anything like Java, you are utterly wrong - the two languages could hardly be more different –  Jul 08 '18 at 21:19
  • The reason I'm looking into this if you place a value inside a stack variable and then pass to a function in a loop it will pass again, this requires ternary operations and variable changes where a null or similar may improve efficiency. – TrevorLee Jul 08 '18 at 21:22
  • 1
    @TrevorLee Please elaborate on why do you think "nullable variables" would improve efficiency (allocations on the heap via the `new`, and `delete`, as you suggested is more pricey, than stack allocations). That, currently, doesn't make a lot of sense. – Algirdas Preidžius Jul 08 '18 at 21:26
  • purely because I want to write as little code as possible to achieve what ever it is I'm working towards, but I do understand you and I didn't mean to point as improved efficiency maybe more at code elegance and general discovery of what features are available in c++. – TrevorLee Jul 08 '18 at 21:31
  • If you want do *default initialise*, then you can do `x = int();` as well... – Aconcagua Jul 08 '18 at 21:40
  • @TrevorLee In that case, don't you think that even setting the variables to "null" (or to whatever value) provides extra code, which wouldn't impact the overall execution of the program? And hence, it is also against your goal to write as little code, as possible? – Algirdas Preidžius Jul 08 '18 at 21:41

1 Answers1

3

C++ provides no notion of "uninitialized", and it even rarely defines a common notion of "explicitly undefined".

Concerning "uninitialized", a variable in local scope that is not of class type is not initialized, and there is no way to detect at runtime if such a variable got initialized or not. It is rather subject to the programmer to access such variables only if they got assigned a value before. Otherwise, the behaviour of the program is undefined.

Concerning "explicitly undefined", it is usually subject to the programmer to treat one particular value in the range of the respective data type as "undefined", if necessary. For example, for a variable "int numOfChildren", you might define a value of -1 to express that the number of children is unknown or not evaluated. For pointers, in contrast, there is a constant called nullptr, which is commonly used as a value explicitly expressing an undefined state. But note: a declaration like int *x will not set the value of x to nullptr unless x is defined at file scope (i.e. is a global variable).

Hope it helps.

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58