1

So I'm making a application framework, every class basically inherits YObject, my Application manager handles any object that gets added to the class and stores them in: vector Application::ApplicationObjects`, I want to make a function like

ObjectType* object = Application->object_of<ObjectType>();

I'm not really sure how exactly to do this, I've heard one of the C++ casts can be used to determine if ObjectType derives from YObject, but I'm not sure!

EDIT:

Since its important to make understandable questions that can be researched or whatever..

What I was trying to make was a template function that would loop through all available objects and check if the current object could be cast to the template defined type.

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
Yamiez
  • 39
  • 1
  • 9
  • dynamic_cast, but why? – Captain Giraffe Oct 26 '15 at 23:16
  • You speak of [dynamic_cast](http://en.cppreference.com/w/cpp/language/dynamic_cast). What is your actual goal here? I ask because you may be lowering yourself into a snake pit. – user4581301 Oct 26 '15 at 23:18
  • @CaptainGiraffe because the Application object is a singleton, my entire framework is based off of my YObject class, this class has all basic information about the object, its mainly so that other threads can e.g do ObjectOf()->Allocate... or similiar! Thanks for the answer tho! =] – Yamiez Oct 26 '15 at 23:19
  • @user4581301 I'm aiming for a similiar object management like unity :P – Yamiez Oct 26 '15 at 23:19
  • 1
    This question is both unclear in its scope and very broad. I have to vote to close. – Captain Giraffe Oct 26 '15 at 23:22
  • @CaptainGiraffe dynamic_cast worked, question is tho if it actually works during runtime, by 'worked' I mean it compiled haha :P http://puu.sh/kYQX4/b6a892cda7.png – Yamiez Oct 26 '15 at 23:24
  • Please provide a [MCVE](http://stackoverflow.com/help/mcve) exemplifying what you want to achieve. As it stands your question is unclear and liable to being closed. – Walter Oct 26 '15 at 23:27
  • @Walter it has been answered – Yamiez Oct 26 '15 at 23:28
  • @Yamiez You misunderstand the purpose of this site. It's not just for getting quick answers to specific and poorly phrased questions, but for providing good answers to (searchable) questions that many people may have. Your question is so unclear that nobody else can be sure that their specific question is (essentially) identical to yours and hence has the same answer. – Walter Oct 26 '15 at 23:35
  • Have you considered good ol' [RAII](https://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization) instead? Maybe I'm just old, but spraying pointers around in C++ and counting on garbage collection to keep things unknotted kinda creeps me out. – user4581301 Oct 26 '15 at 23:39
  • @Walter I edited my question a bit, I'm not that good at formatting hopefully its good enough not to get the question deleted. – Yamiez Oct 27 '15 at 12:18
  • 1
    @Yamiez `ObjectType* object = Application->object_of();` is **not a function**. Don't provide links to code/images, but insert/embed them in the post. – Walter Oct 27 '15 at 12:37
  • @Walter added the actual function underneath – Yamiez Oct 27 '15 at 13:14
  • @Yamiez Lol, I just found this and thought I'd post a correct answer on it. Looks like you had my exact thought (which unquestionably means you're brilliant.) But, don't add your answer to the question. You should post it as an answer and accept it or just accept mine which happens to be identical to yours :) – Jonathan Mee Oct 27 '15 at 13:49
  • @JonathanMee Yep I accepted your answer, you explained it better then I could, better that way haha :P' – Yamiez Oct 27 '15 at 13:57
  • @Yamiez I've gone ahead and edited your question to not include the answer. If you need to change anything about my edit feel free to roll it back. – Jonathan Mee Oct 27 '15 at 14:01
  • @JonathanMee Alright, edit is fine and can stay :) – Yamiez Oct 27 '15 at 14:07
  • @Yamiez In case your interested, I tried to do this using `void*` instead of `YObject*` and got a nasty surprise. Apparently you cannot `dynamic_cast` from `void*` to an object pointer! I've asked a question about it here: http://stackoverflow.com/q/33370296/2642059 – Jonathan Mee Oct 27 '15 at 14:14

1 Answers1

1

I believe that you may have already found your answer here, but it may be beneficial to spell out how to do this for anyone else having the same issue.

You say that you want to:

Make was a template function that would loop through all available objects and check if the current looped object could be dereferenced to the template defined type

So let's start by assuming that you have pointers to all available objects in vector<YObject*> Application::m_foo, and you have pointers to a bunch of different objects in Application::m_foo.

You'll want to design your templated class like this:

template<typename T>
T* Application::object_of() {
    T* result = nullptr;

    for(auto& i : m_foo) {
        T* bar = dynamic_cast<T*>(i);

        if(bar != nullptr) {
            result = bar;
            break;
        }
    }
    return result;
}

You can see a hacky example of this here: http://ideone.com/J4oA1j I say "hacky" cause rather than having an Application class I've just used a global variable, and I never delete my news. But I believe that the idea is clear.

Community
  • 1
  • 1
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • Thanks for the answer, but yes I solved, this was a better explanation and wasn't a comment so I'll accept this answer! =] – Yamiez Oct 27 '15 at 13:55