Implementation hiding

Ary Manzana ary at esperanto.org.ar
Wed Jun 29 20:30:14 PDT 2011


On 6/30/11 4:29 AM, so wrote:
>> Caveats:The class Foo cannot have any data members, even hidden ones
>> like a vptr. Therefore, it cannot have any virtual functions.Foo
>> cannot have any constructors, because we aren't constructing a real
>> Foo, only a counterfeit one.
>
> Funny, these two requirements are in fact the starting points for my
> search. I asked many times:
>
> * I don't need any virtual method.
> * I don't need a constructor and construction is problematic for this
> kind of work, mass allocation, io and such...
>
> Then why do i have to pay for a vtable or another layer of indirection
> and on top of it an ugly design? There has to be a solution to this! But
> there was none.

The solution is to be able to override new. In Ruby you can do this:

---
class Foo
end

class Bar
   def self.new
     Foo.new
   end
end
---

Now you do:

Bar.new #=> #<Foo:0x0000010180bf90>

and as you see, you get a Foo instance.


More information about the Digitalmars-d mailing list