Announcement

Collapse
No announcement yet.

C++ and polymorphism

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • C++ and polymorphism

    Okay, first off: I have been spoiled by Java.



    So, now, I am going back to programming in C++ and I am having a slight problem. In Java I made much use of the instanceof operator to determine which derived class an object was an instance of. Now, sure enough, I was coding in C++ the other day and wanted to use that same thing, but... It looks like there is no such thing. So the question is:

    Suppose I have some base class, and a bunch of derived classes. I also have a method that should process an object. And that object can be an instance of any of the derived classes. So, I specify the type of the parameter as the base class. However, for bookkeeping purposes, I would still need to know exactly which of the derived classes the parameter is an instance of. Is there any way at all to do it? (Short of putting an identifyier string member into the base class and initializing it to different values in the derived classes?)

    Any help would be appreciated.
    XBox Live: VovanSim
    xbox.com (login required)
    Halo 3 Service Record (I fail at FPS...)
    Spore page

  • #2
    Re: C++ and polymorphism

    Originally posted by vovansim
    (Short of putting an identifyier string member into the base class and initializing it to different values in the derived classes?)
    shot my idea down.
    "I've lived too long with pain. I won't know who I am without it. We have to leave this place, I am almost happy here."
    - Ender, from Ender's Game by Orson Scott Card

    Comment


    • #3
      No way. I'm pretty sure the instanceof operator only works in Java because of reflection, which I'm pretty sure can only work in an interpreted language (otherwise things such as field and method names are lost).

      Comment


      • #4
        i'd just use templated functions with a name member var, if you really need to know what class it is (although, ideally, you should never need to know)
        "I've lived too long with pain. I won't know who I am without it. We have to leave this place, I am almost happy here."
        - Ender, from Ender's Game by Orson Scott Card

        Comment


        • #5
          Uber - that's not true. Say you're retrieving something dynamically from a hashtable. It contains a bunch of "Airplanes", but some of these are "CivilianAirplanes" and others are "MilitaryAirplanes", which have different methods and fields. To know what to cast it as, you have to know which subclass it's an instance of. Storing the data in a string would work, but could be VERY memory-intensive.

          Comment


          • #6
            Originally posted by skywalker
            Uber - that's not true. Say you're retrieving something dynamically from a hashtable. It contains a bunch of "Airplanes", but some of these are "CivilianAirplanes" and others are "MilitaryAirplanes", which have different methods and fields. To know what to cast it as, you have to know which subclass it's an instance of. Storing the data in a string would work, but could be VERY memory-intensive.
            i didn't mean store all the data in the string, i meant store the class name if you needed to.

            i'm saying that if he's writing a function that works on all plane objects, he shouldn't need to know what kind of plane it is. and if it's plane specific, it should be a member function of the derivative class anyway.
            "I've lived too long with pain. I won't know who I am without it. We have to leave this place, I am almost happy here."
            - Ender, from Ender's Game by Orson Scott Card

            Comment


            • #7
              Even storing the class name could get bulky if he has a lot of instances.

              If it's plane specific, he has to know what type of plane to know which methods he can call.

              Comment


              • #8
                Alright. Thanks. That's what I thought. Just making sure I am not missing something here. I think I have found a little workaround for my issue. Thanks for the feedback any ways.
                XBox Live: VovanSim
                xbox.com (login required)
                Halo 3 Service Record (I fail at FPS...)
                Spore page

                Comment


                • #9
                  Even storing the class name could get bulky if he has a lot of instances.

                  Aren't there static variables in C++?
                  Blog | Civ2 Scenario League | leo.petr at gmail.com

                  Comment


                  • #10
                    Nobody would put a identifier string in every instance. Better to define a pure method in the base class, like

                    virtual const char* ClassName() = 0;

                    and overwrite it in the derived classes, like this:

                    class MyDerivedClass : public MyBaseClass {
                    ...
                    virtual const char* ClassName();
                    ...
                    };

                    const char* MyDerivedClass::ClassName()
                    {
                    return "MyDerivedClass";
                    }

                    In this case, the ID string exists only once per application.

                    Comment


                    • #11
                      I have very little experience with c++. What does virtual do?

                      However, since the program itself doesn't know what class the object is an instance of, wouldn't it call the wrong method?

                      Comment


                      • #12
                        virtual makes you call the latest derived version of a function. For instance, you have the mentioned class Airplane and derived classes CivilianAirplane and MilitaryAirplane. Now you have a pointer

                        Airplane* myPtr;

                        and assign a pointer to an object of one of the derived classes to it, let's say a pointer to a CivilianAirplane. Note that the content of the pointer is seen as instance of the base class Airplane. Now you call the method ClassName:

                        std::cout << myPtr->ClassName() << std::endl;

                        and in the standard output appears the string "CivilianAirplane", although myPtr points to the class Airplane and not CivilianAirplane. If the function was not virtual, the function Airplane::ClassName would be called.

                        Note that pure methods (those that end with = 0) are always virtual.

                        Comment


                        • #13
                          Thanks.

                          That's pretty useful.

                          Comment


                          • #14
                            Originally posted by Sir Ralph
                            Nobody would put a identifier string in every instance. Better to define a pure method in the base class.
                            Right, that's what I meant. Sorry if I didn't make it clearer...
                            XBox Live: VovanSim
                            xbox.com (login required)
                            Halo 3 Service Record (I fail at FPS...)
                            Spore page

                            Comment


                            • #15
                              So because a lot of C programmers is here. A little question.

                              So imagine DLL file. I know name of method in it and I would like to call it from Java. I know I need to have wrapper by JNI however how can I do wrapper. (There might be a little problem if I find no compiller. )

                              Could you write a compilable example? Use NewLibrary.DLL as name of library.

                              Comment

                              Working...
                              X