字符串和NULL参数传递 [英] strings and NULL argument passing

查看:133
本文介绍了字符串和NULL参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我对将值传递给接受字符串的函数有疑问。


======== ==============================

#include< iostream>

using namespace std;


int main()

{

void print(const string&);

print(" hi");

print(NULL);

返回0;

}

void print(const string& s)

{

cout<<" s"<<< s<< endl;

}

================================== ====


以上程序编译成功但在运行时失败,因为

NULL在第二次打印调用中作为参数传递。


为什么编译器在传递NULL值时没有出错?


我们如何在程序中检查这些参数使用

字符串?


问候

Sanjay Raghani

解决方案

" sanjay" < sa ************ @ gmail.com在留言新闻中写道:e6 ************************** ******** @ h23g2000 prf.googlegroups.com ...





我有关于将值传递给接受字符串的函数的疑问。


=========================== ===========

#include< iostream>

使用命名空间std;


int main()

{

void print(const string&);

print(" hi");

print(NULL);

返回0;

}

void print(const string& s)

{

cout<<" s's"<<<<<<<<<< endl;

}

=== ===================================


以上程序成功编译但在运行时失败,因为

NULL在第二次print调用中作为参数传递。



我不这么认为。你检查过了吗?

您可以通过在功能打印中打印s的地址来检查它。

我认为在打印之前发生了错误。

编译器尝试创建一个临时字符串并传递该字符串的地址进行打印。

print(" hi");编译为print(string(" hi"));.

根据您对NULL的定义,

print(NULL);可以编译为print(string(NULL));.

这可能在创建临时期间失败,而不是在函数打印中。



为什么编译器在传递NULL值时不会出错?


当我们使用时,我们如何在程序中检查这些参数? br />
字符串?



你不能。在函数启动之前发生错误。

你应该先检查一下这个论点。


sanjay写道:


= =====================================

#include< iostream> ;

使用命名空间std;


int main()

{

void print(const string&);

print(" hi");

print(NULL);

返回0;

}

void print(const string& s)

{

cout<<" s"<<< s<< endl;

}

=========================== ===========

上面的程序编译成功,但在运行时失败,因为

NULL作为参数传递给第二个打印电话。


为什么编译器在传递NULL值时没有错误?



从语义上讲,NULL可以转换为std :: string,就像hi一样。可以。

编译器怎么知道NULL不是有效值?


我们如何在程序中检查这些参数当我们使用

字符串?



除了为const char *提供

函数的重载外,我没有办法做到这一点。问题是在std :: string的构造函数中,错误已经在输入打印之前已经显示



BTW:我使用的实现引发了类型异常std :: logic_error

在这种情况下,但我不认为这是标准所要求的。


11月13日,下午3:19,Rolf Magnus< ramag ... @ t-online.dewrote:


sanjay写道:


======================================

#include< iostream>

using namespace std;


int main()

{

void print(const string&);

print(" hi");

print(NULL);

返回0;

}

void print(const string& s)

{

cout<<" s是"<< s<< endl ;

}

================================ ======


上述程序成功编译但在运行时失败,因为

NULL作为参数传递在第二次打印打印。


为什么编译器在传递NULL值时不会出错?



从语义上讲,NULL可以转换为std :: string,就像hi一样。可以。

编译器怎么知道NULL不是有效值?


我们如何在程序中检查这些参数当我们使用

字符串?



除了为const char *提供

函数的重载外,我没有办法做到这一点。问题是在std :: string的构造函数中,错误已经在输入打印之前已经显示



BTW:我使用的实现引发了类型异常std :: logic_error

在这种情况下,但我不认为这是标准所要求的。



嗨Rolf,


感谢您的回复..


可以你详细说明你在哪里抛出异常

处理这种情况?


问候

Sanjay Raghani


Hi,

I have a doubt about passing values to a function accepting string.

======================================
#include <iostream>
using namespace std;

int main()
{
void print(const string&);
print("hi");
print(NULL);
return 0;
}
void print(const string& s)
{
cout<<"s is "<<s<<endl;
}
======================================

The above program compiles successfully but fails at run time because
NULL is passed as argument in the second call to print.

Why doesn''t the compiler give an error on passing of NULL value?

How could we check for such arguments in our program when we are using
strings?

Regards
Sanjay Raghani

解决方案

"sanjay" <sa************@gmail.comwrote in message news:e6**********************************@h23g2000 prf.googlegroups.com...

Hi,

I have a doubt about passing values to a function accepting string.

======================================
#include <iostream>
using namespace std;

int main()
{
void print(const string&);
print("hi");
print(NULL);
return 0;
}
void print(const string& s)
{
cout<<"s is "<<s<<endl;
}
======================================

The above program compiles successfully but fails at run time because
NULL is passed as argument in the second call to print.

I don''t think so. Did you check it?
You can check it by printing the address of s in the function print.
I think that the error occurs before print is called.
The compiler tries to create a temporary string and passes the address of that string to print.
print ("hi"); is compiled as print (string ("hi"));.
Depending on you definition of NULL,
print (NULL); may be compiled as print (string (NULL));.
This may fail during the creation of the temporary, not in the function print.


Why doesn''t the compiler give an error on passing of NULL value?

How could we check for such arguments in our program when we are using
strings?

You can''t. The error occurs before your function starts.
You should check the argument earlier.


sanjay wrote:

======================================
#include <iostream>
using namespace std;

int main()
{
void print(const string&);
print("hi");
print(NULL);
return 0;
}
void print(const string& s)
{
cout<<"s is "<<s<<endl;
}
======================================

The above program compiles successfully but fails at run time because
NULL is passed as argument in the second call to print.

Why doesn''t the compiler give an error on passing of NULL value?

Well, semantically, NULL can be converted to std::string, just as "hi" can.
How could the compiler know that NULL isn''t a valid value?

How could we check for such arguments in our program when we are using
strings?

I don''t see a way to do that, except for providing an overload of the
function for const char*. The problem is that the error already manifests
before print is even entered, in the constructor of std::string.
BTW: The implementation I use throws an exception of type std::logic_error
in this case, but I don''t think that this is required by the standard.


On Nov 13, 3:19 pm, Rolf Magnus <ramag...@t-online.dewrote:

sanjay wrote:

======================================
#include <iostream>
using namespace std;

int main()
{
void print(const string&);
print("hi");
print(NULL);
return 0;
}
void print(const string& s)
{
cout<<"s is "<<s<<endl;
}
======================================

The above program compiles successfully but fails at run time because
NULL is passed as argument in the second call to print.

Why doesn''t the compiler give an error on passing of NULL value?


Well, semantically, NULL can be converted to std::string, just as "hi" can.
How could the compiler know that NULL isn''t a valid value?

How could we check for such arguments in our program when we are using
strings?


I don''t see a way to do that, except for providing an overload of the
function for const char*. The problem is that the error already manifests
before print is even entered, in the constructor of std::string.
BTW: The implementation I use throws an exception of type std::logic_error
in this case, but I don''t think that this is required by the standard.

Hi Rolf,

Thanks for the reply..

Can you elaborate bit more where exactly do you throw the exception
for handling such situation?

Regards
Sanjay Raghani


这篇关于字符串和NULL参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆