j*****k 发帖数: 1198 | 1 class A{};
class B{};
class C: public A
{
C(){};
B b;
}
when instantiate C, C ctor first or B b first? | P********e 发帖数: 2610 | 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?
| p***o 发帖数: 1252 | 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
| P********e 发帖数: 2610 | 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?
| p***o 发帖数: 1252 | 5
Why not? C() {} is C(): b() {} .
【在 P********e 的大作中提到】 : A's ctor yes, : B's ctor no.
| P********e 发帖数: 2610 | 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() {} .
| p***o 发帖数: 1252 | 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
| P********e 发帖数: 2610 | 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
|
|