C++ pointer-like class

本文介绍C++2.0之前的智能指针auto_ptr,并通过代码示例展示了shared_ptr的实现方式及其如何使用。同时探讨了迭代器作为智能指针的一种形式。

C++2.0之前,有个智能指针叫auto_ptr

->符号作用下去之后,会一直作用下去。

sp->已经作用完了,还能px->吗? 可以的,会一直作用下去,这样子这个设计才有意义。
pic
代码:

template<typename T>
class shared_ptr {
public:
	shared_ptr(T* p) : px(p) {}

	// 智能指针一定是这种写法,重载 * 和 ->
	T& operator*() const{
		return *px;
	}

	T* operator->() const{
		return px;
	}

private:
	T* px;
};

struct Foo {
	void method() {
		cout << "This is a test." << "\n";
	}
};

int main(int argc, char** argv) {
	shared_ptr<Foo> sp(new Foo);
	sp->method();

	system("pause");
	return 0;
}

智能指针 C++11 以后有几种,具体需要去查,并且具体用法需要去了解,同时需要去看源码。

迭代器

迭代器也是指针的包装,也可认为是一种智能指针。

# Cross-platform C++ BaseClass and TypeSystem, UnitSystem, Geometry (FreeCAD Base module) Extracted by Qingfeng Xia, 2019-2021 It is extracted from FreeCAD project, <https://github.com/FreeCAD/FreeCAD>, it has the same license as FreeCAD: LGPL v2.1+ This piece of code extraction personal work is NOT sponsored by Qingfeng Xia's employment. v0.19 master branch, Nov 13, 2021. ## Features Most features from FreeCAD Base module, except for XML related IO. Conversion from PyCxx to pybind11 will be experimented here. - `BaseClass` and TypeSystem for C++: extracted from FreeCAD's Base module if only TypeSystem is needed, this can be configured by cmake `set(FC_TYPESYSTEM_ONLY ON)` - Collection of some header only libraries to catch Python productivity: json, argparse - Design pattern: Factory, Observer - Unit and Quantity System - Fundametnal classes for gometry: Vector3D, Boundbox, Axis, Matrix, Quaternion, VievProjection, 2D Shapes, OpenInventor Builder3D Todo: - `std::shared_ptr<T>` replace all void* - python wrapping helper, using pybind11 ## BaseClass ### Java and C# base class C++ does not have a base/root class for all objects, but lot of other high level languages have. see For a few C++ framework like QT, VTK, GTK, there is base class to provide shared functions see: [QT: `QObject`](https://doc.qt.io/qt-5/qobject.html) [VTK: `vtkObjectBase` and `vtkObject`](https://vtk.org/doc/nightly/html/classvtkObjectBase.html) ### Typical functions of base class 1. type system, implemented by c++ macro 2. reference counting, C++11 shared_pointer<> has this function 3. event/observer/subscription pattern, depends on the design of the framework 4. serialization,`std::to_string` ### help on script wrapping ### Tutorial for C++ TypeSystem 1. header file In each class's header file (inc. header only class), `TYPESYSTEM_HEADER();` must be the first line of that class Just like `Q_CLASS` for Qt meta system. ``` class CClass : public Base::BaseClass { TYPESYSTEM_HEADER(); public: int d = 0; }; ``` 2. source file In very beginnning of the source file for that class, not within class scope. Or any other cpp file for header only class. `TYPESYSTEM_SOURCE(CClass, Base::BaseClass);` header only is not supported for static member data declaration. 3. main source file To use this type system: in `main()` or module initialization function (called in `main()`) ```c++ int main() { using namespace Base; Type::init(); // init the type system // then init each class (actually register this class type), including the BaseClass BaseClass::init(); // this root class must be initialized // user classes init, can be wrap in a module init() CClass::init(); Type::destruct(); return 0; } ``` see the [TypeTest source](TypeTest.cpp) ## Collection of Design pattern - Design Patterns in C++ with Real Examples <https://github.com/ehsangazar/design-patterns-cpp> - <https://github.com/JakubVojvoda/design-patterns-cpp> ### included design patterns - AbstractFactory - Observor ## Collection of some header only libraries - toml11: header only lib: https://github.com/ToruNiina/toml11 https://github.com/skystrife/cpptoml - python style C++17 argparse: https://github.com/p-ranav/argparse - json: https://nlohmann.github.io/json/ see more header-only open source libraries at :https://awesomeopensource.com/projects/header-only ## More doc in doc subfolder <doc/FreeCADBaseChangeLog.md> <doc/PythonBinding.md> <doc/TypeSystem.md>翻译
06-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值