avatar
b*n
1
【 以下文字转载自 Quant 讨论区 】
发信人: nadw (singer), 信区: Quant
标 题: C++ questions
发信站: BBS 未名空间站 (Sat Jul 14 16:51:54 2007)
I was asked these two questions recently:
1. what are the drawbacks of pass-by-value? (i said: copy may use alot
memory and slow, can not change the value, slicing problem. But interviewer
said there are more serious problems?)
2. how do you limit the number of objects of a class?
avatar
h*o
2

interviewer
polymorphism
use static member to control

【在 b********n 的大作中提到】
: 【 以下文字转载自 Quant 讨论区 】
: 发信人: nadw (singer), 信区: Quant
: 标 题: C++ questions
: 发信站: BBS 未名空间站 (Sat Jul 14 16:51:54 2007)
: I was asked these two questions recently:
: 1. what are the drawbacks of pass-by-value? (i said: copy may use alot
: memory and slow, can not change the value, slicing problem. But interviewer
: said there are more serious problems?)
: 2. how do you limit the number of objects of a class?

avatar
b*n
3
关键是第一题,第二题没劲。

【在 h**o 的大作中提到】
:
: interviewer
: polymorphism
: use static member to control

avatar
t*t
4
我好象没觉得有什么更加严重的问题啊。

【在 b********n 的大作中提到】
: 关键是第一题,第二题没劲。
avatar
t*t
5
pass-by-value也可以poly的

【在 h**o 的大作中提到】
:
: interviewer
: polymorphism
: use static member to control

avatar
b*n
6
我也想不通啊。

【在 t****t 的大作中提到】
: 我好象没觉得有什么更加严重的问题啊。
avatar
c*g
7
Not only copy constructors are called, but also the matched destructors
will be called when function exits. If the object contains the other objects
,such as string or other large user-defined object,the situation may be more
serious.
pass-by-value won't work if the destination type is an abstract base class a
nd can result in erroneous behavior at runtime if the parameter's class has
derived classes.

interviewer

【在 b********n 的大作中提到】
: 【 以下文字转载自 Quant 讨论区 】
: 发信人: nadw (singer), 信区: Quant
: 标 题: C++ questions
: 发信站: BBS 未名空间站 (Sat Jul 14 16:51:54 2007)
: I was asked these two questions recently:
: 1. what are the drawbacks of pass-by-value? (i said: copy may use alot
: memory and slow, can not change the value, slicing problem. But interviewer
: said there are more serious problems?)
: 2. how do you limit the number of objects of a class?

avatar
g*g
8
第一个我不会,第二个跟singleton相似。搞个factory
维护一个counter就行。

interviewer

【在 b********n 的大作中提到】
: 【 以下文字转载自 Quant 讨论区 】
: 发信人: nadw (singer), 信区: Quant
: 标 题: C++ questions
: 发信站: BBS 未名空间站 (Sat Jul 14 16:51:54 2007)
: I was asked these two questions recently:
: 1. what are the drawbacks of pass-by-value? (i said: copy may use alot
: memory and slow, can not change the value, slicing problem. But interviewer
: said there are more serious problems?)
: 2. how do you limit the number of objects of a class?

avatar
h*o
9
我的意思是说这样会丢失polymorphism啊~
#include
using namespace std;
class A{
public:
virtual void f() { cout << "As class A" << endl; }
};
class B: public A{
public:
virtual void f() { cout << "As class B" << endl; }
};
void f( A a )
{ a.f(); }
int main()
{
B b;
f(b);
return 0;
}

【在 t****t 的大作中提到】
: pass-by-value也可以poly的
avatar
c*g
10
I think your example is about slicing

【在 h**o 的大作中提到】
: 我的意思是说这样会丢失polymorphism啊~
: #include
: using namespace std;
: class A{
: public:
: virtual void f() { cout << "As class A" << endl; }
: };
: class B: public A{
: public:
: virtual void f() { cout << "As class B" << endl; }

avatar
h*o
11
嗯,翻了一下书,这个的确叫slicing
但virtual functions的确是polymorphism的一方面啊

【在 c***g 的大作中提到】
: I think your example is about slicing
avatar
c*g
12
so what?

【在 h**o 的大作中提到】
: 嗯,翻了一下书,这个的确叫slicing
: 但virtual functions的确是polymorphism的一方面啊

avatar
h*o
13
原来楼主提到slicing了,sorry....

【在 c***g 的大作中提到】
: so what?
avatar
s*d
14
T*的pass-by-value总可以了吧。
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。