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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:39:31+00:00 2026-05-29T07:39:31+00:00

I was reading about const_cast operator in c++ 1.First weird thing thing i can’t

  • 0

I was reading about const_cast operator in c++

1.First weird thing thing i can’t understand is

const_cast operator syntax i.e.

-const_cast–<–Type–>–(–expression–)——————–><

what i have understand about this syntax is that it helps to cast away constness of anexpressionof type Type .But consider this code

class  ConstTest {   

private:
    int year;
public:
    ConstTest() : year(2007) {}
    void  printYear() const;
};

int main() {
    ConstTest c;
    c.printYear();
    return  0;
}

void ConstTest::printYear() const {
    ConstTest  *c  = const_cast<ConstTest*>(this);
    c->year  = 42;
    std::cout  <<  "This  is the  year "  << year  << std::endl;
}

Here in line ConstTest *c = const_cast<ConstTest*>(this), I think that the const of this pointer should be cast away, but the output shows that it is the object which this refers to that loses its const-ness.

I feel that the code should have been ConstTest *c = const_cast<ConstTest>(*this), but this produces an error. I know i am wrong at many interpretations. Please correct them all.

2.my second problem is the statement given below

The result of a const_cast expression is an rvalue unless Type is a reference type. In this case, the result is an lvalue.

Why is this so, and why it is not true in case of pointers?

  • 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-29T07:39:32+00:00Added an answer on May 29, 2026 at 7:39 am

    it helps to cast away constness of an expression of type Type

    No, Type is the type of the result, not the type of the operand.

    What i think is const of this pointer should be casted away

    this has type const ConstTest*. const_cast<ConstTest*>(this) has type ConstTest*. That’s what “casting away const” from a pointer-to-const means.

    I feel code should have been ConstTest *c =
    const_cast<ConstTest>(*this)

    The result of const_cast<T> has type T, that’s how it’s defined. Maybe you would have defined it differently, but tough luck, you don’t get a ConstTest* by writing const_cast<ConstTest>, you get it by writing const_cast<ConstTest*>. Your preferred syntax is not available.

    You can either do ConstTest &c = const_cast<ConstTest&>(*this) or ConstTest *c = const_cast<ConstTest*>(this), so pick your favorite.

    The result of a const_cast expression is an rvalue unless Type is a
    reference type. In this case, the result is an lvalue.

    why so and why it is not true in case of pointers?

    It is true of pointers. ConstTest* is not a reference type, and the result of const_cast<ConstTest*>(this) is an rvalue. You then assign that value to the variable c.

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

Sidebar

Related Questions

i have been reading about a function that can overwrite its return address. void
Reading about the G.729 codec , I found this interesting tidbit about Comfort Noise
Reading about Kohana templates and saw something I've never seen before: $this->template->title = __('Welcome
Reading about Django, I saw this: http://docs.djangoproject.com/en/1.1/ref/contrib/admin/#ref-contrib-admin - the fancy simple to use admin
When reading about SQL Injection and XSS i was wondering if you guys have
I've recent been reading about immutable strings Why can't strings be mutable in Java
I was reading a little about ARC and I saw this: @interface Address :
I am reading a book about C++ and more precisely about the operator overloading.
I was reading about this algorithm... And I coded a class to compress, I
After reading alot about VTables, I still have one unanswered question. Given the next

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.