Why doesn’t delete zero out its operand?

Consider

	delete p;
	// ...
	delete p;

If the … part doesn’t touch p then the second “delete p;” is a serious error that a C++ implementation cannot effectively protect itself against (without unusual precautions). Since deleting a zero pointer is harmless by definition, a simple solution would be for “delete p;” to do a “p=0;” after it has done whatever else is required. However, C++ doesn’t guarantee that.One reason is that the operand of delete need not be an lvalue. Consider:

	delete p+1;
	delete f(x);

Here, the implementation of delete does not have a pointer to which it can assign zero. These examples may be rare, but they do imply that it is not possible to guarantee that “any pointer to a deleted object is 0.” A simpler way of bypassing that “rule” is to have two pointers to an object:

	T* p = new T;
	T* q = p;
	delete p;
	delete q;	// ouch!

C++ explicitly allows an implementation of delete to zero out an lvalue operand, and I had hoped that implementations would do that, but that idea doesn’t seem to have become popular with implementers.If you consider zeroing out pointers important, consider using a destroy function:

	template<class T> inline void destroy(T*& p) { delete p; p = 0; }

Consider this yet-another reason to minimize explicit use of new and delete by relying on standard library containers, handles, etc.

Note that passing the pointer as a reference (to allow the pointer to be zero’d out) has the added benefit of preventing destroy() from being called for an rvalue:

	int* f();
	int* p;
	// ...
	destroy(f());	// error: trying to pass an rvalue by non-const reference
	destroy(p+1);	// error: trying to pass an rvalue by non-const reference

Qt5 信号重载

本篇介绍 Qt5 信号的重载

    radioGroup = new QButtonGroup(this);

    radioGroup->addButton(ui->radioButton1);
    radioGroup->addButton(ui->radioButton2);
    radioGroup->setExclusive(true);

    //old style
    connect(radioGroup,SIGNAL(buttonToggled(int,bool)),this,SLOT(radioGroupToggled()));

    //error
    //    connect(radioGroup,
    //            &QButtonGroup::buttonToggled,
    //            this,
    //            &MainWindow::radioGroupToggled
    //            );

    //Qt help
    //    connect(radioGroup,
    //            QOverload<QAbstractButton *, bool>::of(&QButtonGroup::buttonToggled),
    //            [=](QAbstractButton *button, bool checked){
    //        qDebug() << "button is: " << button << " is checked: " << checked;
    //    });

    //fix1
    //    connect(radioGroup,
    //            QOverload<QAbstractButton *, bool>::of(&QButtonGroup::buttonToggled),
    //            this,
    //            &MainWindow::radioGroupToggled
    //            );

延申阅读

  • Qt5中使用lambda表达式
    https://www.cnblogs.com/yuankaituo/p/5135750.html
  • https://www.devbean.net/2012/05/cpp11-lambda/

Qt 批量操作 UI 控件

目的

Qt的UI界面编程时,会遇到这种情况,界面上分布大量相同的控件,如LineEdit_1、LineEdit_2、LineEdit_3 等一系列控件。对这么多控件进行操作,无疑采用遍历的方式是最好的办法,可是往往这些控件都是在UI设计器里手动添加的,所以并不能立刻得到可批量操作的地址。下面有3种方法可以供操作。

Continue reading