4

What is the difference between C++'s constructor and Eiffels create procedure declaration?

What value is there in being able to have multiple constructor functions for a class? Or is there any other benefit to Eiffels "create"?

Thankyou

Michael Brown
  • 498
  • 4
  • 13
  • 1
    When you say "What value is there in being able to have multiple constructor[s]... is there any _other_ benefit to Eiffels "create"" It seems like you are saying that Eiffel can have multiple constructors for a class but C++ can't, which is incorrect. I might have misread though. – Seth Carnegie Jan 13 '12 at 18:58
  • Your right! I never knew that you could overload a constructor. – Michael Brown Jan 13 '12 at 19:03

1 Answers1

3

In C++ you can overload constructors, so you can define multiple constructors as well.

Having constructor procedures as in EIFFEL has the advantage, that you can define different constructors having the same signature (i.e. same number and type of arguments).

Just imagine a Triangle class: You might want to construct a triangle by giving the lengths of the three sides (three float values), or by giving two sides and the enclosing angle (also three float values).

In C++ you would have to define an additional parameter (e.g. a enum value for the 'construction mode'), in EIFFEL you can simply define two construction procedures with different names.

MartinStettner
  • 28,719
  • 15
  • 79
  • 106
  • 2
    Actually you can just create a `static` function with a different name which will create the objects in the way you want, the same way as Eiffel's `create` – Seth Carnegie Jan 13 '12 at 19:09
  • Yes, but that's a completely different thing! Just think of inheritance for example: you cannot use these static functions within the constructor of a child class. – MartinStettner Jan 13 '12 at 19:13
  • That is true. Actually you could, but it wouldn't be worth the extra effort, which is why I said "at least one difference". – Seth Carnegie Jan 13 '12 at 19:14
  • I seriously doubt you can, at least not in standard C++. Also, `static` functions could only return references to dynamically allocated objects (or pointers). Of course you can use smart pointers etc. But it makes no sense, when comparing *language features*, to say that one feature of language *A* 'is the same thing' as some feature of language *B*, just because you can implement additional functionality to get the *same effect*. You could as well argue that a `struct` in C is more or less the same thing as a `class` in C++, just because you can do OOP in C as well ... – MartinStettner Jan 13 '12 at 19:40
  • 1
    Actually static functions have no problem returning objects by value, they don't have to return references or pointers. And yes you could in standard C++ use a static function to initialise an object, but it's not worth providing an example since it doesn't relate to this question. And yes, it is only achieving the same effect through a little more work, not through the same mechanism. He did ask "what value is there in..." not "what is the difference", and if you can achieve the same effect with a little more work, there's not much value. – Seth Carnegie Jan 13 '12 at 21:16
  • When returning objects by value, there are at least copy constructors or assignment operators involved - so there's a lot mor eto consider beyound 'simple' object construction. And I still cannot imagine a way how you would initialize a base class using a static method from within a child class constructor (at least in the way and with the guarantees constructors do it). But yes, I might have overlooked some 'real-world-language' (I'm not english native, sorry) subtleties in the question. and yes, this discussions seems so go a bit beyond the subject. – MartinStettner Jan 13 '12 at 23:06