PDA

View Full Version : QPainter Constructor Syntax Question



deejross
25th January 2011, 15:38
I have a quick question about the QPainter examples I have seen. The syntax seems strange and doesn't look like it should compile.


QPainter painter(this);

I would expect this to be something like:


QPainter painter = new QPainter(this);

or even something like this:


QPainter painter = QPainter::fromWidget(this);

But instead it looks like a syntax error. So how and why does this work? I'm from C# and still a beginner with C++ and Qt, so thanks in advance for answering such newbie question :)

high_flyer
25th January 2011, 20:25
Please google about stack vs heap allocation in C++, and the concept of pointers.

deejross
25th January 2011, 21:21
Since I already gave an example of creating the object on the heap, I can only assume you are trying to say that this is the syntax C++ uses to create objects on the stack. The question I have is why is the object created in this way as opposed to on the heap like nearly every other object I've seen examples of?

Lykurg
25th January 2011, 21:36
Since I already gave an example of creating the object on the heap,...which were wrong. You are missing a *.
But anyway. What do you don't like with
QPainter p(this);Would
QPainter p;satisfy you? If so great, because
QPainter p(this);is just a shorthand for
QPainter p;
p.begin(this);

Or please elaborate, what puzzles you exactly.

deejross
25th January 2011, 21:45
The syntax is what confused me. I had never seen parameters passed right after the variable name before, so I had no idea what was going on. Now that I know that it is creating the object, but just on the stack instead of the heap, that helps. Thanks.

ChrisW67
25th January 2011, 21:59
The example that puzzles you is allocation on the stack. The bracketed part of the expression is the arguments to the QPainter constructor. If you wrote:

QPainter p; you would be creating a QPainter using its default (parameterless) constructor. The example you give uses a different constructor to build the QPainter and initialise it in some way. The constructor options are in the Assistant docs for QPainter. The same syntax can be used to initialise ('construct') basic types created on the stack:


int x(9);
char c('A');


You have given a (slightly incorrect) example of allocating the QPainter on the heap.
Your second example may be either stack or heap (with the same missing * as your first) depending on what your fictious QPainter::fromWidget() is supposed to return (a pointer or an instance of QPainter).

wysota
26th January 2011, 01:16
And not looking too far in nearly all Qt apps we have:

QApplication app(argc, argv);

By the way, there is one glitch - if a class has a default constructor, you might be tempted to do this:

QPainter p();
which is wrong as it is a declaration of a function called "p" returning a QPainter object. A small C++ standard anomaly :) It becomes more obvious if you rewrite it as:

QPainter p(void);