In my understanding, this piece of code actually does nothing except that, the default constructor of foo will be called, a will be initialize as 0, and b will be initialized as nullptr, and a default destructor will eventually be called before doSth() goes out of scope. Moreover, since doSth () is called, a function pointer will be pushed into a stack, and pop up after doSth() goes out of scope. That is it. However, the interviewer was not satisfied with my answer, so I am here to ask if anything missed besides what I mentioned above.
【在 i****n 的大作中提到】 : In my understanding, this piece of code actually does nothing except that, : the default constructor of foo will be called, a will be initialize as 0, : and b will be initialized as nullptr, and a default destructor will : eventually be called before doSth() goes out of scope. Moreover, since doSth : () is called, a function pointer will be pushed into a stack, and pop up : after doSth() goes out of scope. That is it. However, the interviewer was : not satisfied with my answer, so I am here to ask if anything missed besides : what I mentioned above.
a and b are not initialized, but "a" has been allocated some memory and the value in a's memory can be anything, not necessary to be zero. b also point to some memory (not necessary to be nullptr) which can be anything.
doSth besides
【在 i****n 的大作中提到】 : In my understanding, this piece of code actually does nothing except that, : the default constructor of foo will be called, a will be initialize as 0, : and b will be initialized as nullptr, and a default destructor will : eventually be called before doSth() goes out of scope. Moreover, since doSth : () is called, a function pointer will be pushed into a stack, and pop up : after doSth() goes out of scope. That is it. However, the interviewer was : not satisfied with my answer, so I am here to ask if anything missed besides : what I mentioned above.
x*s
17 楼
找个同学的妈接,一个月200够了
【在 s***t 的大作中提到】 : 你也是码工的,怎么还在乎这几百?
c*e
18 楼
a is not initialized to 0, nor b is initialized to NULL. Try this class foo { public: // foo() {cout << a << " " << b << endl;}; int a; int *b; }; void doSth() { foo f; cout << f.a << " " << f.b << endl; } int main() { doSth(); // int a; // cout << a << endl; // int *b; // cout << b << endl; return 0; }
c*t
19 楼
你给你父母每月多少? 我干嘛不能在乎,我每月一分钱都攒不下来,还想给孩子存点 学费呢
【在 s***t 的大作中提到】 : 你也是码工的,怎么还在乎这几百?
c*e
20 楼
I agree. One question, when we declare a variable without initialization, for example "int a;" will a always be initialized as 0?
the point
【在 m*****n 的大作中提到】 : a and b are not initialized, but "a" has been allocated some memory and the : value in a's memory can be anything, not necessary to be zero. b also point : to some memory (not necessary to be nullptr) which can be anything. : : doSth : besides
【在 m*****n 的大作中提到】 : a and b are not initialized, but "a" has been allocated some memory and the : value in a's memory can be anything, not necessary to be zero. b also point : to some memory (not necessary to be nullptr) which can be anything. : : doSth : besides