PDA

View Full Version : Why QWindow is not initialized with the NEW keyword?



probine
3rd April 2010, 20:44
To create a window we can do:

QWidget window;

Can we also do:

QWidget window = new QWindow;


How can we create a window without the NEW keyword? What type of class is it?

Lykurg
3rd April 2010, 21:44
How can we create a window without the NEW keyword?
Ahh... you can create one if you read a introduction into C++!!! Come on, that is so basic stuff!
QWidget* window = new QWidget();

faldzip
3rd April 2010, 21:51
To create a window we can do:

QWidget window;

Can we also do:

This:


QWidget window = new QWindow;

is wrong. With new keyword you have to use a pointer type:


QWidget *window = new QWidget;




How can we create a window without the NEW keyword?
Normally, like any other objects.


What type of class is it?
hmm it depends what type of object you make:


int a; // this is int
QWidget w; // this is QWidget
QListView lv; //this is QListView


Generally, widgets should be created on a heap (so with new keyword) but in main() function it is good to create main widget on stack so it will be deleted when going out of scope which is when your application exit.

probine
3rd April 2010, 22:20
Thanks for the answers. And even though it seems basic, the question is much more complex. Pretend:
_____
class Person
{
...
// Anything that a class will have goes here
}
_____

To instantiate this class I must do:

Person myPerson = new Person();


I couldn't do:

Person myPerson;


Base on this example does it mean that QWidget is a regular class, a struct or what?

faldzip
3rd April 2010, 22:25
Thanks for the answers. And even though it seems basic, the question is much more complex. Pretend:
_____
class Person
{
...
// Anything that a class will have goes here
}
_____

To instantiate this class I must do:

Person myPerson = new Person();


I couldn't do:

Person myPerson;


Base on this example does it mean that QWidget is a regular class, a struct or what?

Your code will only work in case it is a Java code. Are coding in Java or C++?

wysota
3rd April 2010, 23:28
To instantiate this class I must do:

Person myPerson = new Person();


This will only compile with MS Visual C compiler. But this is syntactically incorrect as far as C++ standard is concerned.