[Gambas-user] newbie question virtual classes
Benoit Minisini
gambas at ...1...
Wed Feb 1 08:15:12 CET 2006
On Wednesday 01 February 2006 07:07, Christopher Brian Jack wrote:
> On Tue, 31 Jan 2006, johnf wrote:
> > > Laurent
> >
> > OK so I can - but do not need to use it. All right then what do doc's
> > mean showing the virtual class. As you can see I really don't understand
> > the Doc's reference to the virtual class. To me a virtual class is a
> > class that describes an object for use in other objects. Helper class or
> > a class I inherit. Anyway I don't understand what is being said in the
> > doc's John
>
> Hmm are you coming to Gambas from a C++ background? You can consider
> "virtual class" to mean a class with pure virtual methods and private
> constructor/destructors (which yields a virtual class that you cannot
> create or copy but can pass by reference) but that has friends within
> lower level components that implement the class (it can use the class as a
> base). Thus, though you cannot create the class directly or use it as a
> base class in your Gambas programs, the friends that are implementing the
> class can be accessed as ListBox.ListBoxItem[n] where the "virtual" class
> here is only defining an interface to access the items of the listbox.
>
> This bit of C++ (pardon me in advance if some of my syntax is off)
> attempts to show what's going on. It is possible in C++ to return a
> reference to an interface whose implentor and interface objects are not
> directly instantiable or useable as a base class using inheritence:
>
> class ListBox;
> class ListBoxItemIMP;
>
> class _ListBoxItem {
> friend class _ListBoxItenIMP;
> private:
> _ListBoxItem() {}
> virtual ~_ListBoxItem {}
> public:
> std::String Text()=0
> ...
> };
>
> class _ListBoxItemIMP : private _ListBoxItem {
> friend class ListBox;
> private:
> Listbox& host;
> private:
> _ListBoxItemIMP() {...}
> virtual ~_ListBoxItemIMP {...}
> SetParent(ListBox &parent) : host(parent) {...}
> public:
> std::String Text() {...}
> ...
> };
>
> class ListBox : public Control {
> public:
> ListBox(Control &parent) {...}
> virtual ~ListBox() {...}
> private:
> std::vector<_ListBoxItemIMP> data;
> pubilc:
> ListBoxItem& operator [] (int idx) {return data[idx];}
> int Count() {return data.length();}
> ...
> };
>
>
> -- Christopher Brian Jack
>
'Virtual' class in Gambas has nothing to do with 'virtual' class in C++. I
admit I badly chosen the word, but C++ did too, in a different way.
A virtual class is actually a way for an object of a real class to look like
another class, said virtual. Maybe 'pseudo-class' would be a better word.
You cannot instanciate a virtual class. You can only use it temporarily inside
an expression.
For example: MyTreeView[key] returns a '.TreeViewItem' from its key, but you
cannot put it inside a variable. You can only use it inside an expression,
like MyTreeView[key].Text, or inside a WITH...END WITH control structure:
WITH MyTreeView[key]
PRINT .Text
PRINT .Key
PRINT .Parent.Text
...
END WITH
I did it to avoid object allocations. Gambas creates no object when you access
an item inside a treeview, so things are faster.
As the documentation is automatically generated, the documentation of
properties that use virtual class is not very understandable at the moment.
Sorry for the inconvenience!
I hope it is clearer now :-)
Regards,
--
Benoit Minisini
More information about the User
mailing list