Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8177711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:34:51+00:00 2026-06-06T23:34:51+00:00

I have seen a few articles on generic programming and how you should never

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:34:52+00:00Added an answer on June 6, 2026 at 11:34 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen few py scripts which use this at the top of the
I have seen a few tutorials on jQuery templates , which as far as
I have seen a few posts regarding this issue but not one specific to
I have seen a few similar questions but I think this is new... I
I have seen few questions similar to this one, but I wanted to make
I have seen a few variants of this nested UL array question on stackoverflow,
I have seen a few post on this, but nothing that would work entirely
I have seen a few companies recently that are releasing live email content. This
I have seen some articles that show how to use AlternationIndex with ListBox es
I have seen a few things written about this and remember one where there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.