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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:51:34+00:00 2026-05-22T17:51:34+00:00

(Firstly, as a disclaimer, this is related to an assignment. I’m not asking anyone

  • 0

(Firstly, as a disclaimer, this is related to an assignment. I’m not asking anyone to do my assignment for me, just to try and help me understand how to implement templates properly.)

My current setup is:

I have class A, which is a base class.
Class B, C and D all children of Class A.

I’m trying to make a linked list that, within a single list, can point to either B, C or D.

What I currently have setup is something like this:

enum Types { TypeB, TypeC, TypeD }

struct Node
{
    void * pointerToElement;
    int type;
    Node * next;
};

struct Header
{
    int counter;
    Node * first;
};

This actually works. When I go through the linked list to print out all the elements, I use the and if statement and the int type to identify what kind it is (based on the ENUM defined), and then use static_cast to cast the void pointer to a pointer of either class B, C or D.

Now, I’ve been told I’m required to use templates instead, which is causing a whole lot of headache. I haven’t done much with templates, but my experience with them hasn’t been all that pleasant.

My understanding of templates is that I could use it to define the whole linked list with EITHER class B, C or D, but it wouldn’t be plausible to have B, C or D all appearing in the same linked list?

I have tried the following:

enum Types { TypeB, TypeC, TypeD } // I realise that if templates work, I won't need this

template <class T>
struct Node
{
    T * pointerToElement;
    int type;
    Node<T> * next;    // Reason 1 I suspect I could only use one type
};

template <class T>
struct Header
{
    int counter;
    Node<T> * first;    // Reason 2 I suspect I could only use one type
};

The main question I have, are templates supposed to be able to do this? When implementing it in a class, I needed to specify a type for the header, which I didn’t want to do, so I made that class a template as well, and it kept following through to the rest of my code which shouldn’t need to be a template, and finally got to main() where I would have had to define either class B, C or D.

Comments and suggestions appreciated.

Thanks.

Edit

Thanks everyone for the comments, I’ve probably learnt more trying this then from the lectures.

What I’ve done is pretty much ditched templates, or at least the way I was trying to use them. I have used templates (unfortunately for the sake of using templates,) and it works. Here’s what I’ve now done (all worked out from all of the helpful comments… thanks!)

template <class T>
struct Node
{
    T * pointerToElement;
    int type;   // I can get rid of this after I go through the code and remove all references to it, which I am doing now.
    Node<T> * next;    // Reason 1 I suspect I could only use one type
};

template <class T>
struct Header
{
    int counter;
    Node<T> * first;    // Reason 2 I suspect I could only use one type
};

is still the way it was, but when declaring the Header, I declare it as:

Header * myHeader;

(I’m using a class structure identical to the one in the solution below).

So this is pointing to the base class, the one that all the other classes are derived from. Then, because of inheritance, I can store class B, C or D in there without any problems, and providing that all the functions defined in the derived classes (B, C and D) are defined in the base class, I can call it directly without having to cast it (for example, they all have their own print function, and it calls the correct one when it is defined in the derived class).

I think the idea that the Assignment is trying to get across is a linked list that can be used with any type, I think there was some miscommunication (probably mostly due to me,) were I thought templates were supposed to be used to define different class types in each node, rather they can be used to define the base class.

  • 1 1 Answer
  • 3 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-22T17:51:35+00:00Added an answer on May 22, 2026 at 5:51 pm

    Instead of templates you should be using inheritance. Templates, as you guessed, create instance per type T, and that is not what you want.

    Your code should be something in these lines:

    class A{...};
    class B: public A{...};
    class C: public A{...};
    class D: public A{...};
    struct Node{
        A *next;
    }
    

    you can assign to next pointers to either A, B, C, or D. Make sure to mark member functions as virtual where appropriate.

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

Sidebar

Related Questions

Firstly, this is going to sound like homework, but it ain't. Just a problem
Firstly, this is a homework assignment, and I am very new to programming in
Firstly, I'm not using rails. This is vanilla ruby application. I've read about packaging
Firstly, very sorry if this is not a true stackoverflow question. But it's something
Firstly appologies for the poor title, not sure how to explain this in one
Firstly, this is not a question about repository synchronisation for which there are numerous
Firstly, I wrote my customized setter for an NSString* like this: - (void)setDateString:(NSString *)newDateString
Firstly, is there a command for getting help and parameters for each sos command
Firstly sorry if this is a common question but I couldn't find anything on
Firstly, I know this [type of] question is frequently asked, so let me preface

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.