Cppcoreguidelines: C.49 needs better example

Created on 22 Nov 2017  路  7Comments  路  Source: isocpp/CppCoreGuidelines

C.49's good example is the same what was defined as a bad example in C.45. Your good examples should not contradict your own rules

Most helpful comment

Would

class X {
    string s;
public:
    X(string n) :s{n} { } // GOOD
    // ...
};

Be a better 'good' example? as opposed to

    X(string n) { s = n; } // BAD

All 7 comments

For easy reference:
c.45: Don't define a default constructor that only initializes data members; use member initializers instead
c.49: Prefer initialization to assignment in constructors

Agreed. We could just put /* ... */ in the default constructor in the C.49 example, suggesting it does something _other_ than simply initialize the member. Or we could add a "better" example that follows C.45, with a cross-reference.

Would

class X {
    string s;
public:
    X(string n) :s{n} { } // GOOD
    // ...
};

Be a better 'good' example? as opposed to

    X(string n) { s = n; } // BAD

Yes, that works too. The B example should be changed similarly.

Thank you for the suggestions! We agree, especially with the sample provided by @xtofl. @rmasp98, could you please prepare a PR?

Done! (https://github.com/isocpp/CppCoreGuidelines/pull/1092)

The PR #1092 was merged and closed, so shouldn't this issue be closed, too?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RedDwarf69 picture RedDwarf69  路  3Comments

beinhaerter picture beinhaerter  路  5Comments

franzhollerer picture franzhollerer  路  6Comments

HowardHinnant picture HowardHinnant  路  3Comments

NN--- picture NN---  路  7Comments