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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:44:22+00:00 2026-05-20T19:44:22+00:00

How can i cast to a derived class? The below approaches all give the

  • 0

How can i cast to a derived class? The below approaches all give the following error:

Cannot convert from BaseType to DerivedType. No constructor could take
the source type, or constructor overload resolution was ambiguous.

BaseType m_baseType;

DerivedType m_derivedType = m_baseType; // gives same error

DerivedType m_derivedType = (DerivedType)m_baseType; // gives same error

DerivedType * m_derivedType = (DerivedType*) & m_baseType; // gives same error
  • 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-20T19:44:23+00:00Added an answer on May 20, 2026 at 7:44 pm

    Think like this:

    class Animal { /* Some virtual members */ };
    class Dog: public Animal {};
    class Cat: public Animal {};
    
    
    Dog     dog;
    Cat     cat;
    Animal& AnimalRef1 = dog;  // Notice no cast required. (Dogs and cats are animals).
    Animal& AnimalRef2 = cat;
    Animal* AnimalPtr1 = &dog;
    Animal* AnimlaPtr2 = &cat;
    
    Cat&    catRef1 = dynamic_cast<Cat&>(AnimalRef1);  // Throws an exception  AnimalRef1 is a dog
    Cat*    catPtr1 = dynamic_cast<Cat*>(AnimalPtr1);  // Returns NULL         AnimalPtr1 is a dog
    Cat&    catRef2 = dynamic_cast<Cat&>(AnimalRef2);  // Works
    Cat*    catPtr2 = dynamic_cast<Cat*>(AnimalPtr2);  // Works
    
    // This on the other hand makes no sense
    // An animal object is not a cat. Therefore it can not be treated like a Cat.
    Animal  a;
    Cat&    catRef1 = dynamic_cast<Cat&>(a);    // Throws an exception  Its not a CAT
    Cat*    catPtr1 = dynamic_cast<Cat*>(&a);   // Returns NULL         Its not a CAT.
    

    Now looking back at your first statement:

    Animal   animal = cat;    // This works. But it slices the cat part out and just
                              // assigns the animal part of the object.
    Cat      bigCat = animal; // Makes no sense.
                              // An animal is not a cat!!!!!
    Dog      bigDog = bigCat; // A cat is not a dog !!!!
    

    You should very rarely ever need to use dynamic cast.
    This is why we have virtual methods:

    void makeNoise(Animal& animal)
    {
         animal.DoNoiseMake();
    }
    
    Dog    dog;
    Cat    cat;
    Duck   duck;
    Chicken chicken;
    
    makeNoise(dog);
    makeNoise(cat);
    makeNoise(duck);
    makeNoise(chicken);
    

    The only reason I can think of is if you stored your object in a base class container:

    std::vector<Animal*>  barnYard;
    barnYard.push_back(&dog);
    barnYard.push_back(&cat);
    barnYard.push_back(&duck);
    barnYard.push_back(&chicken);
    
    Dog*  dog = dynamic_cast<Dog*>(barnYard[1]); // Note: NULL as this was the cat.
    

    But if you need to cast particular objects back to Dogs then there is a fundamental problem in your design. You should be accessing properties via the virtual methods.

    barnYard[1]->DoNoiseMake();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why can't I cast a base class instance to a derived class? For example,
The pointer of derived class returned by new can be type cast to the
How can I cast from a String to a long in a Scala play
For example the following cast can be found littered throughout the MSDN documentation: (LPTSTR)&lpMsgBuf
I understand that static_cast can convert between base and derived and between derived and
I am trying to create a base class where I can inherit from it
I have 3 class which derived from each other: class Basic{ ... } class
I'm trying to check if an object can cast to a certain type using
Can you cast a List<int> to List<string> somehow? I know I could loop through
How can I cast long to HWND (C++ visual studio 8)? Long lWindowHandler; HWND

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.