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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:26:27+00:00 2026-05-19T22:26:27+00:00

Is it possible to implement a class template in such a way that one

  • 0

Is it possible to implement a class template in such a way that one object could be casted to another if their template arguments are related? Here is an exaple to show the idea (of course it will not compile):

struct Base {};
struct Derived : Base {};

template <typename T> class Foo {
    virtual ~Foo() {}
    virtual T* some_function() = 0;
};

Foo<Derived>* derived = ...;
Foo<Base>* base = derived;

The additional problem here is that Foo is an abstract class used as an interface containing functions returning T& and T*, so I can’t implement a template copy constructor.

I’m writing a universal Iterator class which can hold any STL iterator, and in addition to type erasure I’d like it to be polymorphic, i.e. I could write something like this:

std::list<Derived> l;
MyIterator<Base> it(l.begin());

UPD: That was my mistake, I didn’t actually need casting Foo* to Foo* to implement MyIterator, so I think the question is not actual anymore.

  • 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-19T22:26:28+00:00Added an answer on May 19, 2026 at 10:26 pm

    The template argument has nothing to do with the content of the object you are pointing to. There is no reason this should work. To illustrate

    struct Base { };
    struct Derived : Base {};
    
    template<typename T> struct A { int foo; };
    template<> struct A<Base> { int foo; int bar; };
    
    A<Derived> a;
    A<Base> *b = &a; // assume this would work
    b->bar = 0; // oops!
    

    You will eventually access integer bar that doesn’t really exist in a!


    OK, now that you provided some more information, it’s clear you want to do something completely different. Here is some starter:

    template<typename T>
    struct MyIterator : std::iterator<...> {
      MyIterator():ibase() { }
      template<typename U>
      MyIterator(U u):ibase(new Impl<U>(u)) { }
      MyIterator(MyIterator const& a):ibase(a.ibase->clone())
    
      MyIterator &operator=(MyIterator m) {
        m.ibase.swap(ibase);
        return *this;
      }
    
      MyIterator &operator++() { ibase->inc(); return *this; }
      MyIterator &operator--() { ibase->dec(); return *this; }
      T &operator*() { return ibase->deref(); }
      // ...
    
    private:
      struct IBase { 
        virtual ~IBase() { }
        virtual T &deref() = 0; 
        virtual void inc() = 0;
        virtual void dec() = 0;
        // ...
    
        virtual IBase *clone() = 0;
      };
      template<typename U>
      struct Impl : IBase { 
        Impl(U u):u(u) { }
        virtual T &deref() { return *u; }
        virtual void inc() { ++u; }
        virtual void dec() { --u; }
        virtual IBase *clone() { return new Impl(*this); }
        U u;
      };
    
      boost::scoped_ptr<IBase> ibase;
    };
    

    Then you can use it as

    MyIterator<Base> it(l.begin());
    ++it; 
    Base &b = *it;
    

    You may want to look into any_iterator. With a bit of luck, you can use that template for your purpose (I haven’t tested it).

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

Sidebar

Related Questions

In C++, is it possible to have a base plus derived class implement a
Is it possible to implement Bluetooth devices to provide conference rather than one-to-one operation.
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions
Is it possible to identify classes that implement IDisposable. I was hoping to do
I'm looking for suggestions on possible IPC mechanisms that I can implement in my
Given a class instance, is it possible to determine if it implements a particular
Is it possible to implement the Visitor Pattern respecting the Open/Closed Principle , but
Is it possible to implement autoboxing for your own classes? To illustrate my example,
Is it possible to implement batching of multiple stored procedure calls (doing updates/deletes) in
Is it possible to have an anonymous type implement an interface? I've got a

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.