HELP: unfamiliar C++ syntax

  • Follow


I am trying to analyse this piece of C++ code below. It is an example
code from qwt.sf.net which is an extension to QT graphics library.

I assume line 03 is calling a constructor? I have never seen it written
this way. Is this a new syntax in C++? Same again in line 12. Are
QApplication & QString constructors? While a & info are the references
to the new constructed objects? And wtf is going on in line 14? Please
can someone explain what is going on.... :S

[code]
01. int main(int argc, char **argv)
02. {
03.     QApplication a(argc, argv);
04.
05.     QVBox vBox;
06.     vBox.setCaption("Cpu Plot");
07.
08.     CpuPlot *plot = new CpuPlot(&vBox);
09.     plot->setTitle("History");
10.     plot->setMargin(5);
11.
12.     QString info("Press the legend to en/disable a curve");
13.
14.     (void)new QLabel(info, &vBox);
15.     a.setMainWidget(&vBox);
16.
17.     vBox.resize(500,300);
18.     vBox.show();
19.
20.     return a.exec();
21. }
[/code]


PS: If you are interested in seeing more, download qwt.sf.net

0
Reply kevingpo (17) 6/27/2005 12:16:07 AM

Line 14 is a call to the class's constructor. Just like Java yes?

Why couldn't line 01 do QApplication a = new QApplication(argc, argv);
and line 12 do QString info = new QString("Press the legend to
en/disable a curve"); ? Or are they just the same as line 14's way of
creating an object?

0
Reply kevingpo (17) 6/27/2005 12:29:50 AM


KevinGPO wrote:

> I am trying to analyse this piece of C++ code below. It is an example
> code from qwt.sf.net which is an extension to QT graphics library.
> 
> I assume line 03 is calling a constructor?

It creates an object.

> I have never seen it written this way. Is this a new syntax in C++?

It's as old as C++ itself.

> Same again in line 12. Are QApplication & QString constructors?

They are classes. Constructors don't have names.

> While a & info are the references to the new constructed objects?

No, they are the objects, not references to them.

> And wtf is going on in line 14?

It dynamically allocates an instance of QLabel. The return value (the
pointer to the new object) is cast to void. I think this is sometimes done
to avoid a compiler warning about an ignored return value.

> Please can someone explain what is going on.... :S

You should get a book on C++. Creation of local objects is a very basic
thing. Don't start with Qt before you have a firm understanding of the C++
language.

0
Reply ramagnus (3485) 6/27/2005 12:32:23 AM

KevinGPO wrote:
> Line 14 is a call to the class's constructor. Just like Java yes?

It is much more than that.  'new' creates an object on the heap and 
initializes it by calling its c'tor and returns a pointer to the object.

> Why couldn't line 01 do QApplication a = new QApplication(argc, argv);
> and line 12 do QString info = new QString("Press the legend to
> en/disable a curve"); ? Or are they just the same as line 14's way of
> creating an object?

It could be done that way but they are not the same.  C++ has multiple 
ways to allocate memory for objects.  Line 01 is a stack allocation, 
also called local or automatic.  The object is deleted automatically 
when the enclosing function returns.  This type of allocation is much 
more efficient than heap allocations.

When 'new' is used you have a heap allocation.  The object can live 
longer than one function's scope, but the programmer becomes responsible 
for deleting it.

-- 
Scott McPhillips [VC++ MVP]

0
Reply Scott 6/27/2005 2:02:29 AM

> Why couldn't line 01 do QApplication a = new QApplication(argc, argv);
> and line 12 do QString info = new QString("Press the legend to
> en/disable a curve"); ? Or are they just the same as line 14's way of
> creating an object?
>

In addition to Scott's comments, QApplication a = new QApplication(argc,
argv); is invalid, it should be

    QApplication* a = new QApplication(argc, argv);
    //...
    delete a;


0
Reply benhongh2 (247) 6/27/2005 3:54:07 AM

Scott McPhillips [MVP] wrote:
> KevinGPO wrote:
> 
>> Line 14 is a call to the class's constructor. Just like Java yes?
> 
> 
> It is much more than that.  'new' creates an object on the heap and 
> initializes it by calling its c'tor and returns a pointer to the object.
> 
>> Why couldn't line 01 do QApplication a = new QApplication(argc, argv);
>> and line 12 do QString info = new QString("Press the legend to
>> en/disable a curve"); ? Or are they just the same as line 14's way of
>> creating an object?
> 
> 
> It could be done that way

No it couldn't. "QString info" declares a QString but "new QString(..." 
creates a pointer. I get the feeling the OP is assuming C++ has the same 
paradigm as Java et al. Not so. In C++ declaring a variable creates an 
actual object, not a syntactically obfuscated pointer. "QString 
info(..." creates an object called info and initialises it by calling 
the constructor that takes the parenthesised arguments.

> but they are not the same.  C++ has multiple 
> ways to allocate memory for objects.  Line 01 is a stack allocation, 
> also called local or automatic.  The object is deleted automatically 
> when the enclosing function returns.  This type of allocation is much 
> more efficient than heap allocations.
> 
> When 'new' is used you have a heap allocation.  The object can live 
> longer than one function's scope, but the programmer becomes responsible 
> for deleting it.

Yes. The OP needs to read a good C++ book, as missing all this stuff 
will leave programs open to memory leaks.

-- 
Ron House     house@usq.edu.au
               http://www.sci.usq.edu.au/staff/house
0
Reply house (179) 6/27/2005 6:58:18 AM

KevinGPO wrote:
> I am trying to analyse this piece of C++ code below. It is an example
> code from qwt.sf.net which is an extension to QT graphics library.
>
> I assume line 03 is calling a constructor? I have never seen it written
> this way. Is this a new syntax in C++?

How else have you seen it written in C++ ? I can't think of any other
(non-contrived) way of writting it to achieve the same thing ? Of
course if you mean you've seen it written differently in a different
language, then you need to realise that just because two language my
look similar it doesn't mean they have anything else in common !!!!

0
Reply gg2957 (99) 6/27/2005 9:49:48 AM


benben schreef:
> > Why couldn't line 01 do QApplication a = new QApplication(argc, argv);
> > and line 12 do QString info = new QString("Press the legend to
> > en/disable a curve"); ? Or are they just the same as line 14's way of
> > creating an object?
> >
>
> In addition to Scott's comments, QApplication a = new QApplication(argc,
> argv); is invalid, it should be
>
>     QApplication* a = new QApplication(argc, argv);
>     //...
>     delete a;

Actually, it should be

QApplication* a = new QApplication(argc, argv);
try {
   //...
}
catch (...) { // or a precise list of whatever try{...} throws
}
delete a;

which is a good reason to stick with the no-* no-new form. And this
try-catch is simple because it's at top level and we don't need to
rethrow. The common form is even more complex.

Regards,
Michiel Salters

0
Reply Michiel.Salters2 (415) 6/27/2005 12:44:55 PM

KevinGPO wrote:

> Line 14 is a call to the class's constructor. Just like Java yes?

No. Java's memory management concepts are completely different from C++'s.
Don't try to compare them.
Line 14 dynamically allocates memory and creates an object of class QLabel
in that memory.

> Why couldn't line 01 do QApplication a = new QApplication(argc, argv);

It would have to be:

QApplication* a = new QApplication(argc, argv);

but why would you dynamically allocate an object that you could create as a
simple local variable?

> and line 12 do QString info = new QString("Press the legend to
> en/disable a curve"); ? Or are they just the same as line 14's way of
> creating an object?

No, they are not at all the same. Again, I recommend reading a good book
about C++. You can't write useful C++ code unless you understand its
fundamental concepts.

0
Reply ramagnus (3485) 6/27/2005 1:13:45 PM

> Actually, it should be
>
> QApplication* a = new QApplication(argc, argv);
> try {
>    //...
> }
> catch (...) { // or a precise list of whatever try{...} throws
> }
> delete a;
>
> which is a good reason to stick with the no-* no-new form. And this
> try-catch is simple because it's at top level and we don't need to
> rethrow. The common form is even more complex.
>
> Regards,
> Michiel Salters
>

Hmm, that's right. Or it could be

    std::auto_ptr<QApplication> a(new QApplication(argc, argv));
    // ...

Regards,
Ben



0
Reply benhongh2 (247) 6/27/2005 2:22:21 PM

benben wrote:

> 
>> Actually, it should be
>>
>> QApplication* a = new QApplication(argc, argv);
>> try {
>>    //...
>> }
>> catch (...) { // or a precise list of whatever try{...} throws
>> }
>> delete a;
>>
>> which is a good reason to stick with the no-* no-new form. And this
>> try-catch is simple because it's at top level and we don't need to
>> rethrow. The common form is even more complex.
>>
>> Regards,
>> Michiel Salters
>>
> 
> Hmm, that's right. Or it could be
> 
>     std::auto_ptr<QApplication> a(new QApplication(argc, argv));
>     // ...

That's actually the preferred way in C++ and the reason why std::auto_ptr
exists.

0
Reply ramagnus (3485) 6/27/2005 2:57:11 PM

10 Replies
24 Views

(page loaded in 0.203 seconds)

Similiar Articles:




7/26/2012 2:20:54 AM


Reply: