i****n 发帖数: 42 | 1 Here is my question:
class foo
{
int a;
int *b;
}
void doSth()
{
foo f;
}
void main()
{
doSth();
}
What will happen at run-time?
Thanks a lot. | H******7 发帖数: 1728 | | l****s 发帖数: 75 | 3 nothing happens.
a and b are not initialized.
try this:
#include
class foo
{
private:
int a;
int *b;
public:
void print()
{
std::cout << a << std::endl;
std::cout << &a << std::endl;
std::cout << b << std::endl;
//std::cout << *b << std::endl;
}
};
void doSth()
{
foo f;
f.print();
}
void main()
{
doSth();
std::cin.get();
}
【在 i****n 的大作中提到】 : Here is my question: : class foo : { : int a; : int *b; : } : void doSth() : { : foo f; : }
| i****n 发帖数: 42 | 4 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. | b*******d 发帖数: 353 | 5 a,b应该不会被initialize吧。。。我印象里只有global variable会自动initialize成
0。。。
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.
| r*****e 发帖数: 792 | 6 default constructor should initialize data members using
their corresponding default values.
【在 b*******d 的大作中提到】 : a,b应该不会被initialize吧。。。我印象里只有global variable会自动initialize成 : 0。。。 : : doSth : besides
| m*******g 发帖数: 410 | | m*****n 发帖数: 2152 | 8 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.
| c**********e 发帖数: 58 | 9 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**********e 发帖数: 58 | 10 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
| c**********e 发帖数: 58 | | w*****e 发帖数: 1050 | 12
the
point
this is right.
【在 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
|
|