I'm not talking about a pointer to an instance, I want a pointer to a class itself.
我说的不是指向实例的指针,而是指向类本身的指针。
9 个解决方案
#1
17
In C++, classes are not "first class objects". The closest you can get is a pointer to its type_info
instance.
在c++中,类不是“第一类对象”。最接近的是指向它的type_info实例的指针。
#2
5
No. A pointer is the address of something in the memory of the computer at run-time. A class is just a set of instructions to the compiler.
不。指针是运行时计算机内存中某物的地址。类只是编译器的一组指令。
#3
4
As everyone else have already said, it's not possible to have a pointer to a class.
正如其他人已经说过的,不可能有指向类的指针。
But if the point is to create a new instance from some class chosen at runtime, you might want to check out the Factory Method (or Abstract Factory) design patterns.
但是,如果要点是从运行时选择的某个类创建一个新实例,您可能想要检查工厂方法(或抽象工厂)设计模式。
#4
1
Yes and No. This depends on your context of what you are trying to achieve. If you simply want a pointer to a type then no there is not a way. A type does not live in memory in the sense of a pointer.
是的,没有。这取决于你想要达到的目标。如果您只是想要一个指向某个类型的指针,那么没有办法。在指针的意义上,类型并不存在于内存中。
There reason I said yes though is some people would consider the virtual table a pointer to a type. It is possible to get this pointer since the virtual table does exist in memory and can be used to invoke virtual methods with a bit of trickery.
我说是的原因是有些人认为虚拟表是指向类型的指针。因为虚拟表确实存在于内存中,所以可以使用这个指针来调用虚拟方法和一些欺骗。
#5
1
Unlike true Object-Based languages, a class is not an object in C++, more is the pity. The closest you can come to "pointer to class" is RTTI:
与真正的基于对象的语言不同,类在c++中不是对象,更可惜的是。最接近“指针到类”的是RTTI:
const std::type_info &info = typeid(object expression);
std::type_info &info = typeid(对象表达式);
type_info has name() member finction, and they can be compared to each other.
type_info具有name()成员构造,它们可以相互比较。
#6
1
A "Class" does not exist. The only thing you can point to is the data.
“类”不存在。你唯一能指向的就是数据。
The rest of a "Class" is actually a dispatch table. For each method in the class, the dispatch table has a pointer. That way, the class points to the correct method of your class regardless of what type it's currently cast to. This would be useless to access.
“类”的其余部分实际上是一个分派表。对于类中的每个方法,分派表都有一个指针。这样,类就会指向类的正确方法,而不管它当前的类型是什么类型。这对访问是无用的。
Methods in your class (the things pointed to by the dispatch table) are actually just "Functions" that are passed in your class data pointer. The definition of a method is pretty much that it's a function that takes the classes data as a parameter. In most C-style languages, that data pointer is hidden but referred to as "this".
类中的方法(调度表指出的东西)实际上只是类数据指针中传递的“函数”。方法的定义非常重要,它是一个将类数据作为参数的函数。在大多数c语言中,数据指针被隐藏,但被称为“this”。
The methods for your class may be spread all over the codebase. Because of parent classes, you're not likely even find these methods adjacent to each other.
类的方法可以分布在所有的代码基上。由于父类,您甚至不太可能发现这些方法彼此相邻。
#7
1
You can't have a (run-time) pointer to a class, but C++ does has a similar compile-time concept: template parameters. Boost has a library dedicated to manipulating them and a traits library for getting information about classes.
您不能有一个指向类的(运行时)指针,但是c++有一个类似的编译时概念:模板参数。Boost有一个用于操作它们的库和用于获取类信息的特性库。
#8
0
Depending upon how you want to think about pointers, you can have a "pointer" to a class, if by pointer you mean some integral value. Boost allows you to register types and assign a unique integer for every type that you register. If the types you are registering are all classes then you can look up at run-time the code necessary to create an object of the type you want, as long as you have the value of the type you want. But in general, classes aren't first class objects in the language and the best you can hope for is to simulate the behavior you want to have.
根据您想要考虑指针的方式,您可以有一个指向类的“指针”,如果您所说的指针指的是某个积分值。Boost允许您注册类型并为注册的每个类型分配唯一的整数。如果您正在注册的类型是所有的类,那么您可以在运行时查找创建您想要的类型的对象所需的代码,只要您有您想要的类型的值。但是一般来说,类在语言中不是第一个类对象,您可以期望的最好结果是模拟您想要的行为。
#9
0
True, there is no support for reflection/introspection in built in to C++, but there are a number of libraries that will add many of eg java's Class functionality, and allow a programmer to get an object representing a class, create an instance, etc. google c++ reflection.
的确,在c++中不支持反射/内省,但是有许多库可以添加许多java的类功能,并允许程序员获得表示类的对象、创建实例等等谷歌c++反射。
#1
17
In C++, classes are not "first class objects". The closest you can get is a pointer to its type_info
instance.
在c++中,类不是“第一类对象”。最接近的是指向它的type_info实例的指针。
#2
5
No. A pointer is the address of something in the memory of the computer at run-time. A class is just a set of instructions to the compiler.
不。指针是运行时计算机内存中某物的地址。类只是编译器的一组指令。
#3
4
As everyone else have already said, it's not possible to have a pointer to a class.
正如其他人已经说过的,不可能有指向类的指针。
But if the point is to create a new instance from some class chosen at runtime, you might want to check out the Factory Method (or Abstract Factory) design patterns.
但是,如果要点是从运行时选择的某个类创建一个新实例,您可能想要检查工厂方法(或抽象工厂)设计模式。
#4
1
Yes and No. This depends on your context of what you are trying to achieve. If you simply want a pointer to a type then no there is not a way. A type does not live in memory in the sense of a pointer.
是的,没有。这取决于你想要达到的目标。如果您只是想要一个指向某个类型的指针,那么没有办法。在指针的意义上,类型并不存在于内存中。
There reason I said yes though is some people would consider the virtual table a pointer to a type. It is possible to get this pointer since the virtual table does exist in memory and can be used to invoke virtual methods with a bit of trickery.
我说是的原因是有些人认为虚拟表是指向类型的指针。因为虚拟表确实存在于内存中,所以可以使用这个指针来调用虚拟方法和一些欺骗。
#5
1
Unlike true Object-Based languages, a class is not an object in C++, more is the pity. The closest you can come to "pointer to class" is RTTI:
与真正的基于对象的语言不同,类在c++中不是对象,更可惜的是。最接近“指针到类”的是RTTI:
const std::type_info &info = typeid(object expression);
std::type_info &info = typeid(对象表达式);
type_info has name() member finction, and they can be compared to each other.
type_info具有name()成员构造,它们可以相互比较。
#6
1
A "Class" does not exist. The only thing you can point to is the data.
“类”不存在。你唯一能指向的就是数据。
The rest of a "Class" is actually a dispatch table. For each method in the class, the dispatch table has a pointer. That way, the class points to the correct method of your class regardless of what type it's currently cast to. This would be useless to access.
“类”的其余部分实际上是一个分派表。对于类中的每个方法,分派表都有一个指针。这样,类就会指向类的正确方法,而不管它当前的类型是什么类型。这对访问是无用的。
Methods in your class (the things pointed to by the dispatch table) are actually just "Functions" that are passed in your class data pointer. The definition of a method is pretty much that it's a function that takes the classes data as a parameter. In most C-style languages, that data pointer is hidden but referred to as "this".
类中的方法(调度表指出的东西)实际上只是类数据指针中传递的“函数”。方法的定义非常重要,它是一个将类数据作为参数的函数。在大多数c语言中,数据指针被隐藏,但被称为“this”。
The methods for your class may be spread all over the codebase. Because of parent classes, you're not likely even find these methods adjacent to each other.
类的方法可以分布在所有的代码基上。由于父类,您甚至不太可能发现这些方法彼此相邻。
#7
1
You can't have a (run-time) pointer to a class, but C++ does has a similar compile-time concept: template parameters. Boost has a library dedicated to manipulating them and a traits library for getting information about classes.
您不能有一个指向类的(运行时)指针,但是c++有一个类似的编译时概念:模板参数。Boost有一个用于操作它们的库和用于获取类信息的特性库。
#8
0
Depending upon how you want to think about pointers, you can have a "pointer" to a class, if by pointer you mean some integral value. Boost allows you to register types and assign a unique integer for every type that you register. If the types you are registering are all classes then you can look up at run-time the code necessary to create an object of the type you want, as long as you have the value of the type you want. But in general, classes aren't first class objects in the language and the best you can hope for is to simulate the behavior you want to have.
根据您想要考虑指针的方式,您可以有一个指向类的“指针”,如果您所说的指针指的是某个积分值。Boost允许您注册类型并为注册的每个类型分配唯一的整数。如果您正在注册的类型是所有的类,那么您可以在运行时查找创建您想要的类型的对象所需的代码,只要您有您想要的类型的值。但是一般来说,类在语言中不是第一个类对象,您可以期望的最好结果是模拟您想要的行为。
#9
0
True, there is no support for reflection/introspection in built in to C++, but there are a number of libraries that will add many of eg java's Class functionality, and allow a programmer to get an object representing a class, create an instance, etc. google c++ reflection.
的确,在c++中不支持反射/内省,但是有许多库可以添加许多java的类功能,并允许程序员获得表示类的对象、创建实例等等谷歌c++反射。