P*******b 发帖数: 1001 | 1 define a function that takes a string
void func(string s);
and then call this function with a char*
int main()
{
char* s = "test";
fun(s);
}
function fun() will get called.
do you know how this happens behind the scene? |
t*******i 发帖数: 4960 | 2 string ( const char * s ); is used to construct a string object |
P*******b 发帖数: 1001 | 3 what's this called?
How it works behind the scene?
【在 t*******i 的大作中提到】 : string ( const char * s ); is used to construct a string object
|
t*******i 发帖数: 4960 | 4 string ( const char * s ) is a constructor |
h**********c 发帖数: 4120 | 5 楼上说得这种现象在c++里叫temporary object.
也有可能是std::string 重载了type cast () operator.
可以看看_string 文件. |
P*******b 发帖数: 1001 | 6 我还是不太清楚,为啥会产生这个临时变量呢?是什么机制导致的。
【在 h**********c 的大作中提到】 : 楼上说得这种现象在c++里叫temporary object. : 也有可能是std::string 重载了type cast () operator. : 可以看看_string 文件.
|
d*******d 发帖数: 2050 | 7 这个动作叫auto type conversion by conversion constructor.
大概就这么叫吧,可能不准确,不过几个关键字都有了.
发生的原因是,string有个constructor take const char *为input.
【在 P*******b 的大作中提到】 : define a function that takes a string : void func(string s); : and then call this function with a char* : int main() : { : char* s = "test"; : fun(s); : } : function fun() will get called. : do you know how this happens behind the scene?
|
a********1 发帖数: 750 | 8 google explicit
explicit void func(string s);
explicit可以阻止这种行为的发生,
【在 P*******b 的大作中提到】 : define a function that takes a string : void func(string s); : and then call this function with a char* : int main() : { : char* s = "test"; : fun(s); : } : function fun() will get called. : do you know how this happens behind the scene?
|
t*******i 发帖数: 4960 | 9 恩,我的意思也是这个。
【在 d*******d 的大作中提到】 : 这个动作叫auto type conversion by conversion constructor. : 大概就这么叫吧,可能不准确,不过几个关键字都有了. : 发生的原因是,string有个constructor take const char *为input.
|
f*******t 发帖数: 7549 | 10 不知道过程确切名称是什么,不过只要能说出explicit关键字的作用应该就算答对了
【在 a********1 的大作中提到】 : google explicit : explicit void func(string s); : explicit可以阻止这种行为的发生,
|
l****s 发帖数: 165 | 11 C++ jobs:
http://jobguiding.com/it-jobs/it-skills/c++.html
【在 P*******b 的大作中提到】 : define a function that takes a string : void func(string s); : and then call this function with a char* : int main() : { : char* s = "test"; : fun(s); : } : function fun() will get called. : do you know how this happens behind the scene?
|
w*******x 发帖数: 489 | 12 According to Effective C++ chapter 0:
This is called "Implicit type conversion"
Define the constructor with key word explicit "explict string(const char *)"
will prohibit the implicit type conversion, but you can still do an
explicit type conversion by call func(string("test")).
【在 P*******b 的大作中提到】 : define a function that takes a string : void func(string s); : and then call this function with a char* : int main() : { : char* s = "test"; : fun(s); : } : function fun() will get called. : do you know how this happens behind the scene?
|