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

  • Home
  • SEARCH
  • 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 160985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:07:56+00:00 2026-05-11T11:07:56+00:00

This is an odd one. Note this is cut down example code, and misses

  • 0

This is an odd one. Note this is cut down example code, and misses out destructors deliberately).

template <class f, class g> class Ptr;  class RealBase { };  template <class a, class b, class c = Ptr<a,b> > class Base : public RealBase { public:     Base(){}; };  template <class d, class e> class Derived : public Base <d,e> { public:     Derived(){};     void DerivedMethod(){}; }; typedef Derived<double,double> D_Derived;  template <class f, class g> class Ptr { public:     Ptr(){};     Ptr(Base<f,g,Ptr<f,g> >* a){in = a;};     Base<f,g,Ptr<f,g> >* operator->()     {         return in;     };      Base<f,g,Ptr<f,g> >& operator*()     {         return *in;     }; private:     Base<f,g,Ptr<f,g> >* in; }; 

I’m using the Ptr class from this example in a vector, as a pointer to a Derived class.

As we can see, the Ptr takes a Base<>* as it’s constructor argument.

Unfortunately I need a constructor that takes a const Base<>*, and I cannot simply do this:

Ptr(const Base<>* a) { in = const_cast<Base<>*>(a)}; 

Any ideas how I can make this class accept a const Base<>* as it’s constructor?


Edit:

Ok, turns out I can fix this by making changes to unrelated code, so it’s a bit of a non-problem now =] Took me about a day to fix it though =[

  • 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. 2026-05-11T11:07:57+00:00Added an answer on May 11, 2026 at 11:07 am

    I think you need to define a separate class to wrap pointers to const, since not only the arguments of the constructor, but also the return types of the operators should be changed to const versions. If you make the ConstPtr a friend of Ptr, this should work out quite nicely:

    template<...> class ConstPtr {   const Base<...> *in;   ConstPtr(Base<...>* a) { in = a; }   ConstPtr(const Base<...>* a) { in = a; }   ConstPtr(const Ptr<...> &a) { in = a.in; }   ... }; 

    To construct wrappers from raw pointers you could add an overloaded function, more or less like this:

    template<..., class P> P make_ptr(Base<...> *t);  template<...> Ptr<...> make_ptr< ..., Ptr<...> >(Base<...> *t) {   return Ptr(t); }  template<...> ConstPtr<...> make_ptr< ..., ConstPtr<...> >(const Base<...> *t) {   return ConstPtr(t) } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 101k
  • Answers 101k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Cross Site Scripting can be easily taken care of with… May 11, 2026 at 8:03 pm
  • Editorial Team
    Editorial Team added an answer At its core, the question boils down to this: what… May 11, 2026 at 8:03 pm
  • Editorial Team
    Editorial Team added an answer What if ffmpeg fails and throws and error? Right now… May 11, 2026 at 8:03 pm

Related Questions

I have implemented the MOD 10 check digit algorithm using SQL, for the US
When I launch an SWT application (via an Eclipse launch profile), I receive the
I have the following Rails link generating code (I have removed potentially 'industry secret'
The current declaration of SendMessage over at PInvoke.net is: [DllImport(user32.dll, CharSet = CharSet.Auto, SetLastError

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.