views:

872

answers:

10

I'm not talking about a pointer to an instance, I want a pointer to a class itself.

+5  A: 

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.

Will Dean
A: 

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.

JaredPar
There is a small problem with that: a class can have multiple virtual tables when using multiple inheritance. Also, if a class inherits from another but does not change any virtual methods, I believe nothing prevents the compiler from merging both classes' virtual tables.
CesarB
True but you can still get a pointer to it. Personally I don't consider this a pointer to a type but plenty of people do so I thought I'd post it for completeness
JaredPar
+17  A: 

In C++, classes are not "first class objects". The closest you can get is a pointer to its type_info instance.

CesarB
+1  A: 

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:

const std::type_info &info = typeid(object expression);

type_info has name() member finction, and they can be compared to each other.

Arkadiy
A: 

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".

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.

Bill K
+3  A: 

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.

CAdaker
+1  A: 

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.

David Nehme
A: 

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.

A: 

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.

Chris Morley
A: 

I think the other obvious reply is what do you want to do with a pointer to a class?

MSN

MSN