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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:38:41+00:00 2026-06-01T02:38:41+00:00

EDIT: I realized that this code compiles and works: #include <iostream> template<class Something> class

  • 0

EDIT:

I realized that this code compiles and works:

#include <iostream>

template<class Something> class Base {
public:
    Base() {

    }

    virtual ~Base() {
    }

    virtual void method() = 0;
};

class Derived: public Base<std::string> {
public:
    Derived() {

    }

    virtual ~Derived() {

    }

    void method() {
    }
};

template<class Something> class TemplatedClass {
public:
    void method(Base<Something>* base) {
    }
};

class SomeClass: public TemplatedClass<std::string> {
};

int main(int argc, char **argv) {

    Derived* d = new Derived();

    Base<std::string>* b = d;
    Base<std::string>* b1 = dynamic_cast<Base<std::string>*>(d);
    Base<std::string>* b2 = static_cast<Base<std::string>*>(d);

    SomeClass sc;
    sc.method(b);
    sc.method(b1);
    sc.method(b2);
}

Then, I am doing a really big mistake preparing the skeleton or even trying to understand my own code. WHAT a SHAME!!! Sorry… This is a really bad question. It would be better to remove it…

** NO SENSE QUESTION AND CODE BELOW **
Given this:

template<class Something> class Base
{
    virtual void method() = 0;
};

class Derived: public Base<std::string>
{
    void method() { ... };
};

I want to downcast a pointer of Derived to a pointer of Base. I tried to use dynamic_cast with no success:

...
Derived* d = new Derived();
Base* b = dynamic_cast<Base*>(d);
...

I always got errors during execution.

I don’t know if I need downcasting,. I have this situation:

template<class Something> class TemplatedClass
{
    void method(Base<Something>* base) { ... };
};

class SomeClass : public TemplatedClass<std::string> { ... };

And I want to call (EDITED*):

Derived* d = new Derived();

SomeClass sc;
sc.method(d);

But it gives me an conversion error.

What does I need? Downcasting or what?

  • 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-01T02:38:42+00:00Added an answer on June 1, 2026 at 2:38 am

    Presumably your test actually looked like this?

    Base<std::string>* b = dynamic_cast<Base<std::string> *>(d); 
    

    i.e. it has to be a cast to the instantiated template, not Base without a type argument.

    Wait a second – you’re doing derived to base!

    Base<std::string>* b = d; 
    

    That should be fine!

    Your second example doesn’t look right – in particular:

    SomeClass sc();
    

    That should be:

    SomeClass sc;
    

    Can you post the real code and the exact error message?

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

Sidebar

Related Questions

EDIT: I just realized that this question may be better suited to ServerFault. Instead
Consider this code: #include <iostream> using namespace std; typedef int array[12]; array sample; array
I have a class Foo defined like this: class Foo { public: Foo(int num);
EDIT I just realized that I must have had a massive brain fart while
Here is some CRTP based template code that I used to try and resolve
I just realized that in Eclipse one cannot collapse code blocks like {} .
EDIT: I have realized the source of my problem. I only have count information
EDIT: This post was originally specific to ASP.NET, but after thinking about it I'm
EDIT: There's now a doc page on this so this question is irrelevant, also
I realized, when just asking a question, I don't understand all the components that

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.