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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:29:05+00:00 2026-06-06T21:29:05+00:00

I have an object of a base class which is actually pointing to a

  • 0

I have an object of a base class which is actually pointing to a derived class like this

Base *b = new Derived()

What I want to know, is it possible to pass the base class pointer by reference to some function which can cast the object back to Derived class. Something like this

Dummy_cast_fn(&b) // casts b to derived class

After calling Dummy_cast_fn, b should have a full copy of Derived class (no slicing).

Edit
I dont understand the part that there is no slicing since pointers are used. My problem is Derived class is returned from a function call to a shared library and I do not have access to .h file for the Derived. Only information I have is that Derived is based on Base class. I have access to Base.h so I can instantiate an object of Base but problem comes when I try to access the functions which are defined in Derived but not in Base. So I was wondering if I can typecast the Base to Derived type, then I will be able to access the function defined in Derived and not in Base.

  • 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-06T21:29:06+00:00Added an answer on June 6, 2026 at 9:29 pm

    As long as b is a pointer or reference to a Derived, you can always:

    1. Down cast it to a Derived.
    2. Treat it as a Base as long as you’re using b.

    Namely, what determines what you can do with b is its static type, in this case Base. However, since it actually points on a Derived, you can always downcast it. Yet, to use it as a Derived you must have a variable whose type is Derived as well.

    So, if the purpose of Dummy_cast_fn is just to fix something in b – it’s useless. If an object is sliced, nothing can fix it. But in your case, there’s no slicing, since you’re using pointers.

    Edit according to the question’s edit:

    First, you’re Derived object is not sliced. Let’s get that off the table. You have a pointer to a complete Derived (assuming that’s what you’ve been passed), but you only have access to its Base part when using the Base pointer. Now, you say you don’t have the definition of Derived. This means you won’t be able to downcast to that type, because the compiler doesn’t know how it’s defined. No casting will work here. There’s no legal C++ way you can call that sum function if you don’t have Derived‘s definition.

    I do wonder why the author of Derived provided you its documentation without providing its definition. With this kind of polymorphism, the provider usually lets the user have some “interface”, leaving the actual type as an internal implementation detail. If you can’t use Derived because you don’t have its definition, there’s no point in letting you have its documentation.

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

Sidebar

Related Questions

I want to have a base class which dictates the alignment of the objects
I have a class derived from base class which has a private field. How
I may be going about this backwards... I have a class which is like
(edit) more info. First notice new virtual. This class inherits a base class which
Okay I have a series of objects based on a base class which are
Say I have a couple of basic objects like so: [Serializable] public class Base
I have created an abstract base class Animal which has public virtual abstract method
I have an Abstract class called Function which has 2 inheriting classes (actually 6,
I have an abstract base class along the lines of: class MyAbstractClass(object): __metaclass__ =
I have a stateless, abstract base class from which various concrete classes inherit. Some

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.