C++: Inline ctors not init'ing data members?
C++: Inline ctors not init'ing data members?
- Subject: C++: Inline ctors not init'ing data members?
- From: Dan Caugherty <email@hidden>
- Date: Mon, 15 Jun 2009 15:35:43 -0400
Hey all --
I found something strange this past week, and I hope there's a simple explanation for it that will leave me feeling terribly stupid.
I'm working on a project that has a class something like the (just a little) contrived one below:
//
// haberdasher.h
//
namespace using std;
class haberdasher {
public:
haberdasher() : hat_sizes_(), customers_() {}
~haberdasher() {}
// ...
void add_customer(const string & name) { customers_.push_back(name); }
const string & get_last_customer() const { return customers_.back(); }
private:
map<string, unsigned int> hat_sizes_;
vector<string> customers_;
};
After a call to add_customer(), the call to get_last_customer() will retrieve a faulty reference (in this case, the data area of the string will contain a char pointer equal to 0x0). Adding and accessing items in the map behaves similarly.
Defining the constructor in haberdasher.cpp (making it non-inline) resolves the issue.
Why should this be the case?
(Again, I'm probably missing something obvious here...)
Thx in advance,
-- Dan C.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden