[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-multimedia
Subject:    Re: adding properties to a C++ class
From:       Scott Wheeler <scott () slackorama ! net>
Date:       2001-12-18 9:20:30
[Download RAW message or body]

Thanks to your help I now have things working.  My question now is, "Is this 
considered safe?"

What I'm doing is defining a subclass of QListViewItem that has an extra 
property (I haven't integrated this into QTagger yet, just a test program.).

Then, to add that to my QListView I use:

myListItem *newItem=new myListItem(itemList);
// ^ my subclass                                     ^my QListView

Then QListView sends a signal which passes a pointer to a QListViewItem, 
which I've implemented a slot for.  In the slot's implementation I do a 
static cast:

myListItem *item=static_cast<myListItem*>(QLVitem);

And then can access my new method.  Here's the slot implementation:
===
void listMain::clickedItem(QListViewItem *QLVitem)
{
  myListItem *item=static_cast<myListItem*>(QLVitem);
  cout << item->text(0) << endl; // a normal QLV method
  cout << item->getMyValue() << endl; // my method
}
===
And the slot that creates the item:
===
void listMain::addItem()
{
  myListItem *newItem=new myListItem(itemList);
  newItem->setText(0, newLine->text()); // newLine is a QLineEdit
  newItem->setMyValue(newItem->text(0).length());
}
===

Is all of that considered safe?  It seems to work, which is good.  I just 
thought I would check with some more experienced people before I added this 
to my app.

Once again, thanks!

-Scott

On Monday 17 December 2001 06:34 am, Rolf Magnus wrote:
> On Monday 17 December 2001 07:18, Scott Wheeler wrote:
> > Is there any way to make a subclass function as a the baseclass even
> > though it has more features?
> > Is there any way to allow class quux to accept assignment from foo?
>
> #include <iostream>
>
> class foo {
>  private:
>    int myInt;
>  public:
>    int value() { return(myInt); }
>    void setValue(int value) { myInt=value; }
>  };
>
> class baz : public foo {};
>
> int main()
> {
>    foo* bar = new baz;
>    bar.setValue(3);
>    cout << bar.value() << endl;
>    baz* quux;
>    quux = static_cast<baz*>(bar);
>    cout << quux->value() << endl;
>    return(0);
> }
>
> To cast the objects themselves you need a proper copy constructor.
_______________________________________________
kde-multimedia mailing list
kde-multimedia@mail.kde.org
http://mail.kde.org/mailman/listinfo/kde-multimedia
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic