roguemacro
roguemacro

Reputation: 319

C++ - Pointer to class type

Can I have a pointer to a class itself, not only a pointer to a variable?

// I'm looking for something like this
class MyClass {};

MyClass* ptr = &MyClass;

MyClass* myclass = new *ptr;

// But not this
class MyClass {};

MyClass myclass;

MyClass* ptr = myclass;

Upvotes: 3

Views: 11273

Answers (5)

Useless
Useless

Reputation: 67713

In some languages, types are first-class objects, meaning you can pass them around as arguments, introspect their class properties, etc.

In a language in which types are objects, you can refer or point to those objects.

In C++, types are used at compile time and then effectively cease to exist. They are never represented as first-class objects, and you can't really introspect them at runtime.

In a language in which types are not objects, you can't refer or point to those objects, because they don't exist.


For implementing a new language in C++, you're starting one level too low: before you can define a user-defined type in your new language, you need to know what type that type will be in your implementation code.

For example, in Python:

class A: pass
class B: pass

defines two (empty) class objects, called A and B. There are not two corresponding C struct types in the CPython implementation. There can't be, because the CPython interpreter was compiled (and is now a fixed sealed executable) before it ever saw our Python code.

So, we must instead define a single C++ type which describes a user-defined type in your new language. Each user-defined type will be an instance of this C++ type.

For example:

struct UserDefinedType: public Object
{
  std::string name;
  std::vector<std::shared_ptr<Object>> members;
  std::vector<std::shared_ptr<Function>> methods;
};

std::vector<std::shared_ptr<UserDefinedType>> types;

and in your new language, a statement like

class MyClass {};

would be implemented in C++ by something like:

case KEYWORD_CLASS:
  types.push_back(make_shared<UserDefinedType>(parser.getName(),
                                               parser.getMembers(),
                                               parser.getMethods()));
  break;

NB. we still haven't discussed taking a pointer to a class inside your new language. However, I made the UserDefinedType struct derive from Object, which means you could use them as Python-like first-class objects.

Upvotes: 8

JEagle
JEagle

Reputation: 46

You can have a pointer to compiler information about type - type_info , returned by operator typeid . But looks like it`s not what you need. You should read about patterns "Factory Method" and "Abstract Factory" .

"Factory Method" pattern looks close to your code example.

Upvotes: 0

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385098

Kind of.

std::type_info is an object that "describes" a type. You can get one with the typeid operator.

However, since C++ does not support reflection, there's very little that you can actually do with it. For example, you won't be able to "dereference" it to generate some pointer-to-member.

You certainly cannot form a normal pointer, to a type.

Upvotes: 4

eerorika
eerorika

Reputation: 238301

Can I have a pointer to a class itself

No. There are pointers to objects, functions and members. There are no pointers to classes, or types in general in C++.

Upvotes: 2

gsamaras
gsamaras

Reputation: 73366

Can I have a pointer to a class itself, not only a pointer to a variable?

No.

A class is basically a type, and it doesn't make sense to point to a type.

As @SomeProgrammerDude commented: you expect e.g. int* ptr = &int; to work? It's the same thing.

PS: Maybe you are trying to practice the paradigm of polymorphism, read about it online..

Upvotes: 2

Related Questions