[AB.h]
#include < iostream.h >
class A
{
public :
A()
{
cout << " Class A is newed " ;
}
};
class B
{
public :
B()
{
cout << " Class B is newed " ;
}
A a;
};
#include < iostream.h >
class A
{
public :
A()
{
cout << " Class A is newed " ;
}
};
class B
{
public :
B()
{
cout << " Class B is newed " ;
}
A a;
};
[AB.cc]
#include " AB.h "
int main()
{
B * bp = new B();
return 0 ;
}
#include " AB.h "
int main()
{
B * bp = new B();
return 0 ;
}
///
编译,g++ AB.cc -o AB,会有warning提示<strstream.h>已经deprecated了。(这个warning提示此用法是C++标准化之前的用法,编译加参数-Wno-deprecated)运行,输出:
Class A is newed
Class B is newed
这说明在实例化B的时候,先保证了成员变量a已经实例化
/
AB.h里面
#include <iostream.h>
iostream.h是旧的头文件,C++标准化后,改成
#include
<
iostream
>
using namespace std;
using namespace std;
就不会有相关warning了。