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 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

Ask A Question

Stats

  • Questions 495k
  • Answers 495k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer $8.5.1/7 - "If there are fewer initializers in the list… May 16, 2026 at 11:30 am
  • Editorial Team
    Editorial Team added an answer In some cases, it is possible to change your IP… May 16, 2026 at 11:30 am
  • Editorial Team
    Editorial Team added an answer I would split your task into a) identify words and… May 16, 2026 at 11:30 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Usually i use this type of data in freemarker: (root) | +- website =
Possible Duplicate: Casting: (NewType) vs. Object as NewType Say for example I have a
I usually use C++ stdlib map whenever I need to store some data associated
I usually use Geany or Hi-Tide under Debian (GNU/Linux) for firmware development, mainly C
I usually use ORM instead of SQL and I am slightly out of touch
I usually use expressions like this CommandArgument='<%# Container.DataItemIndex.ToString() %> ' But I could not
Possible Duplicate: Use of var keyword in C# Which declaration should be accepted ?
I usually avoid VB's built-in conversion functions (CStr, CDate, CBool, CInt, etc.) unless I
Sorry for what seems like a silly question: But I've never, ever worked with
Usually when my regex patterns look like this: http://www.microsoft.com/ Then i have to escape

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.