下面程序的执行结果:
链接:https://www.nowcoder.com/questionTerminal/c85f9e15e6a4410a930581ae12b9a341
来源:牛客网
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
class A{
public:
long a;
};
class B : public A {
public:
long b;
};
void seta(A* data, int idx) {
data[idx].a = 2;
}
int main(int argc, char *argv[]) {
B data[4];
for
(int i=0; i<4; ++i){
data[i].a = 1;
data[i].b = 1;
seta(data, i);
}
for
(int i=0; i<4; ++i){
std::cout << data[i].a << data[i].b;
}
return
0;
}
|
11111111
12121212
11112222
21212121
22221111
答案选E,当调用seta(data,i)时,指针data加1而而data[4]的地址是按class A[0], classB[0],class A[1], classB[1]的地址存放,故答案为22221111。