When I setup the object like this: robot = Sparkoomba(57600,0,true) the values for baud, ddPin, and automaticMode are correctly saved in the object. If I, however, do robot = Sparkoomba() all the values return 0.
Any ideas? Am I not allowed to call a constructor from inside of another constructor
You might want to read the second link in my post above about init lists.
Your way is great but lots of folks recommend init lists instead. It really depends on how expensive it is o construct the RHS of the assignment so with simple types like this it is fine.
Guys, before this gets too academic, I think @harrisonhjones needs to see an example of what either scenario looks like! As for “cost”, how many classes are we talking about. One, maybe two!!! You guys are just too smart sometimes
Ah yeah, totally init lists over assignment is a good rule. I wasn’t considering the c’tor implementation, just focusing on getting a no-arg constructor.
Although here I personally find the direct assignment more readable, especially since we can assign like this->x = x. And as you say, it’s doubtful there’s any penalty for initializing primitives. (And if there were a penalty, it’s not critical unless it’s some inner loop.)
@harrisonhjones - using init lists would give you a c’tor like this:
Oh… I’m fairly sure I’m doing that. I hadn’t considered any other way! :P. I guess I could have just put the class in the top of my main .cpp file? Doesn’t sound too re-usable…
@harrisonhjones, so you would put the Class prototype in the .h file and the Class definition (including init lists) in the .cpp for the library. Then you include the library’s .h file in your app, giving you re-usability. Not sure what you mean by “Doesn’t sound too re-usable”?
Yep! You got it! Reusability is the key…aaaaand then there’s also laziness…in my worst afflicted moments, I’ve just put the entire class def in a header!
Although that might come from the fact I’m used to scripting and Java where you don’t have to faff about separate header and body.
Although truthfully, with C++0x/C++11 the “laziness” has really taken off and we now have many header-only libraries (e.g. boost, STL etc.)