• 3401阅读
  • 4回复

[提问]求助一个问题 关于对象初始化的 [复制链接]

上一主题 下一主题
离线sudoman
 
只看楼主 倒序阅读 楼主  发表于: 2011-11-10

为什么QT中 QLabel *label=new QLabel("HelloQt",0);或者QLabel label("HelloQt",0);可以编译成功  

但是QLabel label=QLabel("HelloQt",0);就不能编译通过。
离线ninsun

只看该作者 1楼 发表于: 2011-11-11
估计QLabel的拷贝函数被屏蔽了……
离线dbzhang800

只看该作者 2楼 发表于: 2011-11-11
引用第1楼ninsun于2011-11-11 08:31发表的  :
估计QLabel的拷贝函数被屏蔽了……

Manual中说的清楚:

No copy constructor or assignment operator

QObject has neither a copy constructor nor an assignment operator. This is by design. Actually, they are declared, but in a private section with the macro Q_DISABLE_COPY(). In fact, all Qt classes derived from QObject (direct or indirect) use this macro to declare their copy constructor and assignment operator to be private. The reasoning is found in the discussion on Identity vs Value on the Qt Object Model page.

The main consequence is that you should use pointers to QObject (or to your QObject subclass) where you might otherwise be tempted to use your QObject subclass as a value. For example, without a copy constructor, you can't use a subclass of QObject as the value to be stored in one of the container classes. You must store pointers.
离线zhy282289
只看该作者 3楼 发表于: 2011-11-11
又长知识了
为什么我脸这么胖~
离线sudoman
只看该作者 4楼 发表于: 2011-11-14
回 2楼(dbzhang800) 的帖子
恩 谢谢  具体的懂了  好像是说QT中得对象都是唯一的 不允许复制  所以Q_DISABLE_COPY()就将=操作符重载和复制构造函数放到private里面了  不允许调用~~是不是这么理解的?
快速回复
限100 字节
 
上一个 下一个