English 中文(简体)
当定义嵌入类的方法时, 是否有办法避免重复包含类?
原标题:When defining methods for an embedded class, is there a way to avoid repeating the containing class?

在 C++ 中, 我可以在另一个类别中定义一个类别, 即声明成员函数 。 稍后, 当我定义这些声明的定义时, 是否有一种方法可以避免重复包含的类别 。 例如, 我的页眉可能看起来是这样 :

class Outer {
  class Inner {
    void one();
    void two();
    void three();
  };
};

后来,我的定义可能看起来是这样的:

void Outer::Inner::one() { ... }
void Outer::Inner::two() { ... }
void Outer::Inner::three() { ... }

是否有某种方法可以省略外人,而不在声明点下定义,或许通过使用命名空间来形成:

void Inner::one() { ... }
void Inner::two() { ... }
void Inner::three() { ... }
最佳回答

我还没有尝试过这个(这里没有编译者), 但我会冒险猜测,如果你和内层一起工作, 比如像“辩论者”, 你可能会做:

using outerclass::innerclass;

并随后:

innerclass::foo() { /* def */ }

假设外类范围中不存在与你内部阶级同名的全球性。

而且,你肯定需要一个宏, 并且可能需要一个类型def(不确定,但值得一试) 。

我说所有这一切都让你的代码不易读 它是一个坏主意 虽然不管你如何围绕它工作。

问题回答

暂无回答




相关问题
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?

热门标签