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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:19:13+00:00 2026-06-12T10:19:13+00:00

Here is a simple piece of code : #include <iostream> #include <fstream> template<typename T>

  • 0

Here is a simple piece of code :

#include <iostream>
#include <fstream>

template<typename T>
class A {
    public:
        T _v;
        template<unsigned short V> void init() { _v *= V; }
        void print(double txt) { std::cout << "Initialized with ?? : " << txt << std::endl; }
};

int main() {
    A<double> foo;
    foo._v = 3.5;
    foo.init<2>();
    foo.print(foo._v);

    A<int> bar;
    bar._v = 2;
    bar.init<5>();
    foo.print(bar._v);
}

I would like to have an implementation of the function A::print(double) dependent on the unsigned short V, for example, replace ?? by the unsigned short from which init() has been instanciated. My questions are: (a) is it doable ? (b) if (a), how ?

While searching if (a), I thought I could add a functor to the class A<T>, initialize its state (with the value of V) in init(), call it in print(double), but I have never used such objects so I have no idea if this is the way to go. I’m basically open to any suggestions, the only thing I need is that the call to print remains the same (because I’ll call it from other classes who have no idea of the value of unsigned short V.

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-06-12T10:19:16+00:00Added an answer on June 12, 2026 at 10:19 am

    One way:

    template<typename T>
    class A {
        public:
            T _v;
            unsigned short _v2;
            template<unsigned short V> void init() { _v2 = V;  _v *= V; }
            void print(double txt) { std::cout << "Initialized with "
                << _v2 << " : " << txt << std::endl; }
    };
    

    Or why not:

    template<typename T, unsigned short V>
    struct A {
            T _v = V;
            void print(double txt) { std::cout << "Initialized with "
                << V << " : " << txt << std::endl; }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is a simple piece of code: import java.io.*; public class Read { public
I am writing a simple piece of code. class A { public: virtual func
Here is my simple piece of code, where I build a String array and
Here is a simple piece of code: HTML: <div id=close>Click me</div> JS Code: $(#close).click(function(){
Here a simple C# piece of code: Convert.ToInt32(TimeSpan.FromMinutes(5).TotalMilliseconds); //which brings me 300000 (int)TimeSpan.FromMinutes(5).Milliseconds; //which
Here is a very simple piece of jQuery code: $(document).ready(function() { alert('document ready was
here my simple code to add filename and their associated icon to virtualtreeview PFileInfoRec
All my code is here,quite simple,and I don't konw where it goes wrong. Person
my sample code is here include 'simple_html_dom.php'; function get_all_links($url){ global $host; $html = new
See this simple piece of code in PHP: //Documentation: //memcache_set ( string $key ,

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.