PDA

View Full Version : inheritance issue



raedbenz
4th August 2010, 08:16
Hello,
i have some strange behaviours in my code:

1- I have cWindowBase class, where it has the public member Qtimer *timer;. Now i have 2 derived classes, Class1 and Class2 that inherit cWindowBase with public keyword. everything works fine in these 2 derived classes. but when i set-up and start the timer in Class1 and i try from Class2 timer->isActive() the app crashes.

2- i have QProgressDialog *progress; as private member in ClassA. when i call the class A from ClassB (no inheritance) like

ClassA test;
test.ShowProg("Ttile", "Label")
the progressdialog works fine the first time i call it, the rest attempts progressdialog works but the Label is not displayed (only title is OK), the only way to get round it is to go out from ClassB.

hints plz?

tbscope
5th August 2010, 05:37
Step 1: get and read a good C++ book.

For problem 1: It's obvious the program will crash at timer->isActive since timer is an uninitialised pointer (as far as I understand your description).
What is it you want to do? Do you want to get information of class 1 in class 2?

Problem 2: Can you give more information, like source code?

raedbenz
5th August 2010, 08:03
For problem 1: It's obvious the program will crash at timer->isActive since timer is an uninitialised pointer (as far as I understand your description).
hello,

The Timer is already initialized and started in Class1. and works fine i.e. it times out and class the slot peridiocically. but if i try to stop or check if it is Active from Class2 it doesnt work. But yes it is not initialized in Class2.


What is it you want to do? Do you want to get information of class 1 in class 2? Yes.. note, the Timer is defined in the base class that is inherited in both Class1 and 2.


Problem 2

void SysClass::ProgDlg(QString title, QString text)
{
progress = new QProgressDialog(0, Qt::Dialog);

progress->setWindowTitle(title);
progress->setLabelText(text);
progress->setRange(0,100);
progress->setAutoClose(true);
progress->setAutoReset(true);

progress->show();
QCoreApplication::processEvents();
}
and i call it every 3 seconds.
SysClass tmp;
SysClass.ProgDlg("Hello","Wait") the progress bas appears but the string Wait appears only in the first call only..
Thanks

tbscope
6th August 2010, 05:41
The Timer is already initialized and started in Class1. and works fine i.e. it times out and class the slot peridiocically. but if i try to stop or check if it is Active from Class2 it doesnt work. But yes it is not initialized in Class2.

Yes.. note, the Timer is defined in the base class that is inherited in both Class1 and 2.

Like I said, try to find a good C++ book that explains pointers and class inheritance in detail.
This is not how C++ works.

Yes, you derived from a common base class where you define a pointer to a timer. But, the pointer in class 1 is completely different than the pointer in class 2.
What you can do is assign the timer pointer of class 1 to the timer pointer of class 2 so that both point to the same memory address. If you don't, timer->isActive() in class 2 WILL crash.

If you have a common base class, it doesn't automatically become the one and only initialised base class of all your subclasses. Look into singletons. Or just use setters/getters, signals/slots, events... to get information from one object to the other.



void SysClass::ProgDlg(QString title, QString text)
{
progress = new QProgressDialog(0, Qt::Dialog);

progress->setWindowTitle(title);
progress->setLabelText(text);
progress->setRange(0,100);
progress->setAutoClose(true);
progress->setAutoReset(true);

progress->show();
QCoreApplication::processEvents();
}
and i call it every 3 seconds.
SysClass tmp;
SysClass.ProgDlg("Hello","Wait") the progress bas appears but the string Wait appears only in the first call only..
Thanks

I have no idea about this problem.
Try this:

SysClass *tmp = new SysClass;
SysClass->ProgDlg("Hello","Wait");
Does that solve the problem?

raedbenz
6th August 2010, 07:46
What you can do is assign the timer pointer of class 1 to the timer pointer of class 2 so that both point to the same memory address. If you don't, timer->isActive() in class 2 WILL crash.

How do we assign the timer pointer of class 1 to the timer pointer of class 2 (without singleton) ?

problem 2 was solved by adding

progress->setMinimumDuration(0);

Thanks alot

tbscope
6th August 2010, 09:42
You can have this for example:


class MyClass1
{
public:
void setTimer(Timer *newTimer)
{
t = newTimer;
}

private:
Timer *t;
};

class MyClass2
{
public:
Timer *timer() const
{
return t;
}

private:
Timer *t;
};

...

MyClass1 *c1 = new MyClass1;
MyClass2 *c2 = new Myclass2;

c1->setTimer(c2->timer());


HOWEVER!
This should be done with care. Class 1 doesn't have any idea when class 2 deletes the timer. At least not if you don't code some handling for this situation.

I suggest working with signals and slots.