avatar
问个先后的问题# Programming - 葵花宝典
j*k
1
class A{};
class B{};
class C: public A
{
C(){};
B b;
}
when instantiate C, C ctor first or B b first?
avatar
P*e
2
A -> B -> C

【在 j*****k 的大作中提到】
: class A{};
: class B{};
: class C: public A
: {
: C(){};
: B b;
: }
: when instantiate C, C ctor first or B b first?

avatar
p*o
3
Well, to be more accurate, aren't A's ctor and B's ctor called by C's ctor?

【在 P********e 的大作中提到】
: A -> B -> C
avatar
P*e
4
A's ctor yes,
B's ctor no.

【在 p***o 的大作中提到】
: Well, to be more accurate, aren't A's ctor and B's ctor called by C's ctor?
avatar
p*o
5

Why not? C() {} is C(): b() {} .

【在 P********e 的大作中提到】
: A's ctor yes,
: B's ctor no.

avatar
P*e
6
Sure, you can explicitely write the code this way, then the answer change as
well
Just treat B as build-in type

【在 p***o 的大作中提到】
:
: Why not? C() {} is C(): b() {} .

avatar
p*o
7
The answer should not change no matter how I write the code (well, there
is a difference, if you treat it as a difference, for build-in types and
PODs).
I mean if we are talking about the 'body' (inside {}) of those ctors, then
A->B->C is definitely correct. But I don't know if the interviewer really
want this anwser ...

as

【在 P********e 的大作中提到】
: Sure, you can explicitely write the code this way, then the answer change as
: well
: Just treat B as build-in type

avatar
P*e
8
To anwser it correctly is enough, i checked thinking in c++, that's about it

【在 p***o 的大作中提到】
: The answer should not change no matter how I write the code (well, there
: is a difference, if you treat it as a difference, for build-in types and
: PODs).
: I mean if we are talking about the 'body' (inside {}) of those ctors, then
: A->B->C is definitely correct. But I don't know if the interviewer really
: want this anwser ...
:
: as

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。