试试:
a.h
#pragma once
struct RandomData;
class A
{
public:
A();
~A();
bool Run();
private:
RandomData *m_pData;
};
a.cpp
#include <iostream>
#include "a.h"
struct RandomData
{
int n;
};
A::A()
:m_pData(NULL)
{
}
A::~A()
{
}
bool A::Run()
{
if(NULL == m_pData)
{
m_pData = new (std::nothrow) RandomData;
if(NULL == m_pData)
{
return false;
}
m_pData->n = 2;
}
}
#include <iostream>
#include "a.h"
using namespace std;
int main(int argc, char *argv[])
{
A a;
a.Run();
return 0;
}
Makefile
test:main.cpp a.cpp
g++ -o $@ $^
clean:
-rm test