After reading up on C++11 and common guidelines surrounding it, I often read about how you should use in-class initialization as well as aggregate initialization.
Here's an example from what seems to be the "old" way of doing things:
class Example
{
public:
// Set "m_x" to "x", "m_y" gets set to the default value of 5
Example(int x) : m_x(x), m_y(5)
{
}
private:
int m_x;
int m_y;
};
And to my understanding this is what people recommend now:
class Example
{
public:
Example(int x) : m_x{x}
{
}
private:
int m_x{0}; // Supposedly {} works too? I guess this would
// only be necessary if I had another constructor
// taking in 0 arguments, so "m_x" doesn't go uninitialized
int m_y{5};
};
My question is: How does this affect pointers, references, and some of the STL classes? What's the best practice for those? Do they just get initialized with {}
? Additionally, should I be doing this even if the constructor initializes the variables anyway? (i.e. Writing m_x{}
even though it gets set to something else by the constructor anyway)
Thank you.
Aucun commentaire:
Enregistrer un commentaire