Michel Fortin wrote:
On 2009-02-09 07:00:56 -0500, Weed <[email protected]> said:
No. By forbiding the cases that leads to slicing, like returning a
polymorphic object by value.
Let's think, can there are other ways to solve problem?
Here, for example my reasons:
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=81958
I'm
not sure I'm getting what you mean in that post.
It seems like you want the ability to copy classes, and thus pass them
by value, but only when there would be no type conversion.
I'm not getting where this can be useful though: by forcing your classes
to be of a specific type, with no derived types allowed, you're losing
polymorphism. The only advantage you have is that your type can be
derived from another so you can reuse some of its implementation. There
are other ways to acheive that in D however (mixins come to mind).
The one case I could think of was the strategy pattern: no data is added
(including, no virtual functions) -- the only thing that's added is a
different implementation of an existing virtual function. In such a
situation, the slicing problem cannot happen. You can have an array of
polymorphic types. But the language won't allow it.
However, I've encountered this problem in C++ as well. Allowing
stack-based classes is NOT sufficient to solve it. What you actually
want is a struct with a vtable pointer stored IN the struct itself
(rather than one instance of the vtable per struct).