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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:27:51+00:00 2026-05-29T12:27:51+00:00

I’m having some trouble with a template struct. template<typename T> struct A{ const int

  • 0

I’m having some trouble with a template struct.

template<typename T>
struct A{
    const int v1;
    T* v2;
};

My purpose is to make v1 always not editable, while v2 should be editable if I use T and not editable if I use const T as type.

If I create constructor to initialize A, the struct becomes:

template<typename T>
struct A{
    const int v1;
    T* v2;
    A() : v1(-1) { v2=NULL; }
    A(int value) : v1(value) { v2=NULL; }
};

But then g++ says that I need a specific assignment operator:

error: non-static const member ‘const int A::v1’, can’t use default assignment operator

But my assignment operator should also allow editing of v1. The only thing I’d like to avoid is an edit from the outside, something like:

A a;
a.v1=10;

Is there any way to implement this (without creating getter/setter or using a pointer to a new A(int) with the desired value)?

What if I declare v1 as a const int * ? It could refer someway to some value, but it cannot edit it.

  • 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-29T12:27:51+00:00Added an answer on May 29, 2026 at 12:27 pm

    Here’s a way to “expose” a public, read-only data member that is modifiable by the class’s own member functions (including assignment):

    template <typename T>
    class Helper {
        friend class A;
        T *ptr;
        Helper &operator=(const Helper &rhs) = default; // in C++11
        Helper &operator=(const Helper &rhs) { ptr = rhs.ptr; } // in C++03
      public:
        Helper(T *ptr) : ptr(ptr) {}
        operator const int &() const { return *ptr; }
    };
    
    class A {
        int v1_;
      public:
        Helper<int> v1;
        A() : v1(&v1_) {} // although `A` should have a constructor that sets `v1_`
        A(const A &rhs) { v1_ = rhs.v1_; v1 = Helper<int>(&v1_); }
        A &operator=(const A &rhs) { v1_ = rhs.v1_; v1 = Helper<int>(&v1_); }
    };
    

    Now anyone outside the class A can use v1, but the only thing they can use it for is to get a const int& reference to v1_.

    It is far easier just to give A a getter function that returns const int &, but if you really want the data member syntax then this provides it…

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have some data like this: 1 2 3 4 5 9 2 6

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.