由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 请教一个C++问题
相关主题
C++ Question[请教]有无大牛能说下自己知道的g家不同组的情况?
Please help on Effective C++ Item 11请问这个C++问题对吗?
copy constructor 的问题C++ Q47: protected constructor (C39)
也来一道c++的题贡献点g家电面题
C++ Q61: 如何对const data member做assignment?再问个C++题目
C++: Q75 copy constructor 问什么用 const reference?C++ Q54: enum (C12)
c++ class default functions?Qualcomm Phone Interbview面筋,赚RP
Can we define pure virtual function?贴一道take home的面试题
相关话题的讨论汇总
话题: string话题: char话题: c++话题: function话题: explicit
进入JobHunting版参与讨论
1 (共1页)
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?

1 (共1页)
进入JobHunting版参与讨论
相关主题
贴一道take home的面试题C++ Q61: 如何对const data member做assignment?
今早Bloomberg电话面试经过,奉献给大家。。。C++: Q75 copy constructor 问什么用 const reference?
新Qualcomm面经c++ class default functions?
再帖一遍Amazon Onsite的题Can we define pure virtual function?
C++ Question[请教]有无大牛能说下自己知道的g家不同组的情况?
Please help on Effective C++ Item 11请问这个C++问题对吗?
copy constructor 的问题C++ Q47: protected constructor (C39)
也来一道c++的题贡献点g家电面题
相关话题的讨论汇总
话题: string话题: char话题: c++话题: function话题: explicit