help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

can const-arrays be used as class-members?


From: Piotr Sawuk
Subject: can const-arrays be used as class-members?
Date: 09 Jul 2007 00:11:14 GMT

class C {
  const int ar[3];
  public:
  C(int k) : ar({k,2*k,3*k}) {}
  C(const C& c) : ar(c.ar) {}
};

does not compile! none of those constructors did manage to initialize C::ar,
and leaving C::ar uninitialized is an error too. did I overlook something?
is there any C++ implementation of the good old const-sized C-arrays? I guess
something like this could be the solution:

template<typename T_, unsigned char size=256> struct array {
  T_ carray[size]; //public for compatibility with C-functions
  template<typename T__> array(const array<T__, size>& a);
  array(T_, ...);//initialize by iterating over all arguments
  T_& operator[](const unsigned char i) {return carray[i];}
  operator array<T_, size-1> (); //reinterpret_cast to smaller array...
  template<unsigned char s_>
  array<T_, size-s_> operator+ (s_); //the same but using the tail...
};

template<typename T_, unsigned short int size> struct array :
  array< array<T_>, 1+ size/256> {/*add implementation here*/};// :-)

and of course above "int ar[3]" would then need to be replaced by
"array<int,3> ar" and initialization would work without any {} around
the list of terms. but don't try this at home :-)...

does anyone know of similar implementations? how would g++ handle such
an array-class during optimization, would it be the same as T_[size]?
-- 
Better send the eMails to netscape.net, as to
evade useless burthening of my provider's /dev/null...

P


reply via email to

[Prev in Thread] Current Thread [Next in Thread]