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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:31:36+00:00 2026-05-12T10:31:36+00:00

I have class Fred { public: void inspect() const {}; void modify(){}; }; int

  • 0

I have

class Fred 
{
public:
  void inspect() const {}; 
  void modify(){};
};

int main()
{
 const Fred x = Fred();
 Fred* p1;
 const Fred** q1 = reinterpret_cast<const Fred**>(&p1);
 *q1 = &x; 
 p1->inspect();
 p1->modify();
}

How would it be possible to do the
const Fred** q1 = &p1
via pointer-casting?

(I have just been reading that this might be possible)

Thank you for your answers. The const_cast works indeed for objects

#include <iostream>
#include <stdio.h>
using namespace std;

class Fred 
{
 int a;

public:
Fred(){};
Fred(int a_input)
{
 a = a_input;
};

void inspect() const 
{
 cout << "Inspect called"<< endl;
 cout << "Value is ";
 cout << a << endl;
}; 

void modify()
{
 cout << "Modify called" << endl;
 a++;
};

};

int main()
{
 const Fred x = Fred(7);
 const Fred* q1 = &x;
 Fred* p1 = const_cast<Fred*>(q1); 
 p1->inspect();
 p1->modify();
 p1->inspect();
 x.inspect();
 *p1 = Fred(10);
 p1->inspect();
}

gives

Inspect called
Value is 7
Modify called
Inspect called
Value is 8
Inspect called
Value is 8
Inspect called
Value is 10
Inspect called
Value is 10

However, for predefined types it does not work:

int main()
{
 const double a1 = 1.2;
 const double* b1 = &a1;
 cout << "a1 is " << (*b1) << endl;
 cout << "b1 is " << b1 << endl;
 double* c1 = const_cast<double*>(&a1);
 cout << "b1 is " << b1 << endl;
 cout << "c1 is " << c1 << endl;

 double* d1 = static_cast<double*>(static_cast<void*>(c1));
 cout << "d1 is " << d1 << endl;
 cout<< "*d1 is " << *d1 << endl;

 *d1=7.3;

 cout<< "*d1 is " << *d1 << endl;
 cout<< "*d1 address is "<< d1 << endl;
 cout << "a1 is " << a1 << endl;
 cout << "a1 address is" << &a1 << endl;
 cout<< "*d1 is " << *d1 << endl;
 cout<< "*d1 address is "<< d1 << endl;

 double f1=a1;
 printf("f1 is %f \n", f1);
}

is resulting in:

a1 is 1.2
b1 is 0xffbff208
b1 is 0xffbff208
c1 is 0xffbff208
d1 is 0xffbff208
*d1 is 1.2
*d1 is 7.3
*d1 address is 0xffbff208
a1 is 1.2
a1 address is0xffbff208
*d1 is 7.3
*d1 address is 0xffbff208
f1 is 1.200000 

Apparently the g++ compiler optimizes such that it replaces a1 by 1.2 whenever it finds it, so, even if its value on the stack has changed, it does not care.

(In my case I had problems with directly reading the *b1, *c1, so I had to do the double static cast – the reinterpret cast did not work).

Is it any way to really change a1, compiling “normally”, therefore not compiling without the optimization (so I overtake the optimization effect)?

  • 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-12T10:31:36+00:00Added an answer on May 12, 2026 at 10:31 am

    You want const_cast.

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

Sidebar

Ask A Question

Stats

  • Questions 181k
  • Answers 181k
  • 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 This to me is horrible part of LINQ-to-Entities. If I'm… May 12, 2026 at 4:17 pm
  • Editorial Team
    Editorial Team added an answer According to the documentation: To retrieve all rows from a… May 12, 2026 at 4:17 pm
  • Editorial Team
    Editorial Team added an answer I think you already have your answer - AddRange(ListViewItem[]). If… May 12, 2026 at 4:17 pm

Related Questions

I know that I can use implicit conversions with a class as follows but
If I have the following domain object: public class Customer { public virtual Guid
I'm new to Java, but have some OOP experience with ActionScript 3, so I'm
How can I mock a DataServiceQuery for unit testing purpose? Long Details follow: Imagine

Trending Tags

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

Top Members

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.