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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:12:28+00:00 2026-06-04T01:12:28+00:00

Here is the code that i have class A { public: void Func1() const;

  • 0

Here is the code that i have

class A
{
public:
    void Func1() const;
};

class B
{
public:
    A* a;
    void Func2() const
    {
        // do something with 'a'
    }
};

void A::Func1() const
{
    B b;
    b.a = this;
    b.Func2();
}

Now obviously this is giving me an error in the line, because I’m trying to convert from const to non-const.

b.a = this;

Is there any way to call Func2 without having to cast away the constness of this. Since Func2 is a const function anyways, it will not change this.

  • 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-06-04T01:12:30+00:00Added an answer on June 4, 2026 at 1:12 am

    If class B is always going to work with *a as a const object then as others have said all it takes is to simply change the declaration to

    public: const A* a
    

    At this point I should mention that the constness of B::Func2 is a red herring because it has absolutely no relation to the constness of B::a. That B::Func2 is const means that it’s not allowed to change the value of a; however, it is allowed to dereference a and mutate the resulting object.

    Now, if class B has both const and non-const operations with respect to *a then your class design needs to change. It would be much better if you switched class B to use a const A* a as above, and added another class D : public B that encapsulates all the mutating operations. In addition, a should be hidden behind a property setter; this allows you to do things like

    class B {
        const A* a;
    public:
        void setA(const A* a) { this->a = a; }
        void Func2() const {}
    };
    
    class D : public B {
        A* a;
    public:
        using B::setA;
        void setA(A* a) { 
            this->a = a;
            static_cast<B*>(this)->setA(const_cast<const A*>(a));
        }
        void Func3() { /* do something to D::a */ }
    };
    

    With this scheme both B and D keep independent, suitably typed pointers to the object to be accessed. If setA is called on a B, or on a D with a const A* parameter then only B::a is set. If setA is called on a D with an A*, then both B::a and D::a are properly set. This has become possible because by abstracting the member behind a setter you can then overload the setter on the constness of its parameter.

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

Sidebar

Related Questions

I have no idea what this means. But here is the code that it
I have this html code that i want to edit with jQuery. Here is
Here is the code that I have for my cell of my UITableView: forKeys:[NSArray
Here's the c# code that I have: private double get806Fees (Loan loan) { Loan.Fee.Items
enter code here I have a ListView that have a different Layout XML for
Here is the piece of code that I have used for Java 5.0 TreeSet<Integer>
I have the following code here that won't run on ARC since it combines
I have a piece of code here that does not work despite me using
So I have some PHP code that looks like: $message = 'Here is the
I have some code that is using SyncEnumerator. As you can see here ,

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.