简体   繁体   English

静态成员函数中包含类的C ++类型

[英]C++ type of enclosing class in static member function

I assume this is outright impossible, but what if. 我认为这是完全不可能的,但是如果可以的话。 Is it possible to somehow get type of enclosing class in a static member function, in any version of C++? 在任何版本的C ++中,是否都可能以某种方式获取静态成员函数中的封闭类的类型?

class Impossible {
public:
    static void Fun()
    {
        typedef Impossible EnclosingClass;

        // now do something with EnclosingClass ...
    }
}

Is there a way to get the type of the enclosing class ( Impossible in this case) without writing the name of the class in the function? 有没有一种方法可以获取封闭类的类型(在这种情况下是Impossible )而无需在函数中编写类的名称?

The reason why I'd like to do that is to avoid repeating the class name in the function. 我想这样做的原因是避免在函数中重复类名。 It could easily lead to a hard to find copy-paste bug, if something like this happened: 如果发生以下情况,很容易导致难以发现复制粘贴错误:

class SomeOther { // another class, with the same interface as Impossible
public:
    static void Fun()
    {
        typedef Impossible EnclosingClass;
        // whoops, copy-pasted, forgot to change "Impossible" to "SomeOther"

        // now do something with EnclosingClass ...
    }
}

Is there a good way to prevent this kind of thing happening? 有防止这种情况发生的好方法吗? I could imagine touching something that was declared private in the enclosing class, but that would be forcing me to write extra code (as my current design doesn't contain any inherent private members, all is public). 我可以想象碰到在封闭类中声明为私有的东西,但是那将迫使我编写额外的代码(因为我当前的设计不包含任何固有的私有成员,所以都是公共的)。

The problem is that C++ is lacking a self keyword. 问题在于C ++缺少self关键字。

I typically write: 我通常写:

struct Foo
{
   typedef Foo self;

   static void bar()
   {
      self* ptr = nullptr;
   }
};

I realise you still have to make sure the typedef is correct, but at least this way you can have it at the top of the type definition where you'll notice it. 我意识到您仍然必须确保typedef是正确的,但是至少以这种方式,您可以将其放在类型定义的顶部,在那里您会注意到它。

With hackery, though, you can make this entirely autonomous . 但是,有了黑客, 您就可以完全自主

C++ does not have any feature to get the name of the current class, namespace, etc. In C++11 you can get type of the variable, but you need the variable in the first place. C ++没有任何功能来获取当前类,名称空间等的名称。在C ++ 11中,您可以获取变量的类型,但首先需要该变量。 In this case you do not have anything to start with. 在这种情况下,您一无所有。

I like the idea of trying to introduce some general name that you can refer to in unrelated classes; 我喜欢尝试引入一些可以在不相关的类中引用的通用名称的想法; for maintainability. 为了可维护性。

My first approach would be a simple one: Provide a typedef in the enclosing class. 我的第一种方法将是一种简单的方法:在封闭的类中提供typedef Give the typedef the same name in every unrelated class. 在每个不相关的类中为typedef赋予相同的名称。

class Impossible {
public:
    typedef Impossible Enclosing;
    static void Fun()
    {   
        Enclosing* theObject = 0;
    }   
};

Doing this will also have the effect -- and I would call it a benefit -- of failing to compile in new unrelated classes where you haven't provided the typedef . 这样做也会产生效果-我称之为好处 -无法在您没有提供typedef新不相关类中进行编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM