b********n 发帖数: 609 | 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? |
h**o 发帖数: 347 | 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?
|
b********n 发帖数: 609 | 3 关键是第一题,第二题没劲。
【在 h**o 的大作中提到】 : : interviewer : polymorphism : use static member to control
|
t****t 发帖数: 6806 | 4 我好象没觉得有什么更加严重的问题啊。
【在 b********n 的大作中提到】 : 关键是第一题,第二题没劲。
|
t****t 发帖数: 6806 | 5 pass-by-value也可以poly的
【在 h**o 的大作中提到】 : : interviewer : polymorphism : use static member to control
|
b********n 发帖数: 609 | 6 我也想不通啊。
【在 t****t 的大作中提到】 : 我好象没觉得有什么更加严重的问题啊。
|
c***g 发帖数: 472 | 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?
|
g*****g 发帖数: 34805 | 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?
|
h**o 发帖数: 347 | 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的
|
c***g 发帖数: 472 | 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; }
|
h**o 发帖数: 347 | 11 嗯,翻了一下书,这个的确叫slicing
但virtual functions的确是polymorphism的一方面啊
【在 c***g 的大作中提到】 : I think your example is about slicing
|
c***g 发帖数: 472 | 12 so what?
【在 h**o 的大作中提到】 : 嗯,翻了一下书,这个的确叫slicing : 但virtual functions的确是polymorphism的一方面啊
|
h**o 发帖数: 347 | 13 原来楼主提到slicing了,sorry....
【在 c***g 的大作中提到】 : so what?
|
s*******d 发帖数: 59 | |