1

I know that you can get a reference to a static method like this:

typedef void (*pointer)();
pointer p = &MyClass::MyMethod;

But is there a way to get a reference to the class itself?

EDIT: I'd like to store a class name to a variable so i could instantiate different kinds of objects based on the current value of the variable to a buffer or a list. I could then perform operations on the list of objects (using polymorphism).

JHollanti
  • 2,343
  • 8
  • 27
  • 39
  • Take a look at this recent question: http://stackoverflow.com/questions/2839470/class-lookup-structure-array-in-c which covers what I think you are asking about. –  May 15 '10 at 18:58
  • It might cover what i'm after. Too bad i don't understand a thing about it ;) But seriously, it's a bit off the point (as far as i can see), though you could do it that way as well. – JHollanti May 15 '10 at 20:04

5 Answers5

5

No, "the class itself" is not an object that exists at runtime in C++ -- it's a compile-time concept only. Therefore you cannot get a reference or pointer to "a class object" per se, only to instances, functions (including static ones), and so on -- things that do exist at runtime.

With RTTI you can get somewhat-similar effects, though -- the typeid keyword gives you a reference to a std::type_info (you need to #include <typeinfo> to enable this) which "stands for" the type in question at runtime. (You may also need special compiler switches to enable this functionality, e.g. /GR for Microsoft's Visual C++).

Whether that's good enough for you depends on exactly what it is that you're trying to accomplish. Care to clarify?

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • Coming from Java, this is the kind of stuff i get stuck with C++ ... It's hard to favor C++ when there's a bunch of stuff you just can't do with it. – JHollanti May 15 '10 at 20:06
  • 1
    @JHollanti, I think I know what you mean -- but my modest experience of Java came _well after_ I'd mastered C++, and I felt the same "in reverse": so many things I was used to (pointer arithmetic, multiple inheritance, powerful templates, ...) just being taken away from me!-) Each of C++ and Java (...and Python and Ruby and Haskell and...;-) just doesn't let you do things that are natural and important in sombunall of the _other_ languages -- the key mindset is, use each language for **its own** strengths, not as a weakened substitute for what would better be done in **another** language!-) – Alex Martelli May 15 '10 at 20:26
1

No, classes are not first-class object in current versions of C++ (unlike other languages like Java, Python, etc.).

Mr Fooz
  • 109,094
  • 6
  • 73
  • 101
1

What you want to accomplish can be done by taking a function pointer, if by no other means then at least like this:

class Base {
}

class A : public Base {
}

class B : public Base {
}

Base *ConstructA() { return new A(); }
Base *ConstructB() { return new B(); }

Base (* c)();

...
     c = ConstructB;
     list <Base *> mylist;
     mylist.push_back(c());
Antti Huima
  • 25,136
  • 3
  • 52
  • 71
0

Although pure C++ doesn't have notions of class at run-time, there are libraries that provide such functionality.

Qt has a meta-object system that provides run-time class reflection/introspection, dynamic properties, and async communication.

http://qt-project.org/doc/qt-4.8/qmetaobject.html#details

Also, CERN's ROOT provides similar capabilities. See TObject and TClass.

http://root.cern.ch/drupal/content/reflex

Both are cross-platform and fairly tool chain compatible. For larger and more complex projects especially, something like these can remove many of the drawbacks of using C++ compared to other languages.

0

What you need is a factory. You can pass it a class name and it will return a pointer to a (new) instance of that class. But this isn't magic; you'll have to write the methods yourself.

When you talk about storing objects of different types in a list, I assume you mean you're going to store pointers to the objects, not the objects themselves (otherwise you're going to have real trouble). And if you're going to do that without violence, and also use polymorphism, these classes had better be derived from a common base.

If and when they are, and the constructors you want to use have similar signatures, you can use templates to save yourself some work:

class myFactory
{
    template <class T>
    Base * makeThing(int n, string s) // whatever arguments you like
    {
            T * ret = new T(n, s);
            return(dynamic_cast<Base*>(ret));
    }
};
Beta
  • 96,650
  • 16
  • 149
  • 150
  • Yep, you nailed it, right down to common base and pointers instead of objects. Thanks for this, it helps to hear how exactly these kinds of things should be done with C++. btw, just to clarify, is there a way to do polymorphism without a common base class (or an interface). 'Cause that's what i understood from your reply? – JHollanti May 15 '10 at 20:11