0

I'm a java developer and currently learning C++. To do so I thought I could just try to code some programs I did in Java in C++; And here's my question: If in Java I have following class-architecture:

abstract class Event {}
class Event1 extends Event {/*define some variables and methods here*/}
class Event2 extends Event {/*define some variables and methods here*/}
class MyEventListeners {
    public void event1Listener(Event1 event){/*do something*/}
    public void event2Listener(Event2 event){/*do something*/}
}
class someclass {
    void somemethod(){
        eventHandlerInstance.add(myEventListenersInstance.getClass().getMethod("event1Listener", null);
        eventHandlerInstance.add(myEventListenersInstance.getClass().getMethod("event2Listener", null);
        //OR!
        eventHandlerInstance.add(myEventListenersInstance);
    }
    void someOtherMethod(){
        eventHandlerInstance.fireEvent(new Event1()); // MyEventListeners#event1Listener gets called
    }
}
class EventHandler {
    HashMap<Event, List<Method>> listeners = new HashMap<Event, List<Method>>();
    public void add(Method m){
        if(m.getParameterTypes().length == 1 && m.getParameterTypes()[0].isAssignableFrom(Event.class){
            listeners.get((Event)m.getParameterTypes()[0]).add(m);
        }
    }
    public void add(Class c){
        for(Method m : c.getDeclaredMethods()){add(m);}
    }
    public void fireEvent(Event e){
        for(Event ev : listeners.keySet()){
            if(e instanceof ev.getClass()){
                for(Method m : listeners.get(ev)){m.invoke(e);}
            }
        }
    }

}

This code probably wont work since I just wrote it down in two minutes, but it should visualize what I'm trying to do. The problem is, I heard that C++ doesnt support reflection... Is it possible to port this Java code to C++, and if yes, how?

Kind regards

Fly
  • 810
  • 2
  • 9
  • 28
  • 3
    I recommend forgetting everything you know about Java when dealing with C++ and learn from the ground-up how to do things the C++ way. Trying to map Java methodology onto C++ methodology will only lead to poor C++ design imho. My previous experience in Java definitely hindered my progress in C++ for that reason. – Galik Sep 22 '14 at 10:53
  • @Galik Thanks for your input. This opens up a question for me, though: How exactly should I start learning C++ then? I started a few days ago by reading tutorials on learningcpp.com, but found myself scrolling through most of it pretty fast, since I already know many of the "basics" from Java. Is there something, like, a tutorial on learning C++ for Java developers? – Fly Sep 22 '14 at 17:07
  • 1
    I have been programming in C++ for many years now and would like to think I have a fair handle on the language. But since the introduction of C++11, even though I have kept abreast of the changes, I feel the need to go back to first principles and re-learn C++ again. So I appreciate what you mean when it comes to going over material that you already know. I have not been to learningcpp.com but I do know there is a plethora of bad tutorials and bad books and bad teachers out there teaching C++ badly. Usually because they teach it like C. – Galik Sep 22 '14 at 17:58
  • 1
    So I would recommend a **good book**. http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list Maybe Scott Meyers Effective C++ would be good coming from Java. Look out for Scott Meyers new book coming out soon (Effective C++11/14) And the best FAQ: http://www.parashift.com/c++-faq-lite/ – Galik Sep 22 '14 at 17:59
  • 1
    Also, if you use IRC, I learned a lot of stuff hanging out in ##c++ on FreeNode. The people in there can be mean and rude and snooty, so you need a thick skin. But they are also quite brilliant and totally know what they are talking about. – Galik Sep 22 '14 at 18:03
  • @Galik Thank you very much, I'm going to check out all those links! – Fly Sep 22 '14 at 18:16

1 Answers1

0

I haven't trawled through your code but no, C++ does not have any run-time type information (reflection) support.

If you are using reflection you need to find another way to achieve the same results (perhaps using interfaces or templates) in order to port it to C++.

Tim B
  • 40,716
  • 16
  • 83
  • 128
  • a bit nit-picky, but c++ does have a form of run-time type information, its just that what it does have does not support reflection. – diverscuba23 Sep 22 '14 at 16:28
  • @TimB Thanks for your input. Interfaces are the same as in Java I guess, so I'm going to read a few tutorials about templates. – Fly Sep 22 '14 at 17:11