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 184025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:13:10+00:00 2026-05-11T15:13:10+00:00

I have a class that my client uses to Get() a packet. The packet

  • 0

I have a class that my client uses to Get() a packet. The packet contains a std::vector whose type is not known until the packet is generated internally in my Interface class (in this example, it depends on the Packet::type variable).

I was wondering if template could be used for this since the Packet class is just a generic class whose type can be pretty much anything.

The problem with it, as far as I can think of, is that the client has no clue what type of packet it is until he gets the packet and look at the Packet::type member. So, this wouldnt work because he wouldnt be able to declare a variable that Get() would return (?)

Could template be used elegantly in this case?

One alternative I could think of is to define a base class, and create a child class for each type. Then, the Get() method could return a pointer to the base class. Then client can simply look at the Packet::type (packet->type) and cast it to be the appropriate child class. But that’s a bit messy? Is there a more elegant solution?

The code below demonstrates the scenario roughly:

enum {   T_FLOAT,   T_COMPLEX } TYPE_ENUM;  // T can either be of type float or std::complex template<typename T> class Packet { public:   TYPE_ENUM       type;   std::vector<T>  data; };  class Interface {  public:   // Method that client calls to obtain the packet   Packet<>  Get()   {     return queue.pop(); // return current packet in queue   }  private:   Queue<Packet>   queue; }; 
  • 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. 2026-05-11T15:13:11+00:00Added an answer on May 11, 2026 at 3:13 pm

    You would want to create a base class, and keep the template Packet class that you have. Call this base class PacketBase. Your Packet class would derive from the new PacketBase class. That is to say each Packet<> type that is generated at compile time, will derive from PacketBase.

    class PacketBase {  };  // T can either be of type float or std::complex or ... template<typename T> class Packet : public PacketBase { public:   std::vector<T>  data;    //Put other members you need here   //Note you don't need the type member that you had before.  }; 

    Interface::Get would return a PacketBase*. Here PacketBase is just used as a generic name that holds any Packet<> type. The queue would store a collection of PacketBase*.

    class Interface {  public:   // Method that client calls to obtain the packet   PacketBase*  Get()   {     return queue.pop(); // return current packet in queue   }  private:   Queue<PacketBase*>   queue; }; 

    To figure out which type of Packet you have you can use RTTI with dynamic_cast.

    InterfaceObject o; //fill the queue PacketBase *pPacket = o.Get(); if(dynamic_cast<Packet<float> * >(pPacket) != NULL)    ;//You have a Packet<float> else if(dynamic_cast<Packet<std::complex> * >(pPacket) != NULL)    ;//You have a Packet<std::complex> //... for each Packet<> type you have 

    You can also beef up your PacketBase with some virtual methods. Then you can call those directly instead of the RTTI dynamic_cast stuff.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The problem is that assigning an array does not copy… May 11, 2026 at 6:42 pm
  • Editorial Team
    Editorial Team added an answer Unfortunately, the C standard says very little about concurrency. However,… May 11, 2026 at 6:42 pm
  • Editorial Team
    Editorial Team added an answer If it's a paddle, then why not paddleBound? That's not… May 11, 2026 at 6:42 pm

Related Questions

I have an ASP.NET MVC (using the release candidate) application that works with a
I'm trying to create an in-process unit test for my service to client interactions
I have a legacy class that the class itself is not a generic but
I'm still new to the ASP.NET world, so I could be way off base
I have created a a subclass of Table in my ASP.NET project which creates.

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.