0

Is it possible to be "DRY" (do not repeat yourself")? I would like to declare my class and essentially construct it later.

this code works:

// default constructor        
UserInput::UserInput() {}; 

// other constructor
UserInput::UserInput(string title, string section, string subsection) :
    title(title), section(section), subsection(subsection) {
    SPLIT();
}

// i want to be "DRY", is it possible?
// this is the same code as the "other constrcutor"
void UserInput::operator()(string title, string section, string subsection) {
    this->title = title;
    this->section = section;
    this->subsection = subsection;
    SPLIT();
}

this does not work, the class ends up with blank strings:

void UserInput::operator()(string title, string section, string subsection) {
    UserInput(title, section, subsection);
}
1
  • It sounds like you might want a boost::optional, which lets you delay creation and makes no value a valid value, like null. Commented Dec 19, 2015 at 2:57

1 Answer 1

1
void UserInput::operator()(string title, string section, string subsection) {
    *this = UserInput(title, section, subsection);
}

This is not terribly efficient, but if performance is not an issue for you, this will do the trick.

void UserInput::operator()(const string &title, const string &section, const string &subsection) {
    *this = UserInput(title, section, subsection);
}

This will be a little bit efficient. You should also make the same change to the real constructor's parameters, as well.

EDIT: here' an alternatve approach referenced in the comments. This is a bit more "standard-ish". It is quite common for constructors to invoke additional class methods to finish constructing object. This only works when class members have default constructors. As mentioned earlier, there's some duplicated effort here -- the class members get default-constructed first, only to be fully constructed again. But, it does tend to eliminate code duplication, and is a bit cleaner when performance or efficiency isn't at the premium.

UserInput::UserInput(const string &title, const string &section, const string &subsection)
{
    operator()(title, section, subsection);
}

void UserInput::operator()(const string &title, const string &section, const string &subsection) {
    this->title = title;
    this->section = section;
    this->subsection = subsection;
    SPLIT();
}
Sign up to request clarification or add additional context in comments.

5 Comments

is what I'm doing non-standard, is there a common way of doing it? Should I be doing something like declaring the object then sending it a class later via a class constructor like this?: UserInput ui; ui = UserInput(params); edit: oh maybe this is exactly what you're doing in your answer.
Well, a slightly more "standard" approach would be to eliminate duplicate code the other way around, by invoking operator() from the constructor: UserInput::UserInput(...) { operator()(title, section, subsection); }
yes! that's what I wanted.. let me see if I can get that implemented.
Hey, could you add this explanation to your answer as a second alternative answer? Thank you!
Sure, added. Nothing really earth-shattering.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.