zoukankan      html  css  js  c++  java
  • 光鉴科技面试

    1.C++11中的线程,生产者和消费者模式

    2.多线程中同步的方法

    互斥量,信号量,条件变量,读写锁;

    3.设计模式中的简单工厂和工厂方法和抽象工厂

    4.快速排序的原理,如何判定一个排序算法是稳定的

    5.单例模式如何实现的

    #include <iostream>
    using namespace std;
    
    class CSingleton
    {
    private:
        CSingleton()    //ctor should be private.
        {
        }
        static CSingleton * m_pInstance; //C++类中不可以定义自己类的对象,但是可以定义自己类的指针和引用.
    
    public:
        static CSingleton * GetInstance()
        {
            if (m_pInstance == nullptr)
            {
                m_pInstance = new CSingleton();
            }
            return m_pInstance;
        }
    };
    
    
    int main()
    {
        CSingleton * pcs = CSingleton::GetInstance();
        CSingleton cs;    //会报错,不允许其他方式生成该类的对象
        return 0;
    }

     上面写的有问题,会报错:

    undefined reference to `CSingleton::m_pInstance'

    因为static变量需要在类外初始化.为什么呢?因为静态变量不属于某个对象,而是属于类,如果放在类内初始化,则变成了这个对象的了,这就和之前的假设矛盾了.
    因此需要改为:
    #include <iostream>
    using namespace std;
    
    class CSingleton
    {
    private:
        CSingleton()    //ctor should be private.
        {
        }
        static CSingleton * m_pInstance; //C++类中不可以定义自己类的对象,但是可以定义自己类的指针和引用.
    
    public:
        static CSingleton * GetInstance();
    
    };
    
        CSingleton *  Singleton::m_pInstance = nullptr;  //定义时不需要static了.
        Singleton * Singleton::GetInstance();
        {
            if (m_pInstance == nullptr)
            {
                m_pInstance = new CSingleton();
            }
            return m_pInstance;
        }
    
    
    int main()
    {
        CSingleton * pcs = CSingleton::GetInstance();
        CSingleton cs;    //会报错,不允许其他方式生成该类的对象
        return 0;
    }
    新战场:https://blog.csdn.net/Stephen___Qin
  • 相关阅读:
    avcodec_decode_video2少帧问题
    什么是I帧,P帧,B帧
    让Ubuntu可以压缩/解压缩RAR文件
    Linux 向文件末尾追加命令
    valgrind: failed to start tool 'memcheck' for platform 'amd64-linux': No such file or directory
    《王者之剑2》性能数据精讲
    Unity加载模块深度解析(纹理篇)
    Unity加载模块深度解析(Shader)
    Unity加载模块深度解析(网格篇)
    Unity将来时:IL2CPP是什么?
  • 原文地址:https://www.cnblogs.com/Stephen-Qin/p/11885127.html
Copyright © 2011-2022 走看看