C++值单例模式与auto_ptr

#include <iostream>
#include <memory>
using namespace std;

class Singleton
{
public:
    static Singleton* GetInstance()
    {
//        if (instance_ == nullptr)
//        {
//            instance_ = new Singleton;
//        }
//        return instance_;
        if(!instance_.get())
        {
            instance_ = auto_ptr<Singleton>(new Singleton);
        }
        return instance_.get();
    }

    ~Singleton()
    {
        cout << "~Singleton ..." << endl;
    }

private:
    // 禁止拷贝(只有声明,没有实现,要是有拷贝的操作,会直接报错)
    Singleton(const Singleton& other);
    Singleton& operator=(const Singleton& other);
    // 拷贝构造声明为私有,外部就不能构造对象
    Singleton()
    {
        cout << "Singleton ..." << endl;
    }
    // static Singleton* instance_;  // 这边使用智能指针,不然就必须手动释放instance_,在项目中,可能有很多地方调用,这样就很难控制在哪释放
    static auto_ptr<Singleton> instance_;
};

auto_ptr<Singleton> Singleton::instance_;

int main() {
    Singleton* s1 = Singleton::GetInstance();
    Singleton* s2 = Singleton::GetInstance();
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

// 输出
Singleton ...
Hello, World!
~Singleton ...

Noncopyable继承注意要点

class Noncopyable
{
protected:
    Noncopyable() {}
    ~Noncopyable() {}
private:
    Noncopyable(const Noncopyable&);
    const Noncopyable& operator=(const Noncopyable&);
};

class Parent : private Noncopyable
{
public:
    // 默认构造函数中,即使后面没有加上:Noncopyable(),也会自动调用基类的默认构造函数。但是拷贝构造函数就不一样。
    // 如果加上了: Noncopyable(),则基类必须要有: Noncopyable()的声明
    Parent() : Noncopyable()
    {

    }
    // 当你自己又重新定义了拷贝构造函数,则不会去调用基类的拷贝构造函数,这样基类的私有拷贝就会失效。这样就会被拷贝出来。
    // 所以这样的拷贝构造是不符合规范的
//    Parent(const Parent& other)
//    {
//
//    }

    // 下面是符合规范的拷贝构造函数(这样才能实现禁止拷贝)
    Parent(const Parent& other) : Noncopyable(other)
    {

    }
};

class Child : public Parent
{

};

int main() {
    Parent p1;
    Parent p2(p1);  // 要调用拷贝构造函数,Parent构造函数又调用Noncopyable的拷贝构造函数(私有且没有实现,所以会报错)

//    Child c1;
//    Child c2(c1);   // 也是同上的道理
    return 0;
}

自定义sizeof和内存对齐

#include <iostream>
using namespace std;

// 大小的宏
// 两个指针相减,得到的是相隔几个元素
#define sizeof_v(x) (char*)(&x+1) - (char*)(&x)
#define sizeof_t(t) ((size_t)((t*)0 + 1))

// 对齐
#define ALIGN(v, b) ((v+b-1) & ~(b-1))
class Empty
{

};
int main() {
    Empty e;
    int n;

    cout << sizeof_v(e) << endl;
    cout << sizeof_v(n) << endl;

    cout << sizeof_t(Empty) << endl;
    cout << sizeof_t(int) << endl;

    cout << ALIGN(3, 16) << endl;
    cout << ALIGN(31, 16) << endl;
    cout << ALIGN(0, 16) << endl;
    cout << ALIGN(4198, 4096) << endl;
    return 0;
}

// 输出
1
4
1
4
16
32
0
8192

相关推荐

  1. C++模式auto_ptr

    2024-06-18 09:58:02       7 阅读
  2. 模式C#】

    2024-06-18 09:58:02       37 阅读
  3. 模式C++)

    2024-06-18 09:58:02       33 阅读
  4. C++ 模式

    2024-06-18 09:58:02       39 阅读
  5. C++ 模式

    2024-06-18 09:58:02       33 阅读
  6. C# 模式

    2024-06-18 09:58:02       19 阅读
  7. C++模式

    2024-06-18 09:58:02       7 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-18 09:58:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-18 09:58:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-18 09:58:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-18 09:58:02       20 阅读

热门阅读

  1. MySQL触发器基本结构

    2024-06-18 09:58:02       10 阅读
  2. 从零开始精通Onvif之图片抓拍

    2024-06-18 09:58:02       7 阅读
  3. PHP之EOF定界符

    2024-06-18 09:58:02       7 阅读
  4. 科研辅助工具

    2024-06-18 09:58:02       5 阅读
  5. Unity与Android交互通信系列(6)

    2024-06-18 09:58:02       4 阅读
  6. idea git stash报错Too many revisions specified

    2024-06-18 09:58:02       7 阅读
  7. 创建单例模式的六种方式

    2024-06-18 09:58:02       9 阅读
  8. jQuery 常用函数解析

    2024-06-18 09:58:02       7 阅读
  9. MVVM模式理解(基于Qt分析)

    2024-06-18 09:58:02       5 阅读
  10. 11 类型泛化

    2024-06-18 09:58:02       6 阅读