avatar
C++ interview questions help# Programming - 葵花宝典
I*k
1
Got some C++ interview questions, but not sure the answer, can anyone here
help??
1. What's wrong with the following lines of code? Does it complie? If yes,
any problems?
class A { ... };
class B : public A { ... } // definitions of classess, these are OK
Foo() {
A & a = B(); // ??
A * p = &B(); // ??
.......
}
2. If I use malloc to get a piece of memory :
char * p = (char*)malloc (1000);
now I found that I need more than 1000 bytes, say, 1020 bytes, how to get
memory such that the newly obtaine
avatar
c*a
2
bloomberg interview?

【在 I****k 的大作中提到】
: Got some C++ interview questions, but not sure the answer, can anyone here
: help??
: 1. What's wrong with the following lines of code? Does it complie? If yes,
: any problems?
: class A { ... };
: class B : public A { ... } // definitions of classess, these are OK
: Foo() {
: A & a = B(); // ??
: A * p = &B(); // ??
: .......

avatar
o*r
3

I tried with following code. It works without problem.
class A {
public:
int i;
};
class B : public A { }; // definitions of classes, these are OK
void Foo() {
A & a = B(); // ??
a.i = 1;
cout << a.i<
A * p = &B(); // ??
p->i = 2;
cout << p->i<}
reallocate a memory block with size 1020?
use new instead?
这样放狗查一下就知道了,估计没多少人用过
好象在哪见过用函数指针转换的

【在 I****k 的大作中提到】
: Got some C++ interview questions, but not sure the answer, can anyone here
: help??
: 1. What's wrong with the following lines of code? Does it complie? If yes,
: any problems?
: class A { ... };
: class B : public A { ... } // definitions of classess, these are OK
: Foo() {
: A & a = B(); // ??
: A * p = &B(); // ??
: .......

avatar
a*o
4
1. object of B is not declared?

【在 o******r 的大作中提到】
:
: I tried with following code. It works without problem.
: class A {
: public:
: int i;
: };
: class B : public A { }; // definitions of classes, these are OK
: void Foo() {
: A & a = B(); // ??
: a.i = 1;

avatar
t*t
5
零分

【在 o******r 的大作中提到】
:
: I tried with following code. It works without problem.
: class A {
: public:
: int i;
: };
: class B : public A { }; // definitions of classes, these are OK
: void Foo() {
: A & a = B(); // ??
: a.i = 1;

avatar
o*r
6
我本来就不知道,
解释一下?

【在 t****t 的大作中提到】
: 零分
avatar
t*t
7
1. search yfh的post,里面有一样的问题(虽然不是他问的)
2. realloc
3. placement new没错
4. 查查书就有了.void * operator new(std::size_t size, void* ptr) throw();
注意你不能overload这个.这个函数其实什么也不做.
5. 在多重继承的子类里,所有同类的virtual base只有一个copy,非virtual的,出现几次
就有几个copy.virtual base用来做interface很不错.
6. 如果不用extern "C",那link时就找不到对应的函数,因为c++版的函数名被decorate
过了

【在 I****k 的大作中提到】
: Got some C++ interview questions, but not sure the answer, can anyone here
: help??
: 1. What's wrong with the following lines of code? Does it complie? If yes,
: any problems?
: class A { ... };
: class B : public A { ... } // definitions of classess, these are OK
: Foo() {
: A & a = B(); // ??
: A * p = &B(); // ??
: .......

avatar
a*h
8
3,
A * pa = reinterpret_cast( p );
*pa = A( );

【在 I****k 的大作中提到】

: Got some C++ interview questions, but not sure the answer, can anyone here
: help??
: 1. What's wrong with the following lines of code? Does it complie? If yes,
: any problems?
: class A { ... };
: class B : public A { ... } // definitions of classess, these are OK
: Foo() {
: A & a = B(); // ??
: A * p = &B(); // ??
: .......

avatar
t*t
9
你只做了一题,不好说你零分,但是做的这题绝对没分
你这样用reinterpret_cast,就假设p指向的地方原来就有一个A对象.下面再*pa=A(),是
把一个新的A copy到*pa.但是这个不是copy constructor,而是operator=.所以A的实现
就会假设*pa里存在一个A对象,并且先做清理工作,再把A()copy进去.这清理工作一做,
可不就core dump了吗.

【在 a*******h 的大作中提到】
: 3,
: A * pa = reinterpret_cast( p );
: *pa = A( );

avatar
t*t
10
Haha, the same set of questions I had with JP Morgan last year. Actually,
this post is exactly the same as one I wrote in JobHunting on Nov 17, 2006.
LZ got this from some recruiters?
avatar
t*t
11
OK, my answers:
1) A & a = B(); // does not compile, need to use const on the left hand side
A * p = &B(); // does compile, problem is: 'B()' is a temporary, so we
cannot use p after this line of code as the memory is released
2) realloc();
3) placement new
4) try Effective C++ (or More effective C++?), forgot which item but there
are several items about this topic
5) try any C++ book
6) name mangling issue
Bottomline is: if you really read through the two books Effective C++ and
More effective
avatar
o*r
12

side
同学, 我都run了这段code了,赋值也做了,没发现毛病
等我把effective c++找出来再翻翻

【在 t******t 的大作中提到】
: OK, my answers:
: 1) A & a = B(); // does not compile, need to use const on the left hand side
: A * p = &B(); // does compile, problem is: 'B()' is a temporary, so we
: cannot use p after this line of code as the memory is released
: 2) realloc();
: 3) placement new
: 4) try Effective C++ (or More effective C++?), forgot which item but there
: are several items about this topic
: 5) try any C++ book
: 6) name mangling issue

avatar
t*t
13
跟你说零分了你怎么还不信呢,零分的意思就是你的答案没一个对的

【在 o******r 的大作中提到】
:
: side
: 同学, 我都run了这段code了,赋值也做了,没发现毛病
: 等我把effective c++找出来再翻翻

avatar
o*r
14
我都承认我孤陋寡闻了不行嘛:)
看了你们前面关于第一题的讨论了,
en
景仰之情有如那啥啥啥的,
不过我确实是在VC++ 2005下compile, run了,
一点问题没有,嘿嘿
要不,我说一道面试题?
class A{
public:
int i;
};
A *pA = NULL;
void foo(A& ob)
{
ob.i = 1;
}
请问
foo(*pA)会在哪出错,
我瞎猜了一个,
后来用VC/gcc verify了一下,

【在 t****t 的大作中提到】
: 跟你说零分了你怎么还不信呢,零分的意思就是你的答案没一个对的
avatar
a*h
15
不大明白你的意思。
什么叫做清理工作?
默认的operator=是要首先做"清理工作"的吗? 难道不是只做memberwise copy就完事了?
为什么这样就会core dump?

【在 t****t 的大作中提到】
: 你只做了一题,不好说你零分,但是做的这题绝对没分
: 你这样用reinterpret_cast,就假设p指向的地方原来就有一个A对象.下面再*pa=A(),是
: 把一个新的A copy到*pa.但是这个不是copy constructor,而是operator=.所以A的实现
: 就会假设*pa里存在一个A对象,并且先做清理工作,再把A()copy进去.这清理工作一做,
: 可不就core dump了吗.

avatar
t*t
16
1.这题里问的不一定是默认的op=
2.op=不一定只是做memberwise copy
3.如果不是做memberwise copy,那就会对原来的结构有一定的假设
4.当这个假设不满足时,就有可能发生core dump
5.如果不明白的话,请自己写一个string class就明白了

事了?

【在 a*******h 的大作中提到】
: 不大明白你的意思。
: 什么叫做清理工作?
: 默认的operator=是要首先做"清理工作"的吗? 难道不是只做memberwise copy就完事了?
: 为什么这样就会core dump?

avatar
t*o
17
1 对的吧,,GCC是这样报的错。。。

【在 o******r 的大作中提到】
: 我都承认我孤陋寡闻了不行嘛:)
: 看了你们前面关于第一题的讨论了,
: en
: 景仰之情有如那啥啥啥的,
: 不过我确实是在VC++ 2005下compile, run了,
: 一点问题没有,嘿嘿
: 要不,我说一道面试题?
: class A{
: public:
: int i;

avatar
a*h
18
明白了,谢谢。

【在 t****t 的大作中提到】
: 1.这题里问的不一定是默认的op=
: 2.op=不一定只是做memberwise copy
: 3.如果不是做memberwise copy,那就会对原来的结构有一定的假设
: 4.当这个假设不满足时,就有可能发生core dump
: 5.如果不明白的话,请自己写一个string class就明白了
:
: 事了?

avatar
p*o
19

side
unclear to me why adding the "const" will solve the problem. my feeling is
that
that the newly generated B object will also disappear after this line, so
what
is the const reference a pointing to?

【在 t******t 的大作中提到】
: OK, my answers:
: 1) A & a = B(); // does not compile, need to use const on the left hand side
: A * p = &B(); // does compile, problem is: 'B()' is a temporary, so we
: cannot use p after this line of code as the memory is released
: 2) realloc();
: 3) placement new
: 4) try Effective C++ (or More effective C++?), forgot which item but there
: are several items about this topic
: 5) try any C++ book
: 6) name mangling issue

avatar
t*t
20
You should understand difference between
A &a = B();
and
A *p = &B();
In the first line, a new object is created out of the temporary object
resulted from B() to be associated with the reference `a'. Thus, it is OK
that the temporary is gone. Of course, here the problem is that temporary
objects are by default as `const' objects so we have to match it with `const
A &a' on the LHS.
In the second line of code, we are only playing with the address of the
temporary using a pionter p. No other objec
avatar
t*t
21
correction. in
const A& a=B();
there is NO "new" object created from the temporary object. instead, the
temporary object ITSELF is bind to the const reference. the lifetime of the
temporary object is extended to the lifetime of the const reference. [8.5.3,
clause 8; 12.2, clause 5]

const

【在 t******t 的大作中提到】
: You should understand difference between
: A &a = B();
: and
: A *p = &B();
: In the first line, a new object is created out of the temporary object
: resulted from B() to be associated with the reference `a'. Thus, it is OK
: that the temporary is gone. Of course, here the problem is that temporary
: objects are by default as `const' objects so we have to match it with `const
: A &a' on the LHS.
: In the second line of code, we are only playing with the address of the

avatar
z*q
22
请问什么地方可以查到 类名() 这样的用法的说明?
谢谢

the
3,

【在 t****t 的大作中提到】
: correction. in
: const A& a=B();
: there is NO "new" object created from the temporary object. instead, the
: temporary object ITSELF is bind to the const reference. the lifetime of the
: temporary object is extended to the lifetime of the const reference. [8.5.3,
: clause 8; 12.2, clause 5]
:
: const

avatar
a*s
23
A * p = &B();// ??
g++ warning: taking address of temporary
{ p->i = 2; cout << p->i<The temporary is not destroyed?
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。