[Gambas-user] Gambas bug?

Fabián Flores Vadell fabianfloresvadell at ...626...
Sat Feb 11 16:22:19 CET 2012


2012/2/11 Bruce Bruen <bbruen at ...2308...>

>
> Interesting, I have a similar problem, perhaps the same but maybe
> slightly different.
>
> I have a base class that is CREATE PRIVATE, it cannot be instantiated
> directly by a client application.  It contains various methods that must
> be available to it's child classes (which CAN be instantiated) and can
> be called by the client application.  For example, DBAdd is public in
> the base class so that the child class can call it.
>
> What I have done, with some success is as follows:
>
> In the base class I inspect the System.Backtrace and compare the object
> indicated by the second item (i.e. System.Backtrace[1]) to see that it
> "IS" a Me.  (is that clear, if not I'll have to go find the code?)
> IOW I check that the caller object is an instance of Me, which can only
> be the base class itself or a class that inherits the base class. If
> not, I just raise an "Illegal Call" error.
>

I don't undertand what is the problem that you are intending to solve.

You declare your class as PRIVATE, hence just instances from its subclasses
can call the methods on the base class. So, allways the inmediate caller
would be a method of an instance of a its subclass.


> This works within reason, however it has the following drawbacks:
> b) it does not prevent a client application from instantiating an object
> that inherits directly from the base class.
>

Why you want to avoid that a client application can instantiate a subclass
of the its private superclass?

Anyway, don't you can do it making the subclass private too?


> To expand this further.  I also have examples where I need (read "want")
> to have methods in the base class that MUST be overridden in the
> specialised child classes.  This is because these methods are templates
> only. However, they must be present in the base class, as the base class
> calls these methods!  IOW the base class calls "SomeMethod", which my
> design says "must be implemented in the child class".  Gambas is
> (almost) great at achieving this by its' use of virtual dispatch. If the
> child class has correctly overridden the base class method, then that is
> the one that is called.  And in the base class, I can have a "stub"
> method that simply raises an error.  If the child class does not
> override the method the the error is raised.
>

Seems to me that you don't need do this if you are programming alone or
with your team, because you easily can convey the need to redefine some
methods.

I can see the need for do that only when you are writing an object library
or a generic component, that will be used for unknown programmers (clients).


> But hope this helps.


Thanks, Bruce. In my case this don't help because the problem that I intend
to solve, is present only when to use inheritance is not reasonable.


-- 
Fabián Flores Vadell
www.comoprogramarcongambas.blogspot.com
www.speedbooksargentina.blogspot.com



More information about the User mailing list