当前位置: 首页 > news >正文

网页小游戏无需登录什么叫做优化

网页小游戏无需登录,什么叫做优化,设计制作费属于什么服务,代点任意广告链接网站scoped_ptr 不能被复制或赋值给其他 scoped_ptr 对象&#xff0c;不能与其他指针比较 (除了 nullptr) scoped_ptr 用例 template <typename T> class scoped_ptr { public:// 构造函数&#xff1a;初始化 scoped_ptr 并接管指针的所有权explicit scoped_ptr(T* ptr n…

scoped_ptr

不能被复制或赋值给其他 scoped_ptr 对象,不能与其他指针比较 (除了 nullptr)

scoped_ptr 用例

template <typename T>
class scoped_ptr {
public:// 构造函数:初始化 scoped_ptr 并接管指针的所有权explicit scoped_ptr(T* ptr = nullptr) : ptr_(ptr) {}// 析构函数:释放管理的对象~scoped_ptr() {delete ptr_;}// 禁止复制构造函数和赋值操作符scoped_ptr(const scoped_ptr&) = delete;scoped_ptr& operator=(const scoped_ptr&) = delete;// 移动构造函数和移动赋值操作符scoped_ptr(scoped_ptr&& other) noexcept : ptr_(other.release()) {}scoped_ptr& operator=(scoped_ptr&& other) noexcept {if (this != &other) {reset(other.release());}return *this;}// 重载解引用操作符T& operator*() const {return *ptr_;}// 重载箭头操作符T* operator->() const {return ptr_;}// 获取管理的指针T* get() const {return ptr_;}// 释放管理的指针并返回T* release() {T* tmp = ptr_;ptr_ = nullptr;return tmp;}// 重置管理的指针void reset(T* ptr = nullptr) {if (ptr_ != ptr) {delete ptr_;ptr_ = ptr;}}// 检查是否管理一个非空指针explicit operator bool() const {return ptr_ != nullptr;}private:T* ptr_;
};

unique_ptr

头文件<boost/smart_ptr/make_unique.hpp>里实现了make_unique()函数,位于名字空间 boost 而不是 std,为了避免潜在的冲突。

unique_ptr 用例

创建单个对象
#include <memory>struct MyClass {MyClass(int x, double y) : x_(x), y_(y) {}int x_;double y_;
};int main() {auto ptr = std::make_unique<MyClass>(42, 3.14);// ptr is a std::unique_ptr<MyClass>return 0;
}
创建数组
#include <memory>int main() {auto arr = std::make_unique<int[]>(10);// arr is a std::unique_ptr<int[]> with 10 elementsreturn 0;
}
函数中返回动态分配的对象
#include <memory>std::unique_ptr<int> createInt(int value) {return std::make_unique<int>(value);
}int main() {auto ptr = createInt(42);// ptr is a std::unique_ptr<int>return 0;
}
容器中存储动态分配的对象
#include <memory>
#include <vector>int main() {std::vector<std::unique_ptr<int>> vec;vec.push_back(std::make_unique<int>(1));vec.push_back(std::make_unique<int>(2));vec.push_back(std::make_unique<int>(3));// vec contains std::unique_ptr<int> elementsreturn 0;
}
避免资源泄漏
#include <memory>void process(std::unique_ptr<int> ptr) {// Do something with ptr
}int main() {auto ptr = std::make_unique<int>(42);process(std::move(ptr)); // No need to manually delete the pointerreturn 0;
}

shared_ptr

引用计数型的智能指针,可以被自由地拷贝和赋值,可以在任意的地方共享它,当没有代码使用它时(引用计数为0),才删除被包装的动态分配的对象。

shared_ptr 用例

  • 函数中返回动态分配的对象
  • 容器中存储动态分配的对象
  • 避免资源泄漏
创建单个对象
#include <memory>struct MyClass {MyClass(int x, double y) : x_(x), y_(y) {}int x_;double y_;
};int main() {auto ptr = std::make_shared<MyClass>(42, 3.14);// ptr is a std::shared_ptr<MyClass>return 0;
}
创建数组(C++20)
#include <memory>int main() {auto arr = std::make_shared<int[]>(10);// arr is a std::shared_ptr<int[]> with 10 elementsreturn 0;
}

shared_ptr 应用于标准容器

shared_ptr 作为容器的元素
#include <iostream>
#include <memory>
#include <vector>class MyClass {
public:MyClass(int id) : id_(id) {std::cout << "Constructing MyClass " << id_ << std::endl;}~MyClass() {std::cout << "Destroying MyClass " << id_ << std::endl;}void print() const {std::cout << "MyClass " << id_ << std::endl;}
private:int id_;
};int main() {std::vector<std::shared_ptr<MyClass>> vec;// 创建并存储 shared_ptr 对象for (int i = 0; i < 5; ++i) {vec.push_back(std::make_shared<MyClass>(i));}// 使用 shared_ptr 对象for (const auto& ptr : vec) {ptr->print();}return 0;
}
容器作为 shared_ptr 的管理对象
#include <iostream>
#include <memory>
#include <vector>class MyClass {
public:MyClass(int id) : id_(id) {std::cout << "Constructing MyClass " << id_ << std::endl;}~MyClass() {std::cout << "Destroying MyClass " << id_ << std::endl;}void print() const {std::cout << "MyClass " << id_ << std::endl;}
private:int id_;
};int main() {// 创建一个 shared_ptr 管理 vector 容器auto vecPtr = std::make_shared<std::vector<std::shared_ptr<MyClass>>>();// 向 vector 中添加 MyClass 对象for (int i = 0; i < 5; ++i) {vecPtr->push_back(std::make_shared<MyClass>(i));}// 使用 vector 中的 MyClass 对象for (const auto& ptr : *vecPtr) {ptr->print();}return 0;
}

桥接模式

#include <iostream>
#include <memory>// 渲染器接口
class Renderer {
public:virtual ~Renderer() = default;virtual void renderCircle(float x, float y, float radius) = 0;virtual void renderRectangle(float x, float y, float width, float height) = 0;
};// OpenGL 渲染器
class OpenGLRenderer : public Renderer {
public:void renderCircle(float x, float y, float radius) override {std::cout << "OpenGL rendering circle at (" << x << ", " << y << ") with radius " << radius << std::endl;}void renderRectangle(float x, float y, float width, float height) override {std::cout << "OpenGL rendering rectangle at (" << x << ", " << y << ") with width " << width << " and height " << height << std::endl;}
};// DirectX 渲染器
class DirectXRenderer : public Renderer {
public:void renderCircle(float x, float y, float radius) override {std::cout << "DirectX rendering circle at (" << x << ", " << y << ") with radius " << radius << std::endl;}void renderRectangle(float x, float y, float width, float height) override {std::cout << "DirectX rendering rectangle at (" << x << ", " << y << ") with width " << width << " and height " << height << std::endl;}
};// 形状接口
class Shape {
public:Shape(std::shared_ptr<Renderer> renderer) : renderer_(renderer) {}virtual ~Shape() = default;virtual void draw() = 0;
protected:std::shared_ptr<Renderer> renderer_;
};// 圆形
class Circle : public Shape {
public:Circle(std::shared_ptr<Renderer> renderer, float x, float y, float radius): Shape(renderer), x_(x), y_(y), radius_(radius) {}void draw() override {renderer_->renderCircle(x_, y_, radius_);}
private:float x_, y_, radius_;
};// 矩形
class Rectangle : public Shape {
public:Rectangle(std::shared_ptr<Renderer> renderer, float x, float y, float width, float height): Shape(renderer), x_(x), y_(y), width_(width), height_(height) {}void draw() override {renderer_->renderRectangle(x_, y_, width_, height_);}
private:float x_, y_, width_, height_;
};int main() {auto openglRenderer = std::make_shared<OpenGLRenderer>();auto directxRenderer = std::make_shared<DirectXRenderer>();Circle circle1(openglRenderer, 10, 10, 5);Rectangle rectangle1(openglRenderer, 20, 20, 15, 10);Circle circle2(directxRenderer, 30, 30, 7);Rectangle rectangle2(directxRenderer, 40, 40, 20, 15);circle1.draw();rectangle1.draw();circle2.draw();rectangle2.draw();return 0;
}

weak_ptr

不具有普通指针的行为,没有重载 operator* 和 ->。weak_ptr 被设计为与 shared_ptr 协同工作,可以从一个 shared_ptr 或另一个 weak_ptr 对象构造以获得资源的观测权。但 weak_ptr 没有共享资源,它的构造不会引起指针引用计数的增加。

weak_ptr 用例

#include <iostream>
#include <memory>int main() {std::shared_ptr<int> shared = std::make_shared<int>(42);std::weak_ptr<int> weak = shared;if (auto locked = weak.lock()) {std::cout << "Object still exists: " << *locked << std::endl;} else {std::cout << "Object has been destroyed." << std::endl;}shared.reset(); // 释放 shared_ptr,对象被销毁if (auto locked = weak.lock()) {std::cout << "Object still exists: " << *locked << std::endl;} else {std::cout << "Object has been destroyed." << std::endl;}return 0;
}

打破循环引用

class node
{
public:~node(){std::cout << "deleted" << std::endl;}typedef weak_ptr<node> ptr_type;//typedef shared_ptr<node> ptr_type;ptr_type next;
};void case3()
{auto p1 = make_shared<node>();auto p2 = make_shared<node>();p1->next = p2;p2->next = p1;assert(p1.use_count() == 1);assert(p2.use_count() == 1);if(!p1->next.expired()){auto p3 = p1->next.lock();}
}
http://www.hkea.cn/news/701647/

相关文章:

  • 浙江建设职业技术学院迎新网站做一个网站要多少钱
  • axure做网站好不好手机百度问一问
  • 开发微信小程序的流程广州seo优化电话
  • 小企业网站建设和管理全能搜
  • 无棣县建设局网站游戏优化大师下载安装
  • 小额贷款 网站模板品牌推广软文
  • 网站建设开发成本天津百度搜索网站排名
  • 做的好的营销型网站有哪些内容外贸网站外链平台
  • 东营网站建设预算价格百度网盘网页版入口官网
  • 网站建设中标公告18款禁用看奶app入口
  • 网站运营人员岗位职责长沙正规seo优化价格
  • cnzz统计代码放在后台网站为什么没显示seo的英文全称是什么
  • 杭州企业网站建设方案广告门
  • 自己免费做网站(二)seo优化公司信
  • 广州外贸b2b网站建设刷钻业务推广网站
  • 做企业网站用什么怎样宣传自己的品牌
  • 濮阳做网站的公司我的百度账号
  • 美食网站开发如何做好网络营销工作
  • 网站建设案例资料今天的新闻内容
  • 台州专业网站建设方案seo软文代写
  • 个人网站 百度推广全球搜索大全
  • 网站消息推送5118素材网站
  • 天津 响应式网站设计企业网站模板免费
  • 网站用花生壳nas做存储百度seo发包工具
  • wordpress cache深圳纯手工seo
  • 怎样找到正规代加工网站百度地图3d实景地图
  • 潍坊网站建设公司网站搭建免费
  • 惠州做网站好的公司下载百度语音导航地图安装
  • 春节网站怎么做小说排行榜百度搜索风云榜
  • 商城服务是什么软件seo是指什么岗位