Am Fri, 09 Dec 2016 13:58:38 +0100
schrieb Alain Ketterlin <al...@universite-de-strasbourg.fr.invalid>:
> But you need to worry about your design: defining a struct with a
> const ref member *and* an assignment operator does not make much
> sense. Paavo's solution essentially bypasses the inherent incoherence
> by using destructor and constructor, but the problem remains: your
> design is absurd.
Okay, I am open to suggestions as to the design, although with Paavo's
solution, everything works as expected. My X is a class used in a
generic backtracking algorithm I have written. It contains the status
that changes when creating new chlidren or siblings.
template <class Status> class Backtrack {
bool findAll, isValid;
Status state;
bool reject();
bool accept();
Backtrack<Status> first();
Backtrack<Status> next();
void output();
public:
Backtrack(Status initial, bool fa=false) :
findAll(fa),state(initial),isValid(true) {}
void bt() {
//code according to the pseudo code in the wikipedia
//article on backtracking
}
};
I've used that code in a number of projects now by writing appropriate
functions reject(), accept(), first(), next(), and output(). It always
bothered me that I couldn't have members of Status which are const &,
although it might contain immutable information that is needed at all
levels of the search tree. My attempt is not to expose that information
if it is not necessary. Therefore, I want to avoid a global variable.
Also, a static member variable feels wrong since it must be accessed
from outside the class and I want to be able to have more than one
variable of class Backtrack<X> with different information in state. With
my design now I only need my „Special“ Status class and
Backtrack<Special> b;
b.bt(Special(…),true);
and I can use different parameters in … As I said, „Special“ can be very
big with only a few bits of information actually changing. Therefore, I
try to only make those parts variable and keep the rest const & as in
the example in the OP to avoid copying all the big stuff.
Any comments?