Take the 2-minute tour ×

In C++ or Objective-C, is the term "constructor" exactly the same as "initializer"?

The reason is that, whenever I see the term "constructor", it feels somewhat vague, because it also feels like the constructor is responsible for setting up the object in the memory space, while in fact, constructors merely "initialize". So the term "constructor" can be confusing because it feels like it is alloc and init combined, while the term initializer is very clear that it is merely setting up values.

Is the above statement generally correct?

share|improve this question

3 Answers 3

No, they aren't the same.

An initializer is part of the C++ grammar, not a function. For example:

std::string s = "abcd";

The initializer is = "abcd".

When an initializer is present in a variable definition, it supplies arguments which will be passed to the constructor.

You can read more about this in section 8.5 of the Standard.

share|improve this answer

A constructor is guaranteed to run at allocation time and is used to initialize an object into some defined state. In C++, we call this RAII. An initializer is, as you said, merely "setting up values" or "initializing the object" and has no requirement to run at allocation time.

share|improve this answer
so when we say we write the constructor of a C++ class, we are merely writing "part 2" of the constructor's work (the initialization)? (part 1 was automatically done for you) –  Jeremy L Apr 23 '12 at 2:30
If it helps you to think of it that way, then yes. When you create an object of class foo or struct bar (structs are actually classes in C++), the constructor is automatically called. –  San Jacinto Apr 23 '12 at 11:12

A constructor takes some raw storage, and constructs an object there.

An initializer is a value used to initialize some variable.

While you probably have a point that what's called a constructor could have been called an initializer instead, I doubt it was ever really considered for either C++ or Objective-C. "Initializer" already had its currently accepted meaning in C well before either C++ or Objective C came along. Since term was already well known in C, I can't imagine anybody trying to re-define it to mean something entirely different in a language based on C.

share|improve this answer

Your Answer


