string initializing as NULL in C++

20,167

Solution 1

Actually to get an empty std::string, you just write

std::string a;

std::string's default constructor will give you an empty string without further prompting.

As an aside, using NULL in C++ is generally discouraged, the recommendation would be to either use 0 (which NULL tends to be defined to anyway) or if you have a modern enough compiler, nullptr.

Solution 2

There is a difference between null and empty string (an empty string is still a valid string). If you want a "nullable" object (something that can hold at most one object of a certain type), you can use boost::optional:

boost::optional<std::string> str; // str is *nothing* (i.e. there is no string)
str = "Hello, world!"; // str is "Hello, world!"
str = ""; // str is "" (i.e. empty string)
Share:
20,167
user1559792
Author by

user1559792

Updated on December 27, 2020

Comments

  • user1559792
    user1559792 over 3 years
    string a=NULL;
    

    it gives error. Why and how can I initialize string as NULL?

    but when I write

    string a="foo";
    

    this it works fine.

    • Zaffy
      Zaffy over 11 years
      try to use pointer, you can assign NULL to it, but dont forget to use properly new and delete
    • sbi
      sbi over 11 years
      @Borgleader: What's wrong with string a;?
    • Borgleader
      Borgleader over 11 years
      @sbi: Nothing, in fact it's probably better.
  • Zaffy
    Zaffy over 11 years
    In C, NULL is defined as ((void*)0) but in C++ its just 0.
  • Puppy
    Puppy over 11 years
    nullptr will be UB, as it will try to construct a std::string from a null pointer, which is not allowed by the Standard.
  • Fergus In London
    Fergus In London over 11 years
    Note the typecast (void*)0 - which means a pointer to 0; not am integer representation of 0. In C++ I think it largely depends on the compiler.
  • Puppy
    Puppy over 11 years
    No, it is just zero. There are plenty of other wrongs in this answer.
  • Mark Ransom
    Mark Ransom over 11 years
    @DeadMG I think "as an aside" means it doesn't apply to this situation.
  • Matteo Italia
    Matteo Italia over 11 years
    As an aside, I don't see how using NULL is discouraged in pre-nullptr C++... in practice it's the same as 0 (it's guaranteed to be defined as 0), and it makes more clear that we are talking about pointers.
  • Admin
    Admin over 11 years
    @MatteoItalia foo(NULL) will it call foo(char*) or foo(int)? You'd say foo(char*) because NULL is used for pointers, but it's actually calling foo(int). Clear code is important. Don't ever use NULL in C++.
  • Jakub Zaverka
    Jakub Zaverka over 11 years
    @Timo I edited the question, but I still claim that a pointer is an integer (or long). Otherwise you would not be able to perform pointer arithmetics on it.
  • Admin
    Admin over 11 years
    std::string a = NULL; is an initialization, not an assignment. operator= isn't called (or at least not directly, depending on the implementation).
  • Jakub Zaverka
    Jakub Zaverka over 11 years
    @DeadMg please enlighten me.
  • Zaffy
    Zaffy over 11 years
    "abc" is also an "integer" (pointer to ROM address where "abc" is)
  • Admin
    Admin over 11 years
    @Zaffy no. It's an array of chars. It decays to a pointer, and how that pointer is stored (if it is stored at all) is implementation-defined. It may be a floating point number on crazy architectures.
  • Matteo Italia
    Matteo Italia over 11 years
    @Zoidberg'--: no, I'd say foo(int) because I know that NULL is plain zero. But if I make the mistake and later someone else notices that the wrong overload is called he can immediately understand what I meant and add the relevant cast (otherwise he would have to ask me if I meant "integer zero" or "pointer zero"). I agree that NULL is a botch - nullptr was introduced for a reason, but I feel it's better than using 0 also for pointers. Anyway, all this is both non relevant to the question (I'm sorry for derailing it) and non relevant in general now that nullptr solves this problem.
  • Timo Geusch
    Timo Geusch over 11 years
    @JakubZaverka, a pointer is convertible to an numerical value like int or long, but it is a distinct type. That's a big difference.