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

  • Home
  • SEARCH
  • 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 616279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:18:36+00:00 2026-05-13T18:18:36+00:00

I want to implement a Mesh class for a CG project, but have run

  • 0

I want to implement a Mesh class for a CG project, but have run into some problems.
What I want to do is a Mesh class that hides implementation details (like loading to a specific API: OpenGL, DirectX, CUDA, …) from the user. Additionally, since the Mesh class will be used in research projects, this Mesh class has to be very flexible.

class Channel {
  virtual loadToAPI() = 0;      
}

template <class T>
class TypedChannel : public Channel {

  std::vector<T> data;
};

template <class T>
class OpenGLChannel : public TypedChannel<T> {

  loadToAPI(); // implementation
};

class Mesh {

  template<class T>
  virtual TypedChannel<T>* createChannel() = 0; // error: no virtual template functions

  std::vector<Channel*> channels;
};

class OpenGLMesh {

  template<class T>
  TypedChannel<T>* createChannel()
  {
    TypedChannel<T>* newChannel = new OpenGLChannel<T>;
    channels.push_back(newChannel);
    return newChannel;
  };
};

For flexibility, each Mesh is really a collection of channels, like one position channel, a normal channel, etc. that describe some aspects of the mesh. A channel is a wrapper around a std::vector with some added functionality.

To hide implementation details, there is a derived class for each API (OpenGLMesh, DirectXMesh, CUDAMesh, …) that handles API-specific code. The same goes for the Channels (OpenGLChannel, etc. that handle loading of the Channel data to the API). The Mesh acts as a factory for the Channel objects.

But here is the problem: Since the Channels are template classes, createChannel must be a template method, and template methods cannot be virtual. What I would need is something like a Factory Pattern for creating templated objects. Does anyone have advice on how something similar could be accomplished?

Thanks

  • 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-05-13T18:18:36+00:00Added an answer on May 13, 2026 at 6:18 pm

    It’s an interesting problem, but let’s discuss the compiler error first.

    As the compiler said, a function cannot be both virtual and template. To understand why, just think about the implementation: most of the times, objects with virtual functions have a virtual table, which stores a pointer to each function.

    For templates however, there are as many functions as combinations of type: so what should be the virtual table like ? It’s impossible to tell at compilation time, and the memory layout of your class includes the virtual table and has to be decided at compilation time.

    Now on to your problem.

    The simplest solution would be to just write one virtual method per type, of course it can soon become tedious, so let’s pretend you haven’t heard that.

    If Mesh is not supposed to know about the various types, then surely you don’t need the function to be virtual, because who would know, given an instance of Mesh, with which type invoking the function ?

    Mesh* mesh = ...;
    mesh.createChannel<int>(); // has it been defined for that `Mesh` ??
    

    On the other hand, I will suppose that OpenGLMesh does know exactly which kind of TypedChannel it will need. If so, we could use a very simple trick.

    struct ChannelFactory
    {
      virtual ~ChannelFactory() {}
      virtual Channel* createChannel() = 0;
    };
    
    template <class T>
    struct TypedChannelFactory: ChannelFactory
    {
    };
    

    And then:

    class Mesh
    {
    public:
      template <class T>
      Channel* addChannel()
      {
        factories_type::const_iterator it = mFactories.find(typeid(T).name());
        assert(it != mFactories.end() && "Ooops!!!" && typeid(T).name());
    
        Channel* channel = it->second->createChannel();
        mChannels.push_back(channel);
        return channel;
      } // addChannel
    
    protected:
      template <class T>
      void registerChannelFactory(TypedChannelFactory<T>* factory)
      {
        mFactories.insert(std::make_pair(typeid(T).name(), factory));
      } // registerChannelFactory
    
    private:
      typedef std::map < const char*, ChannelFactory* const > factories_type;
      factories_type mFactories;
    
      std::vector<Channel*> mChannels;
    }; // class Mesh
    

    It demonstrates a quite powerful idiom known as type erasure. You probably used it even before you knew the name 🙂

    Now, you can define OpenGLMesh as:

    template <class T>
    struct OpenGLChannelFactory: TypedChannelFactory<T>
    {
      virtual Channel* createChannel() { return new OpenGLChannel<T>(); }
    };
    
    OpenGLMesh::OpenGLMesh()
    {
      this->registerChannelFactory(new OpenGLChannelFactory<int>());
      this->registerChannelFactory(new OpenGLChannelFactory<float>());
    }
    

    And you’ll use it like:

    OpenGLMesh openGLMesh;
    Mesh& mesh = openGLMesh;
    
    mesh.addChannel<int>();    // fine
    mesh.addChannel<float>();  // fine
    
    mesh.addChannel<char>();   // ERROR: fire the assert... (or throw, or do nothing...)
    

    Hope I understood what you needed :p

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

Sidebar

Ask A Question

Stats

  • Questions 337k
  • Answers 337k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If your program is 32-bit, be sure that your library… May 14, 2026 at 4:05 am
  • Editorial Team
    Editorial Team added an answer Because it's older and more established. Therefore there are more… May 14, 2026 at 4:05 am
  • Editorial Team
    Editorial Team added an answer I contacted hosting support and there is a hardware firewall… May 14, 2026 at 4:05 am

Related Questions

The overall goal I'm trying to accomplish is to implement a customizable avatar system
Hokay so here is what I'm trying to accomplish: I'm going to be sending
I've been trying to set up some sort of geometry batching for a week
I've been thinking lately, would it be a good form of syntactic sugar in
I want to implement a paperless filing system and was looking to use WIA

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.