2

I'm sure I'll botch some of the specific terminology, but what is the difference between object and object2 in this implementation? Is the only purpose of the first method (prototyping the class's function by the same name of the class) just to save typing? At the end of the code, does object==object2?

//first method
class Class1{
public:
        int i;
        Class1 (parameter);
};
Class1::Class1 (parameter){
        i = 10;
}
Class1 object1(parameter);

//second method
class Class2{
public:
        int i;
        void function (parameter);
};
Class2::function (parameter){
        i = 10;
}
Class2 object2;
object2.function(parameter);

(C++11)

3

Class1 and Class2 should be something like:

class Class1 {
public:
  int i;
  Class1(int parameter) : i(parameter) {}  // This is a constructor
};

class Class2 {
public:
  int i;
  void function(int parameter) {i = parameter;} // This is a init function
};

and

Class1 object1(10);
Class2 object2;
object2.function(10);

The value of i will be the same: object1.i == object2.i but the purpose of the constructor is NOT to save typing.

  • as Joel said in a comment , with the constructor it's mandatory to assign a value to i, while with the init function you could use i without a previous explicit assignment.

    In properly designed OO-code the constructor is responsible for establishing the class invariants (here i should be a private data member). Without a constructor each use of the class must first ensure that the object has been properly built.

    You cannot re-initialize an object of a class using its constructor (*) but you can use operator=.

  • const objects can be created only via constructor:

    const Class1 object1(10);
    

    you cannot write:

    const Class2 object;   // error (uninitialized const object)
    object2.function(10);  // error (attempt to change the const object)
    
  • constructor is always more or equally efficient as having code outside

  • many other reasons (see references).

Technicalities

  • it's possible to re-initialize an object of a class using its constructor via placement new. Of course it's ugly beyond description.
  • there are reasons to prefer init functions. Init functions are able to call virtual functions on the object. This doesn't always work in a constructor, because in the constructor of the base class, the derived class part of the object "doesn't exist yet", and in particular you can't access virtual functions defined in the derived class. Instead, the base class version of the function is called, if defined (see also Why use an initialization method instead of a constructor?).

References

-4

I believe I've found the answer at cplusplus.com

It looks like I was correct, but that I should also keep in mind that the 'constructor' is only called at creation of the object and can never be called again per that object, whereas a function within the class can be called repeatedly.

  • 1
    It's not only a matter of being called only once or more, but it also changes what is mandatory or not: in class 1, setting "i" is mandatory, whereas in class 2 it isn't. – Joel Oct 20 '14 at 17:51
  • 2
    Ironically, your own answer doesn't address the points you raised in your question. And while the site you mentioned may answer things more fully, the link you provided is subject to link rot and the reader has to dig in to find the specifics of how it answered your question. – GlenH7 Oct 21 '14 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.