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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T14:53:45+00:00 2026-05-17T14:53:45+00:00

all: this is quoted from Effective C++ 3rd editiion const_cast is typically used to

  • 0

all:
this is quoted from Effective C++ 3rd editiion

const_cast is typically used to cast away the constness of objects. It is the only C++-style cast that can do this.

My question is can const_cast add constness to a non-const object?
Actually i wrote a small programme trying to approve my thought.

class ConstTest
{
 public:

 void test() {
    printf("calling non-const version test const function \n");
}

 void test() const{
    printf("calling const version test const function \n");

} 

};
 int main(int argc,char* argv){
 ConstTest ct;
 const ConstTest cct;
 cct.test();
 const_cast<const ConstTest&>(ct).test();//it is wrong to write this statement without the '&',why

}

Omitting the ‘&’ incurs error below:

error C2440: ‘const_cast’ : cannot convert from ‘ConstTest’ to ‘const ConstTest’

It shows that const_cast can add constness,but seems like you have to cast to a object reference, what is the magic of this reference ?

  • 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-17T14:53:45+00:00Added an answer on May 17, 2026 at 2:53 pm

    You don’t need const_cast to add constness:

    class C;
    C c;
    C const& const_c = c;
    

    The other way around needs a const_cast though

    const C const_c;
    C& c = const_cast<C&>(const_c);
    

    but behavior is undefined if you try to use non-const operations on c.

    By the way, if you don’t use a reference, a copy of the object is to be made:

    C d = const_c; // Copies const_c
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

([\'])(?:\\\\?+.)*?\\1 I came up to this regexp to match all quoted strings.. It seems
How can I get all this to not only output on the screen, but
I was trying to get all quoted ( or ') substrings from a string
Does .net have the equivilent of Delphi's QuotedStr function. This replaces all quotes with
Hi all this is my xml file with xsd defined as internal to it
hello all this seems to be my problem I have a table in mysql
First of all this is my markup: <div class=first></div> <div class=second></div> <div class=second></div> <div
first of all: this is not the same as this . The ModelBackend has
first of all this is my third question about web services here and i
New to all this so forgive my ignorance. I am trying to figure out

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.