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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:39:48+00:00 2026-05-25T16:39:48+00:00

I find when writing functions (that use function overloading) that accept either a main

  • 0

I find when writing functions (that use function overloading) that accept either a main class or subclass argument, that often implicit upcasting will occur (the subclass is upcasted as the main class and the main class function called). I don’t want this implicit upcasting to occur as it means subtle bugs sneak in and cause problems later on down the line.

I have searched on google for information on this, but there is little coherent information I can make use of and only indirect references to it.

How do I disable, stop or prevent implicit upcasting (and even downcasting) from occurring?

(I can’t supply any example code as this is a general problem that occurs from time to time).

No, this isn’t to do with methods (I would have specified methods) but functions.

No example code, but pseudo idea:
void Function(BaseClass &A);
void Function(SubclassClass &B);

Function(ASubclass); //Implicit upcasting occurs, calls BaseClass instead

The above situation won’t happen conventionally (say that the SubclassClass function gets knocked out/erased), but Subclass will be upcast to the BaseClass for use with the BaseClass function, instead of, say, reporting an error or generating a warning – either would be helpful as I don’t want implicit upcasting to occur.

Please don’t confuse upcasting with non-virtual method calls.

  • 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-25T16:39:49+00:00Added an answer on May 25, 2026 at 4:39 pm
    class Base
    {    
    };
    
    class Derived1:public Base
    { 
    };
    
    class Derived2:private Base
    {  
    };
    
    void doSomething(Base *ptr)
    {
    }
    
    int main()
    {
      Derived1 *ptr1 = new Derived1;
      Derived2 *ptr2 = new Derived2;
    
      doSomething(ptr1);   //works fine
      doSomething(ptr2);   //Gives error
    
      return 0;
    };
    

    Upcasting:

    A Base class pointer can always point to a derived class object as long as the derived class is publically derived from the Base class. Eg: First Function Call.
    This upcasting happens implicitly.

    If the derivation is private this upcasting does not happen implicitly and compiler will issue an error.

    Though, using private inheritance is not the way to achieve this behavior. You should use private inheritance if it suits your design and it will implicitly gaurantee you that upcasting never happens implicitly.

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

Sidebar

Related Questions

I'm writing a function that I need to use either a TABLE variable for
I find that I frequently end up writing a function that I always call
I often find myself writing a property that is evaluated lazily. Something like: if
I'm writing an interactive function that I'd like to have remember the last argument
I'm working in a CakePHP Model class, writing a function that is supposed to
I find myself writing delegates occasionally for really simple functions (take no arguments and
I find myself writing file and directory utility functions all the time, and I
I find myself writing code that looks like this a lot: set<int> affected_items; while
I find that writing web apps and WinForm apps generally come out a lot
I am writing a function that well keep the user in lightbox images while

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.