gpt4 book ai didi

c++ - 用最少的代码实现 pImpl

转载 作者:可可西里 更新时间:2023-11-01 16:13:20 25 4
gpt4 key购买 nike

可以使用什么样的技巧来最小化实现 pImpl 类的工作量?

标题:

class Foo {
struct Impl;
boost::scoped_ptr<Impl> self;
public:
Foo(int arg);
~Foo();
// Public member functions go here
};

实现:

struct Foo::Impl {
Impl(int arg): something(arg) {}
// All data members and private functions go here
};

Foo::Foo(int arg): self(new Impl(arg)) {}
Foo::~Foo() {}

// Foo's public functions go here (and they refer to data as self->something)

您将如何改进这一点,使用 Boost、可能的继承、CRTP 或其他技巧来尽可能避免使用样板代码?运行时性能不是问题。

最佳答案

pimpl 的实现来自 Loki可能是一个很好的答案。另见 DDJ Article对此。

关于c++ - 用最少的代码实现 pImpl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2353514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com