相关疑难解决方法(0)

在C++中使用"super"

我的编码风格包括以下习语:

class Derived : public Base
{
   public :
      typedef Base super; // note that it could be hidden in
                          // protected/private section, instead

      // Etc.
} ;
Run Code Online (Sandbox Code Playgroud)

这使我能够使用"super"作为Base的别名,例如,在构造函数中:

Derived(int i, int j)
   : super(i), J(j)
{
}
Run Code Online (Sandbox Code Playgroud)

或者甚至在其重写版本中从基类调用方法时:

void Derived::foo()
{
   super::foo() ;

   // ... And then, do something else
}
Run Code Online (Sandbox Code Playgroud)

它甚至可以链接(我仍然可以找到它的用途):

class DerivedDerived : public Derived
{
   public :
      typedef Derived super; // note that it could be hidden in
                             // protected/private section, instead

      // Etc.
} ; …
Run Code Online (Sandbox Code Playgroud)

c++ coding-style

192
推荐指数
8
解决办法
15万
查看次数

在C++中删除类型名称的名称空间

在C++中,当我们使用typeid获取对象或类的类型名称时,它将显示一个装饰(受损)字符串.我用cxxabi它去解码它:

#include <cxxabi.h>
#include <typeinfo>

namespace MyNamespace {

class MyBaseClass
{
public:
    const std::string name()
    {
        int status;
        char *realname = abi::__cxa_demangle(typeid (*this).name(),0,0, &status);
        std::string n = realname;
        free(realname);
        return n;
    }
};

}

int main()
{
    MyNamespace::MyBaseClass h;
    std::cout << h.name() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

输出gcc是:

myNameSpace对象:: MyBaseClass

我需要MyNamespace::从上面的字符串中删除.我可以通过字符串操作删除它们 .

但是,有没有标准方法与cxxabi其他库或这样做或明确的解决方案?(至少可以在gcc和Visual C++之间移植)

c++ rtti

20
推荐指数
1
解决办法
4479
查看次数

标签 统计

c++ ×2

coding-style ×1

rtti ×1