Let
class Customer
{
private:
long id;
string name;
public:
...
};
be part of a class definition.
What is wrong with the following constructors ?
Customer () { id(0); name(''X''); }
Customer () ( long n, const string& s) : id(s), name(n) { }


Does anyone know this?

Dani AI

Generated

Quick clarification tied to the thread: ’s original constructors show syntax and type mistakes (parentheses in the wrong place, parameters/initializers swapped, and a character literal used where a string is intended). ’s quip about strict syntax is accurate, and correctly highlighted the subtle but important initialization-order issue. The following notes expand on those points and add safer patterns for modern C++.

A concise, modern approach is to use in-class defaults and, when needed, delegating or parameterized constructors. This avoids trivial boilerplate and reduces mistakes:

class Account {
  long id_ = 0;
  std::string name_ = "X";
public:
  Account() = default;                     // in-class defaults used
  Account(long i, std::string n) : id_(i), name_(std::move(n)) {}
  Account(long i) : Account(i, "generated") {}  // delegating ctor
};

Key practical rules and cautions:

  • Initialize members in the initializer list when they are const, references, or lack a default constructor; assignment inside the constructor body happens after default construction and may cost extra copies/moves.
  • Members are initialized in the order they are declared in the class, not the order in the initializer list; avoid computing one member from another in initializers to prevent undefined results.
  • Use double quotes for string literals (single quotes are for characters). For single-argument constructors consider explicit to prevent unintended conversions.

For authoritative details, see the cppreference pages on constructors and initialization order:
Constructors — cppreference
Order of initialization — cppreference

Recommended Answers

All 3 Replies

What's a tremendous, innovative C++ syntax! ;)
Are you sure that it's DaniWeb members homework?

Syntax mainly:

Constructors use variable initializers and an additional code block
which happens after initialization.

i.e.

Customer() : id(0),name("X") 
{ // code block here }

the second version is wrong because you have an extra ().
(write long int please!).

The only other thing you are going to need is that initialization of c++ constructors is in order!

// WRONG CODE:
class A
{
    int a;
    int b;
 public:
    // THIS DOESN't WORK 
    A(const int I) : b(I), a(b+1) {}  
    // THIS WOULD WORK: 
//    A(const int I) : b(a+1),a(I) {}
};

Try the fragment above and you will find that a can be just about anything as b in uninitialized when b+1 is calculated. That because despite the order you write it in, a is initialized first as it is in the class first.

OK, thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.