class collar {
public:
collar() { std::cout << " collar is born.\n"; } //Since collar in dog class invokes default constructor in collar
};
class dog {
collar m_collar;
string& m_name; // Reference should be initialized, hence leads to an error.
};
int main() {
dog dog1;
}
output:
main.cc:13: error: no matching function for call to `dog::dog()'
main.cc:8: note: candidates are: dog::dog(const dog&)
// Add to dog:
// string& m_name;
// Result: not compilable because age is not initialized.
/* C++ 11 Update: */
class dog {
public:
dog() = default;
dog(string name) {...};
}
Comments
Post a Comment