b***k 发帖数: 2673 | 1 ☆─────────────────────────────────────☆
yww (petite) 于 (Wed Feb 20 12:03:09 2008) 提到:
(1)random walk with prob p>0.5 going +1 and 1-p going -1, what's the
probability it will ever hit 5
(2) pseudo code for read/write lock
(3) A is base class, B: public A; C: public B, all classes have a virtual
function f(), what will happen if you can f() in C's Ctor
(4) Singleton, Prototype, State design pattern
(5) what resource is shared between parent and child process after fork ?
(6) derivative of x^ | n**x 发帖数: 6 | 2 for (3), you actually call the f() in the base class A. Never call a virtual
function during construction or destruction because the call never goes
down to the drived class even if you are trying to create a derived class
object.Since base class part is always created before derived class part,
and during the creation of the base class part, all part of C++ will view it
as a base class object since all the derived class part has not been
initialized. So the virtual function to be called is alwa | r***w 发帖数: 35 | 3 I think the answer for (1) is 1 is p>q. Not familiar with distribution, but
from point of view of PDE, this is a bias random walk, the corresponding PDE
is covection-diffusion. The solution will approach to the drift term in
asymptotic sense. Then, phyiscally 1 is the solution. Mathematically, the
PDE is
u_t+(2p-1)u'=pu", where p>0.5. Then, upwind of conservation law is to right
always. So, hitting 5 always happen. | t*****n 发帖数: 167 | 4 then, why not B's f()? I think B is already created, right?
virtual
it
【在 n**x 的大作中提到】 : for (3), you actually call the f() in the base class A. Never call a virtual : function during construction or destruction because the call never goes : down to the drived class even if you are trying to create a derived class : object.Since base class part is always created before derived class part, : and during the creation of the base class part, all part of C++ will view it : as a base class object since all the derived class part has not been : initialized. So the virtual function to be called is alwa
| d*********1 发帖数: 25 | 5 #include
using namespace std;
class A {
public:
A() { cout<<"A constructor\n"; }
virtual void f() { cout<<"A"; }
};
class B: public A {
public:
B() { cout<<"B constructor\n"; }
virtual void f() { cout<<"B"; }
};
class C: public B {
public:
C() { cout<<"C constructor\n"; f(); }
virtual void f() { cout<<"C\n"; }
};
int main()
{
A *a= new C;
a->f(); //will print "C"
C |
|