Classes from pointers?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • zRaze

    Classes from pointers?

    Scenario:
    I have a base class called "BaseCls", and two derived classes called
    "DerCls1" and "DerCls2"

    I declare a variable -
    BaseCls *MyClassObj;

    I have some code -

    If( Condition )
    {
    MyClassObj = new DerCls1;
    } else {
    MyClassObj = new DerCls2;
    }

    How can I tell if MyClassObj is pointing to a DerCls1 or DerCls2
    object?

    Thanks in advance...
  • Jakob Bieling

    #2
    Re: Classes from pointers?

    "zRaze" <abuse@msn.co m> wrote in message
    news:o2agov8svc pv69oah2mus8ebk a1v1fq0sl@4ax.c om...[color=blue]
    > Scenario:
    > I have a base class called "BaseCls", and two derived classes called
    > "DerCls1" and "DerCls2"
    >
    > I declare a variable -
    > BaseCls *MyClassObj;
    >
    > I have some code -
    >
    > If( Condition )
    > {
    > MyClassObj = new DerCls1;
    > } else {
    > MyClassObj = new DerCls2;
    > }
    >
    > How can I tell if MyClassObj is pointing to a DerCls1 or DerCls2
    > object?[/color]


    One way would be to have a virtual function in your base class, that
    returns some info that it is the base class. The derived classes are
    required to overload it and return info depending on what class they are.

    Another way is to use dynamic_cast. If you cast the base class pointer
    to the wrong derived class pointer type, you will get a null pointer.

    Personally, I would prefer the former method, even tho you have the
    disadvantage of requiring all derived classes to overload that function. If
    the base class is (or can be) abstract anyway, you could make that make the
    'info' function pure virtual even.

    hth
    --
    jb

    (replace y with x if you want to reply by e-mail)


    Comment

    • zRaze

      #3
      Re: Classes from pointers?

      Thanks!

      On Sat, 11 Oct 2003 18:05:13 +0200, "Jakob Bieling" <netsurf@gmy.ne t>
      wrote:
      [color=blue]
      >"zRaze" <abuse@msn.co m> wrote in message
      >news:o2agov8sv cpv69oah2mus8eb ka1v1fq0sl@4ax. com...[color=green]
      >> Scenario:
      >> I have a base class called "BaseCls", and two derived classes called
      >> "DerCls1" and "DerCls2"
      >>
      >> I declare a variable -
      >> BaseCls *MyClassObj;
      >>
      >> I have some code -
      >>
      >> If( Condition )
      >> {
      >> MyClassObj = new DerCls1;
      >> } else {
      >> MyClassObj = new DerCls2;
      >> }
      >>
      >> How can I tell if MyClassObj is pointing to a DerCls1 or DerCls2
      >> object?[/color]
      >
      >
      > One way would be to have a virtual function in your base class, that
      >returns some info that it is the base class. The derived classes are
      >required to overload it and return info depending on what class they are.
      >
      > Another way is to use dynamic_cast. If you cast the base class pointer
      >to the wrong derived class pointer type, you will get a null pointer.
      >
      > Personally, I would prefer the former method, even tho you have the
      >disadvantage of requiring all derived classes to overload that function. If
      >the base class is (or can be) abstract anyway, you could make that make the
      >'info' function pure virtual even.
      >
      >hth[/color]

      Comment

      • Cy Edmunds

        #4
        Re: Classes from pointers?

        "zRaze" <abuse@msn.co m> wrote in message
        news:o2agov8svc pv69oah2mus8ebk a1v1fq0sl@4ax.c om...[color=blue]
        > Scenario:
        > I have a base class called "BaseCls", and two derived classes called
        > "DerCls1" and "DerCls2"
        >
        > I declare a variable -
        > BaseCls *MyClassObj;
        >
        > I have some code -
        >
        > If( Condition )
        > {
        > MyClassObj = new DerCls1;
        > } else {
        > MyClassObj = new DerCls2;
        > }
        >
        > How can I tell if MyClassObj is pointing to a DerCls1 or DerCls2
        > object?
        >
        > Thanks in advance...[/color]

        If you have to make that distinction you probably have a poor design. When
        you have a pointer to a base class which really points to a derived class,
        you are using polymorphism. As Jakob implied, the best way to deal with
        polymorphism is to call a virtual function which does the right thing no
        matter what type it is. This scheme is efficient and maintainable. (For
        instance it would be easy to add a DerCls3 later.) If it is impossible
        because you can't implement any such virtual function then maybe
        polymorphism was the wrong approach in the first place.

        A couple of other points: you should probably put a do-nothing virtual
        destructor in the base class:

        virtual ~BaseCls() {}

        to make sure possible destructors in derived classes are called properly.
        Also, whenever you use new() you must use delete() to avoid a memory leak
        and you must use it only once to avoid undefined behaviour. This can be a
        source of problems. Consider using a reference counted smart pointer instead
        of a raw pointer for MyClassObj:

        typedef boost::shared_p tr<BaseCls> BaseClsPtr;

        MyClassObj = BaseClsPtr(new DerCls1);

        It will automatically delete the object when the pointer goes out of scope
        so you don't have to worry about it.You can get such a pointer at



        Good luck.

        --
        Cy



        Comment

        • zRaze

          #5
          Re: Classes from pointers?

          Basically, the reason I need to know what class a pointer points to is
          because I have a 2 dimensional array of pointers (it's a map)
          and my classes are the landmarks etc. (Road, open space etc.)

          So therefore, I don't have the problem with adding new classes, but
          when I come do save the map to disk, I need to be able to tell what is
          on that tile for when reloading.

          I have now got a new method of this. I had a function that returned a
          graphic handle (for drawing on the screen), however, I was having
          problems (which I thought was related, but wasn't) so I changed it to
          return an enum value which gets linked to the graphic, and I'm just
          going to use that enum value...

          Thanks for your help tho...

          On Sun, 12 Oct 2003 02:47:54 GMT, "Cy Edmunds"
          <cedmunds@spaml ess.rochester.r r.com> wrote:
          [color=blue]
          >"zRaze" <abuse@msn.co m> wrote in message
          >news:o2agov8sv cpv69oah2mus8eb ka1v1fq0sl@4ax. com...[color=green]
          >> Scenario:
          >> I have a base class called "BaseCls", and two derived classes called
          >> "DerCls1" and "DerCls2"
          >>
          >> I declare a variable -
          >> BaseCls *MyClassObj;
          >>
          >> I have some code -
          >>
          >> If( Condition )
          >> {
          >> MyClassObj = new DerCls1;
          >> } else {
          >> MyClassObj = new DerCls2;
          >> }
          >>
          >> How can I tell if MyClassObj is pointing to a DerCls1 or DerCls2
          >> object?
          >>
          >> Thanks in advance...[/color]
          >
          >If you have to make that distinction you probably have a poor design. When
          >you have a pointer to a base class which really points to a derived class,
          >you are using polymorphism. As Jakob implied, the best way to deal with
          >polymorphism is to call a virtual function which does the right thing no
          >matter what type it is. This scheme is efficient and maintainable. (For
          >instance it would be easy to add a DerCls3 later.) If it is impossible
          >because you can't implement any such virtual function then maybe
          >polymorphism was the wrong approach in the first place.
          >
          >A couple of other points: you should probably put a do-nothing virtual
          >destructor in the base class:
          >
          >virtual ~BaseCls() {}
          >
          >to make sure possible destructors in derived classes are called properly.
          >Also, whenever you use new() you must use delete() to avoid a memory leak
          >and you must use it only once to avoid undefined behaviour. This can be a
          >source of problems. Consider using a reference counted smart pointer instead
          >of a raw pointer for MyClassObj:
          >
          >typedef boost::shared_p tr<BaseCls> BaseClsPtr;
          >
          >MyClassObj = BaseClsPtr(new DerCls1);
          >
          >It will automatically delete the object when the pointer goes out of scope
          >so you don't have to worry about it.You can get such a pointer at
          >
          >www.boost.org
          >
          >Good luck.[/color]

          Comment

          • jeffc

            #6
            Re: Classes from pointers?


            "zRaze" <abuse@msn.co m> wrote in message
            news:o2agov8svc pv69oah2mus8ebk a1v1fq0sl@4ax.c om...[color=blue]
            > Scenario:
            > I have a base class called "BaseCls", and two derived classes called
            > "DerCls1" and "DerCls2"
            >
            > I declare a variable -
            > BaseCls *MyClassObj;
            >
            > I have some code -
            >
            > If( Condition )
            > {
            > MyClassObj = new DerCls1;
            > } else {
            > MyClassObj = new DerCls2;
            > }
            >
            > How can I tell if MyClassObj is pointing to a DerCls1 or DerCls2
            > object?[/color]

            This is not a hard and fast rule, but one of the basic principles OO code
            such as that is you're not *supposed* to know. And you're supposed to write
            your code in such a way that the code doesn't care either.


            Comment

            Working...