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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:38:13+00:00 2026-06-04T04:38:13+00:00

I have the following class: template <typename T> class Fixed2DContainer { T* ptr; public:

  • 0

I have the following class:

template <typename T>
class Fixed2DContainer {

    T* ptr;

public:
    const int total_cols;
    const int total_rows;

    Fixed2DContainer(int cols, int rows);
    T& operator()(int col_n, int row_n);
    ~Fixed2DContainer();

private : //disallow copy
    Fixed2DContainer& operator=(const Fixed2DContainer&);
    Fixed2DContainer operator()(const Fixed2DContainer&);
};

Now I’d like to specialize this template for some class so that the only change is that I can have an another constructor.
Basically I want to be able to do:

Fixed2DContainer<Image>("filename.jpg");

is there an elegant way to do this? I am fairly new to template so i have no idea of the difficulty

  • 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-04T04:38:14+00:00Added an answer on June 4, 2026 at 4:38 am

    Here’s a quick example of what I meant in my earlier comment …

    template <typename T>
    class Fixed2DContainerBase {
    
      T* ptr;
    
    public:
      const int total_cols;
      const int total_rows;
    
      Fixed2DContainerBase(int cols, int rows);
      T& operator()(int col_n, int row_n);
      ~Fixed2DContainerBase();
    
    private : //disallow copy
      Fixed2DContainerBase& operator=(const Fixed2DContainerBase&);
      Fixed2DContainerBase(const Fixed2DContainerBase&);
    };
    
    // primary template
    template <typename T>
    class Fixed2DContainer : public Fixed2DContainerBase<T> {
    
      Fixed2DContainer(int cols, int rows);
      ~Fixed2DContainer();
    };
    
    // explicit specialization
    template <>
    class Fixed2DContainer<Image> : public Fixed2DContainerBase<Image> {
    
      Fixed2DContainer(int cols, int rows);
      Fixed2DContainer(const std::string&);
      ~Fixed2DContainer();
    };
    

    N.B. because the base class is non-copyable the derived classes will be too. It may not be necessary to define a destructor in the derived classes if all the cleanup can be done by the base destructor.

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

Sidebar

Related Questions

I have the following class: template <typename T> class matrix { private: int _n;
I have the following pattern: template <int a, int b> class MyClass { public:
I have the following small code: template <typename T> class V { public: T
I have a class similar to the following: class SomeClass { public: template<typename... Args>
I have the following class definition: template<typename QueueItemT> class QueueBC { protected: QueueBC() {}
I have the following code: class TimeOutException {}; template <typename T> class MultiThreadedBuffer {
I have the following template method declared in my interface: class IObjectFactory { public:
I have the following code: template <class T> struct pointer { operator pointer<const T>()
I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(int
I have the following function as the constructor for a class: template<typename T> void

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.