例如我这样定义一个 C++ 类:
class cat : public animal
{
public:
cat(const char* nameString) : m_name(nameString) {}
~cat(){}
const char* getName() {return m_name.c_str();}
private:
std::string m_name;
};
后来:
cat kitty("Garfield");
printf("%s", kitty.getName());
对于上面的示例代码,ctags可以告诉我继承、成员、方法、访问类型(公共(public)、私有(private))等。
但是我怎样才能知道 kitty 是 cat 的一个实例呢?
最佳答案
std::cout << "kitty is a: " << typeid(kitty).name() << '\n';
但是,你可以see ,可能不是您想要的。
Returns an implementation defined null-terminated character string containing the name of the type. No guarantees are given, in particular, the returned string can be identical for several types and change between invocations of the same program.
您可以 compare typeid
与其他已知类型,如 cat
。
并且您可以按照与对象名称相同的方向/样式制作它,并在 Animal
中包含一个虚函数:
virtual string whatAnimal()const{return "animal";}
在派生类中:
string whatAnimal()const override {return "cat";}
现在您可以完全控制每种类型的字符串,与您在代码中使用的名称约定无关。
关于c++ - 如何使用 ctags 获取 C++ 类实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14987122/