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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:32:52+00:00 2026-05-24T01:32:52+00:00

In this post , a brave wants (in C++) to downcast a object of

  • 0

In this post, a brave wants (in C++) to downcast a object of type Base to a Derived type. Assuming that the Derived type has no more attributes than Base, it can make sense if you’re jealous of the extra methods that the Derived class provides.

Are there programming languages that allow such a thing?

  • 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-24T01:32:52+00:00Added an answer on May 24, 2026 at 1:32 am

    Actually, this is something that is done without problem in Common Lisp, and in other Lisp dialects where CLOS (Common Lis Object System) was ported. You use the change-class generic function for that.

    CLOS works with multiple dispatch methods, so a method is not tied to a class or object, it’s just a function that is chosen in a group of similar functions WRT to the types (or identities) of its arguments. When using change-class, you can give arguments as if you were creating a new instance, and data already stored in the object will remain. Here is a little session that shows how it works:

    CL-USER> (defclass base ()
           ((name :initarg :name)))
    #<STANDARD-CLASS BASE>
    CL-USER> (defclass derived (base)
           ((age :initarg :age :initform 0)))
    #<STANDARD-CLASS DERIVED>
    CL-USER> (defvar foo (make-instance 'base :name "John Doe"))
    FOO
    CL-USER> (change-class foo 'derived :age 27)
    #<DERIVED {100338F2D1}>
    CL-USER> (with-slots (name age) foo
           (list name age))
    ("John Doe" 27)
    CL-USER> (defvar bar (make-instance 'base :name "Baby Joe"))
    BAR
    CL-USER> (change-class bar 'derived)
    #<DERIVED {10036CF6E1}>
    CL-USER> (with-slots (name age) bar
           (list name age))
    ("Baby Joe" 0)
    CL-USER> 
    

    If this default behaviour is not enough, you may define a method on update-instance-for-different-class.

    So yeah, there are programming languages that allow such a thing!

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

Sidebar

Related Questions

This post identifies a feature that I would like to disable. Current numpy behavior:
This post is intended to be less of a question and more a confirmation
This post says that you need a 64 bit system for MongoDB if your
This post is an continuation of this post I have DlUser Class each object
This post talks about the problem that jQuery tabs is still having with the
This post is similar to: this I have a console app that expects both
This post says that it is possible to turn off the CLR flag for
This post has the comment if you need to call the method multiple times,
This post and this post says that with Visual Studio, the run time library
This post has the following code. Process p = new Process(); StringBuilder sb =

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.