C ++类定义拆分成两个头? [英] C++ class definition split into two headers?

查看:186
本文介绍了C ++类定义拆分成两个头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有可能在C ++中拆分类成员在两个头文件的定义?



例如:



a1.h

  class A {
public:
int var;
void foo1(int b);
}

a1.cpp

  #includea1.h

void A :: foo1(int b){
cout< b;
}

a2.h

  [extend] class A {
public:
void foo2(double c);
}



a2.cpp

  #includea2.h

void A :: foo2(double c){
cout< C;
}


解决方案

class ,但您可以使用 pimpl 模式:

  class A {
public:
void foo1(int b);
private:
AImpl * pimpl;

}

,然后使用 AImpl.h AImpl.cpp 可隐藏所有私人详细资料。


Is it possible in C++ to split the definition of class members in two headers? What would be the appropriate way to code it?

For instance:

a1.h

class A {
    public:
        int var;
        void foo1(int b);
}

a1.cpp

#include "a1.h"

void A::foo1(int b) {
    cout << b;
}

a2.h

[extend] class A {
    public:
        void foo2(double c);
}

a2.cpp

#include "a2.h"

void A::foo2(double c) {
    cout << c;
}

解决方案

You can't extend a class that way, but you can use the pimpl pattern:

class A {
public:
    void foo1(int b);
private:
    AImpl* pimpl;

}

and then have AImpl.h and AImpl.cpp that hides all the private details.

这篇关于C ++类定义拆分成两个头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆