I have the following
struct dweDMPair {
const dweller *occu;
const double sqDM;
float prob;
dweDMPair(dweller *inOccu, double sqdm) : occu(inOccu), sqDM(sqdm) {} };
I want to return an array of pointers to these objects but desire that they should not be accidentally deleted by the client. Or, err, maybe not, just trying this design iteration.
My question is, is there a (very concise and neat) way of specifying (as I have illustrated with a const prefix) that members are only ever assigned in the constructor?
I know private/public and interface/methods could sort this, but humour me, how far can the const keyword be exploited?
_EDIT_
Nawaz, my goal is simply to initialize 2 of the 3 data members once only, in the constructor. Then I can export this instance as a const dweDMPair *ptrToVal so that the client cannot then call delete- because that won’t work on pointers to const instances will it? The client will then proceed to give their own value for the third member prob. yeah, I know functions enforce protection but I want it faster.
The members can’t be static but instance members. In case you were wondering..
You clarify that what you really want is:
Delete can be called on pointers to
constinstances and it can be called on objects which containconstmembers. This use ofconstwill not help you reach your goal.Some example code that compiles without complaint:
You might want to further clarify your end-goal (possibly in another question if it would change this one too much).