[Solved]what does this syntax mean in c++?
-
I want to know what this syntax says:
@(void) new ClassName();@
the syntax above is used in this snip code for populate a widget.
@ void populateTree(QTreeWidget *treeWidget)
{
QStringList labels;
labels << QObject::tr("Terms") << QObject::tr("Pages");treeWidget->setHeaderLabels(labels); treeWidget->header()->setResizeMode(QHeaderView::Stretch); treeWidget->setWindowTitle(QObject::tr("XML Stream Writer")); treeWidget->show(); (void) new QTreeWidgetItem(treeWidget, QStringList() << "sidebearings" << "10, 34-35, 307-308"); QTreeWidgetItem *subterm = new QTreeWidgetItem(treeWidget, QStringList() << "subtraction"); (void) new QTreeWidgetItem(subterm, QStringList() << "of pictures" << "115, 244"); (void) new QTreeWidgetItem(subterm, QStringList() << "of vectors" << "9"); }@
-
The (void) doesn't do anything, it just removes some warnings for some IDEs to tell it's normal the result of the "new" isn't stored in any variable.
-
[quote author="whitesong" date="1367141103"]thanks. so in some IDEs "(void)" will be needed[/quote]It's not really needed, but it's nice to get rid of compiler warnings.
In this code below, your compiler might warn you that x and y are unused. :
@
void myFunction(int x)
{
int y = 1;
return; // Produces a warning that x and y are unused
}
@But, calling (void) tells the compiler to "do nothing" with the variables, so it won't complain:
@
void myFunction(int x)
{
int y = 1;// Prevents warnings (void) x; (void) y; return;
}
@Qt has its own macro to do this, instead of (void). The code below has exactly the same effect as the code above:
@
void myFunction(int x)
{
int y = 1;// Prevents warnings Q_UNUSED(x); Q_UNUSED(y); return;
}
@