marf
5th July 2008, 03:55
I'd like to think of myself as a intermediate programmer but I think I have a somewhat newb question.
I'm making a class, so I'm making the appropriate class1.h and class1.cpp
in the class1.h I have something like so
Method 1
/*Header File*/
...
private:
QHttp _http;
...
then within my class1.cpp I use it in methods like
/*Cpp File*/
...
_http.setHost("http://www.mysite.com");
connect( &_http, SIGNAL( done( bool ) ), this, SLOT( handleDone( bool ) ) );
...
which works just fine. But I can also change the two above to the following
Method 2
/*Header File*/
...
private:
QHttp *_http;
...
and again
/*Cpp File*/
...
_http = new QHttp("http://www.mysite.com");
_http -> setHost("http://www.myothersite.com");
connect( _http, SIGNAL( done( bool ) ), this, SLOT( handleDone( bool ) ) );
...
I know what I'm doing here, the first method is creating a QHttp variable, and what I assume is using the default constructor with null parameters. I then can use the methods of the class by using a period <.> with the method name. I can also connect the signal by using the & symbol (passing by reference).
If I use method 2, I am just creating a pointer to a QHttp, not calling the constructor, which is why I need to actually call the default constructor in the code. Then to call the methods I have to use -> instead. And when connecting the signal no need to use the &.
so my questions
Both methods work, which one is better and for what situations?
I'm making a class, so I'm making the appropriate class1.h and class1.cpp
in the class1.h I have something like so
Method 1
/*Header File*/
...
private:
QHttp _http;
...
then within my class1.cpp I use it in methods like
/*Cpp File*/
...
_http.setHost("http://www.mysite.com");
connect( &_http, SIGNAL( done( bool ) ), this, SLOT( handleDone( bool ) ) );
...
which works just fine. But I can also change the two above to the following
Method 2
/*Header File*/
...
private:
QHttp *_http;
...
and again
/*Cpp File*/
...
_http = new QHttp("http://www.mysite.com");
_http -> setHost("http://www.myothersite.com");
connect( _http, SIGNAL( done( bool ) ), this, SLOT( handleDone( bool ) ) );
...
I know what I'm doing here, the first method is creating a QHttp variable, and what I assume is using the default constructor with null parameters. I then can use the methods of the class by using a period <.> with the method name. I can also connect the signal by using the & symbol (passing by reference).
If I use method 2, I am just creating a pointer to a QHttp, not calling the constructor, which is why I need to actually call the default constructor in the code. Then to call the methods I have to use -> instead. And when connecting the signal no need to use the &.
so my questions
Both methods work, which one is better and for what situations?