c++ 中string变量的初始化

工程开发中遇到如下报错

libc++abi.dylib: terminating with uncaught exception of type std::logic_error: basic_string::_S_construct NULL

报错代码

PayTableModelLayer::PayTableModelLayer()

:m_sName(NULL)

{

//    for (int i = 0; i < MAX_SYMBOL_COUNT; i++)

//    {

//        m_nodePayTable[i] = NULL;

//    }

}

string m_sName
问题出在了m_sname是个字符串变量不应该初始化成NULL,指针初始化成NULL,查阅stackFlow给出如下解释
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.

share improve this question
 
 
string a = ""; –  Borgleader  Dec 16 '12 at 0:04 
 
try to use pointer, you can assign NULL to it, but dont forget to use properly new and delete –  Zaffy  Dec 16 '12 at 0:18
 
@Borgleader: What's wrong with string a;? –  sbi  Dec 16 '12 at 0:23
 
@sbi: Nothing, in fact it's probably better. –  Borgleader  Dec 16 '12 at 0:24

3 Answers

up vote 9 down vote accepted

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.

share improve this answer
 
 
nullptr will be UB, as it will try to construct a std::string from a null pointer, which is not allowed by the Standard. –  Puppy  Dec 16 '12 at 0:14
 
@DeadMG I think "as an aside" means it doesn't apply to this situation. –  Mark Ransom  Dec 16 '12 at 0:20
1 
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. –  Matteo Italia  Dec 16 '12 at 0:20 
1 
@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++. –  user142019  Dec 16 '12 at 0:21 
2 
@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. –  Matteo Italia  Dec 16 '12 at 0:27 

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 improve this answer
 

Let's break down what you are in fact doing:

string a=NULL;

First you execute string a. This creates a new object on the stack, with default value (an empty string). Then you execute a=NULL, which calls the assignment function of the string class. But what is NULLNULL in C++ is macro expanded into just 0. So you are attepting to assign an integer to a string variable, which of course is not possible.

string a="abc"

works, because you want to assign a char array, and the string class has the assignment operator method overloaded for char arrays, but not for integers. That's why NULL doesn't work and "abc" works.

share improve this answer
 
2 
This is not quite correct - a void* is not an integer. –  Timo Geusch  Dec 16 '12 at 0:11
 
In C, NULL is defined as ((void*)0) but in C++ its just 0. –  Zaffy  Dec 16 '12 at 0:13 
1 
std::string a = NULL; is an initialization, not an assignment. operator= isn't called (or at least not directly, depending on the implementation). –  user142019  Dec 16 '12 at 0:23 
2 
@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. –  user142019  Dec 16 '12 at 0:25 
2 
@JakubZaverka, a pointer is convertible to an numerical value like int or long, but it is a distinct type. That's a big difference. –  Timo Geusch  Dec 16 '12 at 0:28
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值