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

  • Home
  • SEARCH
  • 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 7872221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:08:35+00:00 2026-06-03T02:08:35+00:00

Is it possible to override (C-style) casts in C++? Suppose I have the code

  • 0

Is it possible to override (C-style) casts in C++?

Suppose I have the code

double x = 42;
int k = (int)x;

Can I make the cast in the second line execute some code I wrote? Something like

// I don't know C++
// I have no idea if this has more syntax errors than words
operator (int)(double) {
    std::cout << "casting from double to int" << std::endl;
}

The reason I ask is because of the question “Is there any way to get gcc or clang to warn on explicit casts?” and my suggestion there.

  • 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-03T02:08:36+00:00Added an answer on June 3, 2026 at 2:08 am

    § 12.3.1/1 “Type conversions of class objects can be specified by constructors and by conversion functions. These conversions are called user-defined conversions and are used for implicit type conversions (Clause 4), for initialization (8.5), and for explicit type conversions (5.4, 5.2.9).”

    Yes, we can make conversions, but only if one or both sides is a user-defined type, so we can’t make one for double to int.

    struct demostruct {
        demostruct(int x) :data(x) {} //used for conversions from int to demostruct
        operator int() {return data;} //used for conversions from demostruct to int
        int data;
    };
    
    int main(int argc, char** argv) {
        demostruct ds = argc; //conversion from int to demostruct
        return ds; //conversion from demostruct to int
    }
    

    As Robᵩ pointed out, you can add the explicit keyword to either of those conversion functions, which requires the user to explicitly cast them with a (demostruct)argc or (int)ds like in your code, instead of having them implicitly convert. If you convert to and from the same type, it’s usually best to have one or both as explicit, otherwise you might get compilation errors.

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

Sidebar

Related Questions

Is it possible to make a css declaration override a style set in the
How is it possible to override styles specified in another style sheet? I do
Is it possible to override a generisized function as illustrated in the code snippet
How can I make as perfect a subclass of dict as possible? The end
Is it possible to change default EventKitUI style? I'd like to have different background
Is it possible to override a RESTful path? For instance, I have photo_path(photo) which
Is it possible to override reflection functionality ?
Is it possible to override the standard layouts that are provided with Android? Is
Is it possible to override system property (timezone etc.) in Java using predefined properties
It doesn't seem to possible to override a plugin execution's goal definition. Let say

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.