MWright
MWright

Reputation: 1711

Mixing Generic Programming with Polymorphism

I have seen a few articles on generic programming and how you should never use virtual functions and templates together. I understand this idiom, as templates are decided at compilation, where virtual functions are not chosen until run-time (over simplification).

However, I have a bit of code that uses OO style and Generic style together and it seems to work the way I want.

My Question:

Is the following design bad practice. Mixing Polymorphism and Generic Code?

Is there any pit falls with my code below. (I know I should not inherit data members, but I have :-/).

#ifndef BaseTemplate_H
#define BaseTemplate_H

#include <vector>

template< class T >
class BaseTemplate {

 public:

  typedef std::vector<T*> pVT;

  BaseTemplate(){}
  virtual ~BaseTemplate(){};
  virtual void Process()=0;
  const pVT& getContainer(){ return m_pContainer; }

 protected:

  pVT m_pContainer;

 private:

  BaseTemplate( const BaseTemplate& cpy );
  BaseTemplate& operator=( const BaseTemplate& rhs);



};

#endif

I inherit from the base class first by telling the base template what type I would like when inheriting. This will allow me two inherit for multiple types, which I want to keep separate in my design.

#ifndef DerClassA_H
#define DerClassA_H

#include <iostream>
#include "BaseTemplate.h"

class DerClassA: public BaseTemplate<int> {

 public:

 DerClassA(){}
 virtual ~DerClassA(){}
 virtual void Process(){
   std::cout << "HELLO I AM: DerClassA" << std::endl;
 }//This will push_back objects to m_pContainer

 private:

 DerClassA( const DerClassA& cpy );
 DerClassA& operator=( const DerClassA& rhs);



};

#endif


#ifndef DerClassB_H
#define DerClassB_H

#include <iostream>
#include "DerClassA.h"

class DerClassB: public DerClassA {

 public:

 DerClassB(){}
 virtual ~DerClassB(){}
 virtual void Process(){
   std::cout << "HELLO I AM: DerClassB" << std::endl;
 }//This will push_back objects to m_pContainer

 private:

  DerClassB( const DerClassB& cpy );
  DerClassB& operator=( const DerClassB& rhs);



};

#endif

#include "DerClassA.h"
#include "DerClassB.h"

int main()
{

  BaseTemplate<int> *pClassA = new DerClassA();
  pClassA->Process();

  DerClassA *pClassB = new DerClassB();
  pClassB->Process();

  delete pClassA;
  delete pClassB;

  return 0;
}

Upvotes: 3

Views: 764

Answers (1)

StackedCrooked
StackedCrooked

Reputation: 35505

Is the following design bad practice. Mixing Polymorphism and Generic Code?

No, that's sometimes the right thing to do.

Is there any pit falls with my code below.

  • The container with raw pointers std::vector<T*> pVT; looks a bit fishy.
  • The base class destructor should probably be pure virtual.
  • I would use the C++11 syntax for making the class non-copyable.
  • You only need to make the base class non-copyable.
  • I don't see the need for dynamic allocation in your main function.

For the rest I don't see any immediate errors.

It's not possible to say whether or not your design is good without knowing what you are trying to do.

Upvotes: 4

Related Questions