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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:57:38+00:00 2026-05-21T16:57:38+00:00

Hya, Lemme explain my point. template<typename T> class node{ T data; template<typename X> node<X>*

  • 0

Hya,

Lemme explain my point.

template<typename T>
class node{
  T data;
  template<typename X>
  node<X>* right; // can point to any node<typename> i know its wrong
}

so that i can do something like:

node<int> a;
a.data = 23;
node<float> b;
b.data =43.6;
a.right= b;
std::cout<< a.data <<a.right->data;

Another example:

template <class Type>
struct vnode {
  Type data;
  vnode<Type> * vnodenext;
  // vrow what_to_put_here // **i don't want to use void ptrs neither want to cast back manually**
}

And in main function if I define vnode struct of type string and another vnode of type int, then what pointer def should I replace with vrow in vnode struct definition so that it can point to vnode of type int or other type of vnode? e.g.

vnode<string> mystring;
vnode<int> myint;

myint.vrow = &mystring
  • 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-21T16:57:39+00:00Added an answer on May 21, 2026 at 4:57 pm

    It isn’t really possible to do what you want because when using templates you have to know the types involved at compile time. In contrast, walking a previously constructed linked list requires you to discover the types in the list at runtime.

    To illustrate, consider this:

    struct node_base {
        virtual ~node_base() {}
    }
    
    template<typename T>
    struct node : public node_base {
        T data;
        node_base* right;
    }
    

    Now you can certainly have a list of node_base*, and those nodes can contain any type of data that you want. Constructing the list is not a problem, since at the point you add nodes the static type of data is known and you can create a node<TData>.

    Now the problem is how to get the data back. Assume that there’s a function that returns the data inside a node, given a pointer to that node. What should be the functions return type? Clearly (unless you know from beforehand that all data types share a common base) there is no single type that can be returned. That leaves you with:

    1. Returning a void*
    2. Writing a templated function that receives the data type as an argument

    However, #2 is not feasible in practice (although it works in theory). You cannot write the data type as a template argument because that would require you to know it at compile time, which defeats the purpose of a multi-data-type list.

    Therefore, the only solution left is returning a pointer type (either a node_base* or a void* to the data itself) and then casting that pointer to a useful type using some mechanism.

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

Sidebar

Related Questions

I am making a fraction class for a school project, and my brain is

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.