This topic has been archived. It cannot be replied.
-
工作学习 / 学科技术讨论 / 问两个C++问题,都是面试时遇到的。1. How to ensure the memory allocated is free? (In Java, there is garbage collector, but in C++, how to ensure memory are free?)
2. In a class constructor, other virtual member functions can be called? (e.g, class A has a virtual function called init, in A's constructor, init can be called? why?)
-oldreaper(wendy);
2010-3-30
{301}
(#5980902@0)
-
What type of job are you looking for? Don't tell us it's heavily C++ related.
-wukongjj(不经历风雨怎么见彩虹);
2010-3-30
(#5980943@0)
-
这还不是真正的面试,只是个电话screen,其他问题我都答的不错,就这两个问题,我一时有些蒙。之前参加了这个公司的一个变态C++考试,挺难但我也考过了。结果这两个问题答的不好,人家觉得我不够strong.
-oldreaper(wendy);
2010-3-31
(#5981199@0)
-
第一个问题我说的挺多,但对方不满意,他提到了一个resource什么的,我没听清,但应该是我不知道的。第二个问题我说不要在constructor, destructor中调用virtual function, 但不知道怎样解释好。这个工作用C++和数据库,数据库开发是我目前在干的,回答的还可以,但这两个问题答得不好,失掉了机会。你如果知道答案就告诉我,我也正在学习。
-oldreaper(wendy);
2010-3-31
{135}
(#5981274@0)
-
1) Maybe he want's to know what do you know about the smart pointer.2) When you call virtual function in Contructor/Destructor, the virutal function defined in this class will be called not the one of the derived class, since the vtable is not fully set up.
-stevensun2000(小胖子);
2010-3-31
{189}
(#5981319@0)
-
Resource Acquisition Is Initialization?
-dreamspeaker(风语者);
2010-4-5
(#5991578@0)
-
First question, I guess they may expect smart pointer. The second question, I think your answer is correct, the reason is the virtual table may not be ready and the subclass constructor has not been called.
-kuailerensheng(happy);
2010-3-31
(#5981613@0)
-
1.Resource Pool. 2.你回答得不错。 P.S有人喜欢Boost/STL 。。。,有些人不喜欢。
-frankwoo(柳五随风);
2010-4-5
(#5992015@0)
-
NIHAO
-philiphou(philipgao);
2010-4-16
(#6013804@0)
-
For question 2 please correct me if I am wrong.Calling vitrual function depends on vtable at runtime. But vtable will NOT be constructed untill constructor returns. That's bad calling virtual function inside constructor means calling it (them) without vtable.
-fang_yi2000(Tom);
2010-4-12
{213}
(#6006010@0)
-
The vtable should has been created, but the the pointer to that table wont been created until the ctor done.
-frankwoo(柳五随风);
2010-4-13
(#6008543@0)
-
Both are wrong. The vtable pointer is always created before constructor, and vtable is fixed, filled by the compiler. The real problem is due to the fact that base class constructor is called before it entering its derived class constructor.In base class' constructor, if a virtual method is called, and passed to the derived, however, the derived class did not initialized yet (members are garbage). To avoid this behavior C++ standard requires that the virtual function is not passed down, which at the first glance does not make sense.
-cerboros_redux(Cerboros Reborn);
2010-4-13
{298}
(#6008761@0)
-
Thanks for the illustration, but I did not see any difference from what yout said. the derived class is not fully initialized yet, indicates not just the members are garbage, but also...
-frankwoo(柳五随风);
2010-4-14
(#6011313@0)
-
and actually, regarding to most compilers, "The vtable pointer is always created before constructor", this is wrong.
-frankwoo(柳五随风);
2010-4-15
(#6011333@0)
-
Cerboros Reborn is right.
-fang_yi2000(Tom);
2010-4-15
{1517}
(#6012350@0)
-
If "Both the vtable and vptr are created in compile time and already within the binary image file. " holds water, then there is no problem calling any virtual function from the ctor. but again, is that real true? I do not think so.
-frankwoo(柳五随风);
2010-4-15
(#6013039@0)
-
Because vtable is not the problem here.
-cerboros_redux(Cerboros Reborn);
2010-4-15
{920}
(#6013060@0)
-
To illustrated the issue, considering this code:class Base
{
public:
Base() : { init(); }
virtual void init() { printf("base::init."); }
};
class Derived
{
public:
Derived() : a(0) { }
virtual void init() { printf("derived::init"); }
int a;
};
Derived obj;
it should print:
base::init.
-cerboros_redux(Cerboros Reborn);
2010-4-15
{276}
(#6013063@0)
-
Theoretically the output should be "Base::init", but it is NOT necessary ture in practice...I've tested in VC++, the output is "derived::init" (you can veriry it yourself later).
The hidden issue here, as I stated in my previous reply, is relative to the "this" pointer?
Can you please give me some hint on this?
Thanks in advance!
-fang_yi2000(Tom);
2010-4-16
{249}
(#6014044@0)
-
The theory is correct, and it is also true in practice. Otherwise I will go mad...
-liquid(豆泡松果 之 松果豆泡);
2010-4-16
(#6014282@0)
-
He is not right, that is what I mean, coz in thie regard, no C++ standard implementation exists.
-frankwoo(柳五随风);
2010-4-15
(#6013434@0)
-
but you are definitly not right on vtable.:)
-frankwoo(柳五随风);
2010-4-15
(#6013438@0)
-
"vtable pointer is always created before construcotr" is misleading. It looks like (not actually as)compare apple with orange :).
-fang_yi2000(Tom);
2010-4-15
(#6012488@0)
-
you two do not understand vtable. An object holds hidden vtable pointer, not vtable.
-cerboros_redux(Cerboros Reborn);
2010-4-15
{479}
(#6013086@0)
-
your example has nothing to do with the sequence inside the ctor, since sizeof should be calculated and replace during compilation stage, but it does not show the calling sequence inside ctor.
-frankwoo(柳五随风);
2010-4-15
(#6013328@0)
-
Did some search. Before Derived enters constructor, the vtable pointer is initialized to point to Base's vtable. After Base's constructor is finished, it is set back to its own vtable.Not sure about this is required by the standard, but this implement makes sure that the derived's virtual function is not called inside base's constructor.
Again, it is valid to call virtual function from constructor, just the call is treated as non-virtual.
-cerboros_redux(Cerboros Reborn);
2010-4-15
{263}
(#6013590@0)
-
by the way, the following link illustrates what I mentioned "the vptr has not beedn created when calling the base ctor", and I believe most compiler, ie. g++ follows .....
-frankwoo(柳五随风);
2010-4-15
{4}
(#6013424@0)
-
1)在编译构造函数时,编译器会自动加入指令将vptr指向当前类的vtable,并且这发生在由构造函数产生的任何指令之前;
2)如果在构造函数里调用虚函数,编译器会按照调用普通函数来处理,在这里并没有使用vptr;
-liquid(豆泡松果 之 松果豆泡);
2010-4-16
{4542}
(#6014258@0)
-
谢谢!证实了我的猜测。构造函数里调用虚函数是用“this”而不是用“vptr”。可否推广到任何成员函数里调用虚函数都用“this”而不是用“vptr” ?
-fang_yi2000(Tom);
2010-4-16
(#6014767@0)
-
Your guess is wrong. vptr has been populated before ctors, and vptr is used in all functions except ctors and dtors.
-liquid(豆泡松果 之 松果豆泡);
2010-4-16
(#6014811@0)
-
C+++++++++++++++++++++啊 我就是去学JAVA 也不去学C佳佳,这个程序语言设计的太难了
-ysysning(樱桃果核);
2010-4-16
(#6014854@0)
-
Think Java is easy? Try dig into countless web frameworks, database wrappers and enterprise servers. At least C++ doesn't have so much frameworks and you can fly freely (and/or uncontrolled)...
-liquid(豆泡松果 之 松果豆泡);
2010-4-16
(#6014884@0)