Alf P. Steinbach wrote:
* Steven T. Hatton: As far as I know, there is no way to provide a default value for the
argument to the constructor A::A(char (&array)[16]) in this example.
Correct?
char z[16] = {};
Too bad I can't have that as the default operation performed on a POD class
when it's constructed. AFAIK, I can do this.
struct C { int _a; int _b; };
C c = {};
To reliably zero-initialize all the members of a POD class. But I don't see
a way to use that for the default constructor. Writing out all the members
in the initialization list can be tedious.
struct A{
A(char (&array)[16] ){
A( char (&array)[16] = z )
std::copy(&arra y[0],&array[16],&_array[0]);
You know, names that differ only in an underscore are not visually
distinctive.
Actually, I was being sloppy. Typically I will do this:
struct A{
A(char (&array_)[16] )
{std::copy(&arr ay_[0],&array[16]_,&_array[0]);}
char _array[16];
};
Which in this case doesn't help a lot because the syntax is so busy. In
general, however, since I frequently use _member for the member
declaration, and member_ for a parameter, I'm conditioned to look for them.
It works fairly well. The only symbol that really causes me much problem
is '#'.
}
char _array[16];
};
int main() {
char arr[16] = {0};
A a(arr);
std::cout<<a._a rray<<std::endl ;
}
int main() {
A a;
std::cout<<a._a rray<<std::endl ;
}
Interestingly, if the parameter is not const it won't accept a const char[]
for z, but as far as I can tell, z and A::_array are two distinct objects,
so z would never be modified as a result of that initialization. Typically
the parameter to the constructor should probably be const anyway. I also
found that I can make the default initializer a static member, which
satisfies my desire for proper containment.
This code show some behavior that was not obvious from looking at the
syntax.
#include <iostream>
#include <string>
std::string s("surprise!" );
class A {
static const char s_Z[16];
public:
A(const char (&array_)[16] = s_Z ) {
std::copy(&arra y_[0],&array_[16],&_array[0]);
}
void surprise() { std::copy(&s[0],&s[s.size()],&_array[0]); }
char _array[16];
};
const char A::s_Z[16] = {'1','2','3','4 ','5','6','7',' 8','9'
,'A','B','C','D ','E','F','\0'} ;
char z[16] = {};
struct B {
B( const char (&array_)[16] = z ) {
std::copy(&arra y_[0],&array_[16],&_array[0]);
}
char _array[16];
};
int main() {
char arr[16] = {'1','2','3','4 ','5','6','7',' 8','9'
,'a','b','c','d ','e','f','\0'} ;
A a(arr);
A aa;
std::cout<<a._a rray<<std::endl ;
a.surprise();
std::cout<<aa._ array<<std::end l;
std::cout<<a._a rray<<std::endl ;
std::cout<<z<<s td::endl;
B b;
std::copy(&s[0],&s[s.size()],&z[0]);
B bb;
std::cout<<b._a rray<<std::endl ;
std::cout<<bb._ array<<std::end l;
}
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell