【发布时间】:2019-09-26 23:38:22
【问题描述】:
假设我的程序由 trusted 和 untrusted 两个组件组成。我只想在两个不同的命名空间中对 A 类的声明进行一次编码,但它们的实现可以根据命名空间而有所不同,只需对公共 API 编码一次。我不想将宏用于#ifdef UNTRSUTED .etc 之类的实现。
我不想使用抽象和继承来实现不同的行为。我只是好奇这是否可能。
在标题 A.h 我将有
// A.h
#pragma once
namespace app {
// I know I can't get what I want with naming the same namespace twice
namespace untrusted, trusted {
class A {
doDifferentFoo();
doCommonBar() // this one is common between two impls;
}
}
}
在实现中,我将拥有 A-common.cpp(只为两个命名空间实现公共接口一次)、A-untrusted.cpp(为不受信任的命名空间实现 doDifferentFoo)和 A-trusted.cpp(实现doDifferentFoo 用于受信任的命名空间)
【问题讨论】:
-
doDifferentFoo的两种实现有何不同?条件编译可以工作吗?或者你能提供一个共同的基类,两个类都继承自来处理共同的东西?
标签: c++ namespaces