V
vsgdp
Consider:
#include <iostream>
using namespace std;
class A
{
public:
A(){ p = this; }
static A* p;
};
A* A: = 0;
class B : public A
{
public:
B() : A() {}
};
int main()
{
B b;
if( b.p == &b )
cout << "Equal." << endl;
}
Output: Equal.
Why does b.p point to b? It seems it should point to the "A" part of b.
That is, Stroustrup says "In this respect, the base class acts exactly like
a member of the dervied class." (page 306 in special edition).
However, the behavior of the program can be explained if we think of A() as
a "method" of B. But is that technically correct? Accelerated C++ says
constructors of a base class are not also members of the derived class.
#include <iostream>
using namespace std;
class A
{
public:
A(){ p = this; }
static A* p;
};
A* A: = 0;
class B : public A
{
public:
B() : A() {}
};
int main()
{
B b;
if( b.p == &b )
cout << "Equal." << endl;
}
Output: Equal.
Why does b.p point to b? It seems it should point to the "A" part of b.
That is, Stroustrup says "In this respect, the base class acts exactly like
a member of the dervied class." (page 306 in special edition).
However, the behavior of the program can be explained if we think of A() as
a "method" of B. But is that technically correct? Accelerated C++ says
constructors of a base class are not also members of the derived class.