Of course, C++ constructors can crash for absolutely any reason. The interesting reason is member initialization. Consider this code:
C::C(const std::string& a) : a(a) {}
C::C(const std::string& acuracy) : accuracy(accuracy) {}
Just now, I've seen a more complicated example. A colleague first shown a code like in the first example and asked if it's OK. I immediately replied that it's OK unless there's a typo. But the real crash was a bit more contrived:
class Entry {
public:
     Entry(const std::string& EntryId) : EntryId(EntryId) {}
     std::string EntryId;
};
class LogEntry : public Entry
{
public:
    LogEntry(const std::string& Id): Entry(EntryId) {}    
};
Moral of the story
- Use the "m_" prefix
- Make all data members private
No comments:
Post a Comment