PDA

View Full Version : How can i get the mentioned output



namus
25th June 2010, 13:57
#include<QObject>
#include<QString>
#include<QDebug>
#include<QApplication>

class VerboseObject : public QObject {

public:
VerboseObject(QObject *parent, const QString& name):QObject(parent){
parent->setObjectName(name);
qDebug()<<"\n constructor \n";
}
~VerboseObject() {
qDebug()<<"\n Deleted"<<(*this).objectName()<<"\n";
}
void do_sthing() {
qDebug()<<"\n do sthing"<<(*this).objectName()<<"\n";
}


};


int main(int argc , char* argv[])
{
QApplication A(argc ,argv);

VerboseObject top(&A,"Top");

VerboseObject *x = new VerboseObject(&top,"x");
VerboseObject *y = new VerboseObject(&top,"y");
VerboseObject *z = new VerboseObject(&top,"z");

top.do_sthing();
x->do_sthing();
y->do_sthing();
z->do_sthing();

return 0;
}


I am expecting output:

constructor
constructor
constructor
constructor

do sthing "Top"
do sthing "x"
do sthing "y"
do sthing "z"

Deleted "Top"
Deleted "x"
Deleted "y"
Deleted "z"

JohannesMunk
25th June 2010, 21:59
Hi!



#include<QObject>
#include<QString>
#include<QDebug>
#include<QApplication>

class VerboseObject : public QObject {

public:
VerboseObject(QObject *parent, const QString& name):QObject(parent){
setObjectName(name);
qDebug() << "constructor";
}
~VerboseObject() {
qDebug()<<"Deleted" <<(*this).objectName();
}
void do_sthing() {
qDebug()<<"do sthing" <<(*this).objectName();
}


};


int main(int argc , char* argv[])
{
QApplication A(argc ,argv);

VerboseObject top(&A,"Top");

VerboseObject *x = new VerboseObject(&top,"x");
VerboseObject *y = new VerboseObject(&top,"y");
VerboseObject *z = new VerboseObject(&top,"z");

top.do_sthing();
x->do_sthing();
y->do_sthing();
z->do_sthing();

return 0;
}

results in:

Starting E:\Develop\QtCentre\ObjectDelete\ObjectDelete\debu g\ObjectDelete.exe... constructor
constructor
constructor
constructor
do sthing "Top"
do sthing "x"
do sthing "y"
do sthing "z"
Deleted "Top"
Deleted "x"
Deleted "y"
Deleted "z"
E:\Develop\QtCentre\ObjectDelete\ObjectDelete\debu g\ObjectDelete.exe exited with code 0

You accidentaly set the objectname of the parent instead of the class itself. And you don't need line breaks. Each call to qDebug() automatically ends with a line break.

Joh