Given this class:
void calculate( int*, int* );
struct A{
int p;
int q;
A() { calculate( &p, &q ); }
};
I want to make the members p
and q
constant:
struct A{
const int p;
const int q;
// constructor???
};
The question is, how to I initialize those values? I'd like to call calculate
, or maybe some kind of proxy function, from within the constructor, but how can I do it in a way to initialize both values at a time? One obvious idea would be a wrapper for the constructor:
A create_A(){
int p0;
int q0;
calculate( &p0, &q0 );
return A{ p0, q0 };
}
But this leaves me without an appropriate default constructor. Very inconvenient. Is there a way to accomplish this with a default constructor?
Just for fun, I actually found this, which does what I need but isn't really a viable option because it's not thread-safe:
int foo( bool b )
{
static int p;
static int q;
if( b ){
calculate( &p, &q );
return p;
} else {
return q;
}
}
struct A{
const int p;
const int q;
A() : p( foo( true ) ), q( foo( false ) ){}
};
EDIT Many people complained about me using const
members. But this question is interesting beyond const-members, in my specific use-case p
and q
are not int
but some class-type without a default constructor, so I need to use the member initialization syntax even if they are not const. Oh, and of course const-members do have advantages: faster debug-mode, fewer abstraction layers, less complexity, more compiler enforced invariants.