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

[翻译] Qt 图形视图框架 – Graphics View Framework

翻译

参见:https://doc.qt.io/qt-5/graphicsview.html

Qt 的图形视图,提供了一个界面用来管理大量2D图形项目;一个视图部件用来可视化这些图像项目,支持缩放和旋转。这个框架包括一个事件传播体系结构,该体系结构对场景中的项目进行精度和双精度交互功能。项目可以处理按键事件。鼠标按下、移动、释放和双击事件,他们还可以耿总鼠标的移动。图形视图使用BSP树提供了非常快速的项目检索功能,因此它可以实时可视化大型场景,甚至包括数百万的项目。

Continue reading

仿射变换

仿射变换 得概念如何理解?

几个名字先熟悉一下:

  • 线性变换 linear transformation
  • 平移 translate
  • 旋转 rotate
  • 仿射变换 affine transformation
  • 缩放 scale
  • 剪切 shear
  • 透视变换 perspective transformation

矩阵相关名词:

  • identity matrix 单位矩阵
  • non-singular matrix 可逆矩阵(非奇异矩阵)
  • matrix’s determinant 矩阵行列式

Qt 相关对象

  • QMatrix
  • QTransform

 

QMatrix 结构

QMatrix::QMatrix(qreal m11, qreal m12, qreal m21, qreal m22, qreal dx, qreal dy)
m11 m12 0
m21 m22 0
dx dy 1
  • dx ,dy     :    水平和垂直平移
  • m11, m22 : 水平和垂直缩放
  • m21, m12 : 水平和垂直剪切
x' = m11*x + m21*y + dx
y' = m22*y + m12*x + dy
      QMatrix translationMatrix(1, 0, 0, 1, 50.0, 50.0);
      QMatrix rotationMatrix(cosa, sina, -sina, cosa, 0, 0);
      QMatrix scalingMatrix(0.5, 0, 0, 1.0, 0, 0);

      QMatrix matrix;
      matrix =  scalingMatrix * rotationMatrix * translationMatrix;

QTransform 结构

 

延伸阅读

  • https://en.wikipedia.org/wiki/Affine_transformation
  • 为什么可逆矩阵又叫“非奇异矩阵(non-singular matrix)”
    https://www.cnblogs.com/marsggbo/p/10034629.html

Qt 自定义类“提升”

在设计某些自定义控件,可以将现有的控件进行提升。是一种代码级别的“复用”方式。特别适合对已有控件进行适当改造。参见《如何添加自定义的Qt类

此种方式区别于完全自定义的designer部件,参见 《一本正经建立 Qt 自定义设计师部件 Custom Designer Widget

扩展阅读

  • Qt自定义控件以及控件的提升
    https://blog.csdn.net/y396397735/article/details/78451245