English 中文(简体)
如何从阶级定义中排除私人的非虚拟方法?
原标题:How to omit private non-virtual methods from class definition?

我要说的是:

<>strong>a.hpp:

class B;

class A
{
private:
  std::unique_ptr<B> b_;
}

<>strong>a.cpp:

#include <something_complicated.hpp>

struct B
{
  something_complicated x;
}

something_complicated& A::get_complicated() { return b_->x; }

不幸的是,在这种情况下,由于“植被——复制”不是A的一种方法,因此将进行汇编。

因此,我们可以尝试:

<>strong>a.hpp:

class B;

class A
{
private:
  std::unique_ptr<B> b_;
  something_complicated& A::get_complicated();
}

但是,由于对一些“复制品”的定义,因此“hpp”未能汇编。

我们可以宣布一些东西——如果是一类,但可能是一种类型,这样就可以了。

The only way I can think of doing this without making b_ public nor including something_complicated.hpp in a.hpp is the following:

<>strong>a.cpp:

#include <something_complicated.hpp>

struct B
{
  something_complicated x;
}

#define get_complicated ((b_->x))

Surely I don t have to define a macro to get around this issue? Any alternatives?

最佳回答

恐怕有人误会属于哪类,什么也不。

并非所有针对班级内部的方法都应该是班级方法,毕竟,我们已经具备了<条码>朋友/代码>的功能。 我知道,许多人宣布“求助者<>/em>方法”为私人职能,但这样做带来了不必要的依赖(时间)和“<条码>的可见问题。

在处理《消除童工方案》时,我往往不使用私人职能。 相反,选择是:

  • Making Impl (B in your case) a true class, with its own validation logic and true API
  • Using static free functions (or functions declared in an anonymous namespace)

两者都是好的,使用似乎最为合适。 姓名:

  • methods: when dealing with validation issues
  • free functions: for computing that can be expressed in terms of the aforementionned methods

我有意寻找尽可能少的方法,因为这些方法是能够改变我的班级的唯一办法,而他们越少,我就越能相信会维持老兵。

就你而言,它要由你来决定哪一种办法最适合你。

In Action:

<<>strong>a.cpp

#include <something_complicated.hpp>

struct B
{
  something_complicated x;
}

static something_complicated& get_complicated(B& b) { return b_.x; }

// or anonymous namespace instead
namespace {
  something_complicated& get_complicated(B& b) { return b_.x; }
}

这与你的情况没有多大区别?

<>说明: 我倾向于静态功能,而倾向于匿名名称空间,因为在阅读时更明显。 名称空间介绍范围,在通过档案进行扫描时,范围并不容易发现。 你的里程可能有所不同,两者都有相同的功能(职能)。

问题回答

最容易的解决办法可能是在某一类别中总结一个复杂的类型,然后宣布,在<条码>a.hpp中,并在<条码>中加以界定。

a.hpp:

class B;
class complicated_ref;

class A
{
public:
  complicated_ref get_complicated();
private:
  std::unique_ptr<B> b_;
};

页: 1

// ... complicated definitions ...
typedef whatever something_complicated;

struct complicated_ref
{
    complicated_ref(something_complicated & thing) : ref(thing) {}
    something_complicated & ref;
};

如今,<代码>a.cpp和任何需要使用复杂类型的物品都必须包括标题,但只想使用<代码>的A则无需使用。

假设<代码>A的某些客户有很好的理由查阅复杂事项,但<代码>B却人人无法进入。 如果需要,仍然能够进入<代码>B,并通过这一途径进入复杂情况。

仅可避免在<代码>a.hpp中提及<编码>。

One solution is to replace the member function get_complicated with a free function, or a static method of another class.

<>:

class A_impl_base {
    A_impl_base() {}

    friend class A_impl; // all instances of A_impl_base are A_impl
}; // this stub class is the only wart the user sees

class A
{
private:
    std::unique_ptr< A_impl_base > b_; // this is not a wart, it s a pimpl

    friend class A_impl;
}

<><>>。

class A_impl : A_impl_base {
     static A_impl &get( A &obj ) { return * obj.b_; }
     static A_impl const &get( A const &obj ) { return * obj.b_; }
};

What s wrong with:

<>strong>a.hpp:

class B;

class A
{
private:
  std::unique_ptr<B> b_;
public:
  B& get_B();
}

如果您的客户想从B中找到一些复杂的东西,那么请他们<条码>#include <something_complicated.hpp>。

我们可以宣布一些东西——如果是一类,但可能是一种类型,这样就可以了。

这正是你必须做的事。 而我看不出如何用哪类规则来排除未来的声明。

如果你控制<条码>,便可复制。 您可以做标准图书馆做些什么:创设一个<条码>,以备复制_fwd.hpp,该编码有适当的前期申报,包括可能或不属于类型。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签