Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There is an issue with your requirements. But first let's tackle the fixed size issue, it's called <code>std::tr1::array&lt;class T, size_t N&gt;</code> (if you know the size at compile time).</p> <p>If you don't know it at compile time, you can still use some proxy class over a vector.</p> <pre><code>template &lt;class T&gt; class MyVector { public: explicit MyVector(size_t const n, T const&amp; t = T()): mVector(n,t) {} // Declare the methods you want here // and just forward to mVector most of the time ;) private: std::vector&lt;T&gt; mVector; }; </code></pre> <p>However, what is the point of not being assignable if you are mutable ? There is nothing preventing the user to do the heavy work:</p> <pre><code>class Type { public: int a() const { return a; } void a(int i) { a = i; } int b() const { return b; } void b(int i) { b = i; } private: Type&amp; operator=(Type const&amp;); int a, b; }; </code></pre> <p>Nothing prevents me from doing:</p> <pre><code>void assign(Type&amp; lhs, Type const&amp; rhs) { lhs.a(rhs.a()); lhs.b(rhs.b()); } </code></pre> <p>I just want to hit you on the head for complicating my life...</p> <p>Perhaps could you describe more precisely what you want to do, do you wish to restrict the subset of possible operations on your class (some variables should not be possible to modify, but other could) ?</p> <p>In this case, you could once again use a Proxy class</p> <pre><code>class Proxy { public: // WARN: syntax is screwed, but `vector` requires a model // of the Assignable concept so this operation NEED be defined... Proxy&amp; operator=(Proxy const&amp; rhs) { mType.a = rhs.mType.a; // mType.b is unchanged return *this; } int a() const { return mType.a(); } void a(int i) { mType.a(i); } int b() const { return mType.b(); } private: Type mType; }; </code></pre> <p>There is not much you cannot do with suitable proxies. That's perhaps the most useful pattern I have ever seen.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload