我的编码风格包括以下习语:
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++中,当我们使用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++之间移植)