由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - A question about C++. Thanks.
相关主题
c++ vs Java virtual 实现(Y家)大家谈谈看???
C++继承问题问一个C++ set和unordered_set iterator的问题
问个c++的问题static initialization dependency c++ (转载)
C/C++里数组作函数的参数的话应该怎么写?发两个软件组的面试题
请教一个OOP的C++问题amazon的那道题目
C++相关的面经Interview questions, Bloomberg
C++ online Test 一题问个C++ virtual function的问题
问一个constructor的问题An example of strategy pattern
相关话题的讨论汇总
话题: dosth话题: foo话题: int话题: endl话题: cout
进入JobHunting版参与讨论
1 (共1页)
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
2
这都不会,还是回去看c++ primer吧
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
7
考察参数的生存周期?
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

1 (共1页)
进入JobHunting版参与讨论
相关主题
An example of strategy pattern请教一个OOP的C++问题
srand()的问题C++相关的面经
请教一个c的概念题C++ online Test 一题
有面过knight的吗问一个constructor的问题
c++ vs Java virtual 实现(Y家)大家谈谈看???
C++继承问题问一个C++ set和unordered_set iterator的问题
问个c++的问题static initialization dependency c++ (转载)
C/C++里数组作函数的参数的话应该怎么写?发两个软件组的面试题
相关话题的讨论汇总
话题: dosth话题: foo话题: int话题: endl话题: cout