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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:43:43+00:00 2026-06-09T20:43:43+00:00

I have a class with const members, and one constructor which calls another constructor

  • 0

I have a class with const members, and one constructor which calls another constructor with extra values filled in. Normally I could use a colon initializer for this, but the function is complex (printf/sprintf-like) and requires me to use a variable on the stack, so I have to do this in the body of the constructor and use assign *this to the new object. But of course this is invalid, because my member variables are const.

class A
{
public:
    A(int b) : b(b), c(0), d(0) // required because const
    {
        int newC = 0;
        int newD = 0;
        myfunc(b, &newC, &newD);
        *this = A(b, newC, newD); // invalid because members are const

        // "cannot define the implicit default assignment operator for 'A', because non-static const member 'b' can't use default assignment operator"
        // or, sometimes,
        // "error: overload resolution selected implicitly-deleted copy assignment operator"
    };
    A(int b, int c, int d) : b(b), c(c), d(d) { };

    const int b;
    const int c;
    const int d;
};

A a(0);

(I haven’t explicitly deleted the assignment operator.) I declared the members const because I would like them to be public, but not mutable.

Is there some canonical way of solving this problem without using scary casts and force-overriding the members’ constness? What’s the best solution here?

  • 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-09T20:43:44+00:00Added an answer on June 9, 2026 at 8:43 pm

    You can add a parameters class and use either C++11 constructor delegation or a base class:

    struct parameters {
        int b; int c; int d;
        parameters(int b): b(b), c(), d() {
            myfunc(b, &c, &d);
        }
    };
    
    // constructor delegation
    class A {
    public:
        A(int b): A(parameters(b)) { }
        A(parameters p): b(p.b), c(p.c), d(p.d) { }
    };
    
    // base/wrapper
    class ABase {
        ABase(parameters p): b(p.b), c(p.c), d(p.d) { }
    };
    
    class A: public ABase {
    public:
        A(int b): ABase(parameters(b)) { }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several members in my class which are const and can therefore only
Assume I have a Class Foo which has many internal variables, only one constructor
I have a class whose constructor takes a const reference to a string. This
I have a class CContainer that has some members CMemberX , CMemberY , which
I have two questions about static + const members and static classes. One: Why
I have class Fred { public: void inspect() const {}; void modify(){}; }; int
I have a class essentially: public class WindowEvent extends Event { public static const
I have a class template <typename Iterator, typename Value> class Foo { public: Foo(const
I have code that looks like this: class T {}; class container { const
I have the following code: template <class T> struct pointer { operator pointer<const T>()

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.