由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++ interview questions help
相关主题
看道c++题: brianbench[合集] C++: Is the temporary object automatically const
有没有办法让一个类的变量只读,不是const?reinterpret cast的问题
问个C++ 编译器临时变量的问题 (转载)size不固定的struct怎么定义呀?
最新某公司onsite面试题 (转载)thread-safe singleton implementation
请教struct inside class的问题(C++)question about const reference
请教C++11的rvalue refc++小问题
请教C++ call-by-ref & call-by-val的问题请教一个关于std::function的问题
C++ question引用的几个基本问题,有点糊涂
相关话题的讨论汇总
话题: c++话题: temporary话题: const话题: object话题: ok
进入Programming版参与讨论
1 (共1页)
I****k
发帖数: 35
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
c*******a
发帖数: 18
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(); // ??
: .......

o******r
发帖数: 259
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(); // ??
: .......

a***o
发帖数: 969
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;

t****t
发帖数: 6806
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;

o******r
发帖数: 259
6
我本来就不知道,
解释一下?

【在 t****t 的大作中提到】
: 零分
t****t
发帖数: 6806
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(); // ??
: .......

a*******h
发帖数: 123
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(); // ??
: .......

t****t
发帖数: 6806
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( );

t******t
发帖数: 51
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?
相关主题
请教C++11的rvalue ref[合集] C++: Is the temporary object automatically const
请教C++ call-by-ref & call-by-val的问题reinterpret cast的问题
C++ questionsize不固定的struct怎么定义呀?
进入Programming版参与讨论
t******t
发帖数: 51
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
o******r
发帖数: 259
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

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

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

o******r
发帖数: 259
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 的大作中提到】
: 跟你说零分了你怎么还不信呢,零分的意思就是你的答案没一个对的
a*******h
发帖数: 123
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了吗.

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

事了?

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

t**o
发帖数: 64
17
1 对的吧,,GCC是这样报的错。。。

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

a*******h
发帖数: 123
18
明白了,谢谢。

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

p****o
发帖数: 1340
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

t******t
发帖数: 51
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
相关主题
thread-safe singleton implementation请教一个关于std::function的问题
question about const reference引用的几个基本问题,有点糊涂
c++小问题Is it safe?
进入Programming版参与讨论
t****t
发帖数: 6806
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

z**q
发帖数: 68
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

a***s
发帖数: 614
23
A * p = &B();// ??
g++ warning: taking address of temporary
{ p->i = 2; cout << p->i< The temporary is not destroyed?
1 (共1页)
进入Programming版参与讨论
相关主题
引用的几个基本问题,有点糊涂请教struct inside class的问题(C++)
Is it safe?请教C++11的rvalue ref
[合集] reinterpret_cast a 4 byte unsigned char to integer请教C++ call-by-ref & call-by-val的问题
pointer overflowC++ question
看道c++题: brianbench[合集] C++: Is the temporary object automatically const
有没有办法让一个类的变量只读,不是const?reinterpret cast的问题
问个C++ 编译器临时变量的问题 (转载)size不固定的struct怎么定义呀?
最新某公司onsite面试题 (转载)thread-safe singleton implementation
相关话题的讨论汇总
话题: c++话题: temporary话题: const话题: object话题: ok