c++ - default value at declaration time -


Is there any way to declare an object's value (I int / Double ) At the announcement time, inside a class announcement? Something like this:

  class MyClass {MyInt & lt; 1 & gt; My_int; };   

Is there a library in it?

C + +11, you can provide default initiator within the definition of a class:

  struct foo {int a = 11; Fu () = default; // still a trivial class :-)};   

In the past, you have to use Constructor Starter:

  struct bar {int b; Bar (): B (11) {} / Non-trivial constructor :-(};   

Usage:

  Fu x; (Xa == 11 & amp; yb == 11);   

Perhaps you find the solution for @missletters useful:

  template & Lt; typename t, t inteveal & gt; straight initial type {typedef T type; static type const initial_value = initVal; // ban type and operator (return); {return x;} type const and operator () const {return X;} initial type (): x (intall) {} private: type x;};   

You can add InitializedType & lt; int, 11> n; to get something, it looks like a int , but value 11 Starts with.

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -