帮助:不熟悉的C ++语法 [英] HELP: unfamiliar C++ syntax

查看:64
本文介绍了帮助:不熟悉的C ++语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试分析下面的这段C ++代码。这是来自qwt.sf.net的示例

代码,它是QT图形库的扩展。


我假设第03行调用构造函数?我从来没有见过以这种方式写过这么多b $ b。这是C ++中的新语法吗?第12行也是如此。

QApplication& QString构造函数?而且& info是新构造对象的引用

? wtf在第14行进行?请

有人可以解释发生了什么......:S

展开 | < span class =codeLinkonclick =selectAll(this);>选择 | 换行 | 行号

解决方案

KevinGPO写道:

我正在尝试分析下面的这段C ++代码。这是来自qwt.sf.net的示例代码,它是QT图形库的扩展。

我假设第03行调用构造函数?


它会创建一个对象。

我从未见过以这种方式编写的。这是C ++中的新语法吗?


它和C ++本身一样古老。

第12行再次相同。是QApplication& QString构造函数?


他们是班级。建设者没有名字。

虽然& info是对新构造对象的引用?


不,它们是对象,而不是对它们的引用。

和wtf在第14行进行?


它动态分配QLabel的一个实例。返回值(指向新对象的

指针)将转换为void。我认为这有时会完成

以避免编译器警告忽略返回值。

请有人解释发生了什么......:S



你应该得到一本关于C ++的书。创建本地对象是一件非常基本的事情。在你对C ++

语言有一个充分的了解之前不要从Qt开始。


第14行是一个电话到班级的构造者。就像Java一样吗?


为什么不行01执行QApplication a =新的QApplication(argc,argv);

和第12行执行QString信息=新的QString(按下图例

en /禁用曲线); ?或者它们与第14行创建对象的方式相同?


KevinGPO写道:

第14行是对类的构造函数的调用。就像Java一样?


远不止于此。 ''new''在堆上创建一个对象并且

通过调用它的c''tor来初始化它并返回指向该对象的指针。

为什么不能第01行执行QApplication a =新QApplication(argc,argv);
和第12行执行QString info = new QString(按图例按
启用/禁用曲线); ?或者它们与第14行创建对象的方式相同?




它可以这样做,但它们不一样。 C ++有多种方式为对象分配内存。第01行是堆栈分配,

也称为本地或自动。当封闭函数返回时,对象将自动删除

。这种类型的分配比堆分配更有效。


当使用''new'时,你有一个堆分配。该对象可以比一个函数的范围长得多b $ b,但程序员负责

删除它。


-

Scott McPhillips [VC ++ MVP]


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

Expand|Select|Wrap|Line Numbers

解决方案

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.


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?


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]


这篇关于帮助:不熟悉的C ++语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆