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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:38:35+00:00 2026-05-16T02:38:35+00:00

I usually use C type casting in C/C++ code. My question is, does adding

  • 0

I usually use C type casting in C/C++ code. My question is, does adding the “const” keyword in the casting type mean anything to the result?

For example, I can think up several scenarios:

const my_struct *func1()
{
   my_struct *my_ptr = new my_struct;

   // modify member variables

   return (const my_struct *)my_ptr;
   // return my_instance;
}

In this one, the function constructs a new instance of a struct, and casting it to to a constant pointer, therefore caller will not be able to further modify its internal state except deleting it. Is the “const” casting required, recommended, or simply unnecessary, since either return statement works.

In this one, my_base is the base class of my_derive.

const my_base *func2(const my_derive *my_ptr)
{
    return (const my_base *)my_ptr;
    // return (my_base *)my_ptr;
}

Since my_ptr is already a const pointer, would casting it with (my_base *) involve a const_cast for removing const and another implicit const_cast when returning?

Is there any reason to add “const” to an integer function argument, since changing it never affect state outside the function?

void func3(const int i)
{
    // i = 0; is not allowed, but why, as it is harmless?
}

How about adding “const” when casting an integer? I think this should resemble func2().

void func4(short i)
{
    const unsigned int j = (const unsigned int) i;
    // const unsigned int j = (unsigned int) i;
}

Correct me if I’m wrong. Considering type casting might be an FAQ, I’m not sure if this one duplicates with anything else. Thanks!

  • 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-16T02:38:36+00:00Added an answer on May 16, 2026 at 2:38 am

    Adding the const keyword in the casting type means that the result will be constant. The following will not compile in C++ (in C it has no effect):

    int* x = (const int*)malloc(10); // cannot convert from 'const int *' to 'int *'
    

    You really shouldn’t use C type casting in your C++ code. It is not safe and should be used only for compatibility with the legacy C code. You should use C++ casts instead.

    In cases as in func3 usually const qualifier is not used. There’s no big reason to add const qualifier to function argument if it has not pointer or not reference type. Consider the following:

    void func3(      TYPE i);  // no reason to use `const`
    void func3(const TYPE& i); // use `const`, so as not to accidentally change `i`
    

    When you assign lvalue to rvalue, as in func4, there’s no need to explicitly specify the const qualifier in the cast expression. Lvalue-to-rvalue conversion will be performed implicitly according to the C++ Standard 4.1.

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

Sidebar

Related Questions

Usually i use this type of data in freemarker: (root) | +- website =
How do I call Class.forName() when the result is a generic type? Usually I
Possible Duplicate: Python “is” operator behaves unexpectedly with integers Usually I use the type(x)
I usually use a boolean 'firstTime' like this: in C++: bool firsTime = true;
I usually use Visual Studio Team System 2008 Source Control Explorer with TFS, but
I usually use the following pipeline to grep for a particular search string and
I usually use this line to import file from out of the current folder
I usually use pointers in the following manner char *ptr = malloc( sizeof(char) *
I usually use a TabControl and somehow hide the tabs and navigate through them.
I usually use ORM instead of SQL and I am slightly out of touch

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.