[Gambas-user] newbie question virtual classes

Christopher Brian Jack brian at ...1334...
Wed Feb 1 07:07:33 CET 2006


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




More information about the User mailing list