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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:40:19+00:00 2026-06-18T05:40:19+00:00

I define a Node class with template for its value type template<class T> class

  • 0

I define a Node class with template for its value type

template<class T>
class Node {
  T val;
  public:
    Node (T & v) : val (v) {}
    ...
    void print() { cout << v << endl; }
}

Most times, the node value of interest will be an object class, say class Foo. In that case, use Node<Foo *> will be more convenient. But it could also be that the node will hold primitive time, say int. Then use Node<int> will suffice.

The problem is, some of the function may need to behave differently based on whether T is a pointer type or not. For example, print should cout << *v when it is and cout << v otherwise.

What I’ve tried is to define both:

template<class T>
class Node {
  T val;
  public:
    Node (T & v) : val (v) {}
    ...
    void print() { cout << v << endl; }
}

template<class T>
class Node<T*> {
  T* val;
  public:
    Node (T* v) : val (v) {}
    ...
    void print() { cout << *v << endl; }
}

It now can choose the appropriate definition based on whether it’s Node<int> or Node<int *> But the problem become, the two definitions will share many code. I’m wondering whether there’s better way to achieve this.

  • 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-18T05:40:21+00:00Added an answer on June 18, 2026 at 5:40 am

    See this: C++ template specialization, calling methods on types that could be pointers or references unambiguously

    The same technique should work here, allowing you to deal with the val as a reference (or a pointer) uniformly in both cases.

    CRTP may help reduce code duplication, allowing for common code for two specializations without any overhead, as well.

    Note that ownership semantics get tricky when you sometimes use a pointer and sometimes an instance — what is the lifetime of val if sometimes it is a pointer of an argument, and other times it is a copy of the argument, and how to you enforce it?

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

Sidebar

Related Questions

template<typename Type> class List { public: List(void); ~List(void); ... } which inherited by template<typename
template <class T> class list { public: //stuff list(const list &cctorList); //cctor private: struct
Is it possible to get a node by its internalId? I tried: Store Ext.define('myStore',
I have a Binary-Tree class template with several public and private member functions. When
I'm trying to define a template member inside a template class. Here is a
I have a template class btree and have defined a struct Node in the
How can I get the code below to output node names that I define?
Okay, guys i have been trying to define a Stack , each node also
I'm using Mongoose, MongoDB, and Node.js. I would like to define a schema where
#include <cstdlib> #include <iostream> #include Node.h #ifndef HW4_H #define HW4_H using namespace std; /**

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.