inheritance without using dynamic_cast

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

    #1

    inheritance without using dynamic_cast

    Hello all,

    I think the problem I'm facing is hard so I wonder if someone faced it
    before and if there is a simple solution to it. I am creating a
    program that basically takes all the required information at runtime
    after reading a file. After reading the file, several objects of a
    templated ClassA are created. I thought that the way to do this is to
    create an abstract class AbstractClassA and have all templated classes
    inherit from the Abstract class. Doing this I can work with pointers
    to the base class. In code:

    AbstractClassA {
    ...
    ...
    virtual ~AbstracClassA = 0;
    };

    template<class B, class ATraits>
    class classA : puclic AbstractClassA, public ATraits {
    // overide base class member functions
    ...
    // add other functions specific to the class
    ...
    // some functions that depend on the template parameter
    B someFun1(B&) {
    ...
    }
    B someFun2() {
    ...
    }

    };

    Now, the problem is that I cannot call an additional function of
    classA that is not in the interface of the abstract class because the
    compiler complains (as it should). For example, I cannot call

    typedef classA<int,IntT raitscaint;
    AbstractClassA* pa = new caint();
    int i1 = 10;
    pa->someFun1(i1) ; // error, someFun1 is not defined in AbstractClassA

    Of course, I cannot declare this function in the abstract class,
    because the function depends on a template parameter that doesn't even
    exist at that point.

    One dirty solution to this is to depend on the RTTI and use
    dynamic_cast against every single instantiation of ClassA. However,
    imagine that I have 100 of these different instances! That's why I
    templetized the class in the first place, I don't want to do any
    dynamic_cast on it.

    So I guess my question is if my logic right or I'm missing something
    here? if it is right, is my design right? or there is a better
    approach to do what I want to do?

    Thank you all,

    a²
  • Stuart Redmann

    #2
    Re: inheritance without using dynamic_cast

    aaragon wrote:
    Hello all,
    >
    I think the problem I'm facing is hard so I wonder if someone faced it
    before and if there is a simple solution to it. I am creating a
    program that basically takes all the required information at runtime
    after reading a file. After reading the file, several objects of a
    templated ClassA are created. I thought that the way to do this is to
    create an abstract class AbstractClassA and have all templated classes
    inherit from the Abstract class. Doing this I can work with pointers
    to the base class. In code:
    >
    AbstractClassA {
    ...
    ...
    virtual ~AbstracClassA = 0;
    };
    Your base class AbstractClassA is pretty useless this way. Unless _this_ class
    contains some pure virtual method declarations, there is no point in using it.
    template<class B, class ATraits>
    class classA : puclic AbstractClassA, public ATraits {
    // overide base class member functions
    ...
    // add other functions specific to the class
    ...
    // some functions that depend on the template parameter
    B someFun1(B&) {
    ...
    }
    B someFun2() {
    ...
    }
    >
    };
    >
    Now, the problem is that I cannot call an additional function of
    classA that is not in the interface of the abstract class because the
    compiler complains (as it should). For example, I cannot call
    Right. You can only call methods declared in AbstractClassA.
    You are mixing two design concepts of C++ that almost are mutual exclusive.
    Virtual inheritance is used to device at run-time which functionality should be
    used. In contrast to this, templates are used when you know at compile-time
    which functionality you want but don't want to code a similiar algorithm for
    various data types (this is the most probable scenario for using templates, of
    course there are cases where templates depend on other parameters than types).
    You cannot mix these two concepts!

    In your case it looks as if the class that should contain the virtual methods
    should be the class that you pass as template parameter B to the template
    classA. We can only give you a proper assessment of your design if you tell us
    what you are trying to do (unless this is confidental :-)

    Regards,
    Stuart

    Comment

    • alan

      #3
      Re: inheritance without using dynamic_cast

      On Nov 21, 2:28 pm, aaragon <alejandro.ara. ..@gmail.comwro te:
      Hello all,
      >
      I think the problem I'm facing is hard so I wonder if someone faced it
      before and if there is a simple solution to it. I am creating a
      program that basically takes all the required information at runtime
      after reading a file. After reading the file, several objects of a
      templated ClassA are created. I thought that the way to do this is to
      create an abstract class AbstractClassA and have all templated classes
      inherit from the Abstract class. Doing this I can work with pointers
      to the base class. In code:
      >
      AbstractClassA {
      ...
      ...
      virtual ~AbstracClassA = 0;
      virtual sometypethatdoe sntneedtemplate s
      someFun1(somety pethatdoesntnee dtemplates&);
      >
      };
      >
      template<class B, class ATraits>
      class classA : puclic AbstractClassA, public ATraits {
      // overide base class member functions
      ...
      // add other functions specific to the class
      ...
      // some functions that depend on the template parameter
      You can't get these from an AbstractClassA pointer:
      B someFun1(B&) {
      ...
      }
      B someFun2() {
      ...
      }
      >
      };
      >
      Now, the problem is that I cannot call an additional function of
      classA that is not in the interface of the abstract class because the
      compiler complains (as it should). For example, I cannot call
      >
      typedef classA<int,IntT raitscaint;
      AbstractClassA* pa = new caint();
      int i1 = 10;
      pa->someFun1(i1) ; // error, someFun1 is not defined in AbstractClassA
      >
      Of course, I cannot declare this function in the abstract class,
      because the function depends on a template parameter that doesn't even
      exist at that point.
      >
      One dirty solution to this is to depend on the RTTI and use
      dynamic_cast against every single instantiation of ClassA. However,
      imagine that I have 100 of these different instances! That's why I
      templetized the class in the first place, I don't want to do any
      dynamic_cast on it.
      >
      So I guess my question is if my logic right or I'm missing something
      here? if it is right, is my design right? or there is a better
      approach to do what I want to do?
      >
      Thank you all,
      >
      a²

      Comment

      • shazled@gmail.com

        #4
        Re: inheritance without using dynamic_cast

        On Nov 21, 6:28 am, aaragon <alejandro.ara. ..@gmail.comwro te:
        I think the problem I'm facing is hard so I wonder if someone faced it
        before and if there is a simple solution to it.
        I had a similar problem but only because I wanted to put the different
        templated classes in same datastructure.
        In code:
        >
        AbstractClassA {
        ...
        ...
        virtual ~AbstracClassA = 0;
        >
        };
        >
        template<class B, class ATraits>
        class classA : puclic AbstractClassA, public ATraits {
        // overide base class member functions
        ...
        // add other functions specific to the class
        ...
        // some functions that depend on the template parameter
        B someFun1(B&) {
        ...
        }
        B someFun2() {
        ...
        }
        >
        };
        >
        Now, the problem is that I cannot call an additional function of
        classA that is not in the interface of the abstract class because the
        compiler complains (as it should).
        For my particular problem, I found the visitor design pattern helped.

        Saul

        Comment

        • aaragon

          #5
          Re: inheritance without using dynamic_cast

          On Nov 22, 8:10 am, shaz...@gmail.c om wrote:
          On Nov 21, 6:28 am, aaragon <alejandro.ara. ..@gmail.comwro te:
          >
          I think the problem I'm facing is hard so I wonder if someone faced it
          before and if there is a simple solution to it.
          >
          I had a similar problem but only because I wanted to put the different
          templated classes in same datastructure.
          >
          >
          >
          In code:
          >
          AbstractClassA {
          ...
          ...
          virtual ~AbstracClassA = 0;
          >
          };
          >
          template<class B, class ATraits>
          class classA : puclic AbstractClassA, public ATraits {
          // overide base class member functions
          ...
          // add other functions specific to the class
          ...
          // some functions that depend on the template parameter
          B someFun1(B&) {
          ...
          }
          B someFun2() {
          ...
          }
          >
          };
          >
          Now, the problem is that I cannot call an additional function of
          classA that is not in the interface of the abstract class because the
          compiler complains (as it should).
          >
          For my particular problem, I found the visitor design pattern helped.
          >
          Saul
          That is exactly what I'm using now. I can use an abstract base class
          for the common part of the classes, and then use a visitor to work
          with specific functions of the derived template clases. It is actually
          very cool stuff.

          Comment

          Working...