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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:15:06+00:00 2026-06-10T17:15:06+00:00

Hi I am trying to make some public member variables read only. I know

  • 0

Hi I am trying to make some public member variables read only. I know I could do something like:

private: int _x;
public: const int& x;
Constructor(): x(_x) {}

I’m looking for something more manageable and easier to read. I found several templates on the internet all of which do similar to what is described as a proxy class in this SO answer.

I’m trying to adapt that proxy class so that I can put the template in an include and write something like this for each variable in a class that I need read only variables in:

public: proxy<int, myClass> num;

Even easier would be if I didn’t have to say the class name every time but I don’t know a way to get around that unless the class name is identified in the template.

I tried this in Visual Studio 2010 but it doesn’t work, does anyone know why?

template <class T, class C>
class proxy {
    friend class C;
private:
    T data;
    T operator=(const T& arg) { data = arg; return data; }
public:
    operator const T&() const { return data; }
};

class myClass {
public:
    proxy<int,myClass> x;

public:
    void f(int i) {
        x = i;
    }
};

Thanks

Edit- Someone asked for what I mean by doesn’t work:

int main(int argc, char **argv)
{
    myClass test;
    test.f(12);
    cout << test.x << endl;
    return 0;
}

returns:

b.cpp(122) : error C2649: 'typename' : is not a 'class'
        b.cpp(128) : see reference to class template instantiation 'proxy<T,C>'
being compiled
b.cpp(136) : error C2248: 'proxy<T,C>::operator =' : cannot access private membe
r declared in class 'proxy<T,C>'
        with
        [
            T=int,
            C=myClass
        ]
        b.cpp(125) : see declaration of 'proxy<T,C>::operator ='
        with
        [
            T=int,
            C=myClass
        ]
  • 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-10T17:15:07+00:00Added an answer on June 10, 2026 at 5:15 pm

    Change this:

    template <class T, class C>
    class proxy {
      friend class C;
    

    to this:

    template <class T, class C>
    class proxy {
       friend C;
    

    Because C is a template parameter, it’s not guaranteed that C will necessarily be a class type.

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

Sidebar

Related Questions

I'm trying to make some input fields that I would like to work like
I am trying to specialize some utility code on const member functions, but have
Trying to make some experiments with Spring MVC and Spring Security: @Controller @RequestMapping(/auth) public
I am trying to make some project in which i want a text box
I am trying to make some edits to a site for a client of
I'm trying to make some common scopes for models to be sharing, so i
I'm trying to make some text bold using HTML, but I'm struggling to get
I'm trying to make some social share buttons at my site, but the urls
I'm trying to make some communication for my PIC devices and I use UDP
I'm trying to make some kind of top for some game stadistics Table it's

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.