028-86922220

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

面试题目记录-创新互联

这里写目录标题
推荐几款实用的C++ 在线工具
C++、Qt基础面试题

定制网站设计可以根据自己的需求进行定制,成都网站制作、网站建设构思过程中功能建设理应排到主要部位公司成都网站制作、网站建设的运用实际效果公司网站制作网站建立与制做的实际意义C++ 多线程

使用多线程及其同步的方法,写出一个多线程打印0,1,0,1序列的功能。或者写出一个生产者消费者的功能。
条件变量的wait为何有第二个参数?

拷贝构造、移动构造、赋值运算符、移动赋值运算符返回值

Test拥有默认构造函数、析构函数、拷贝构造、移动构造、移动赋值、拷贝赋值运算符。看fun函数对Test这些函数的调用时机。

Test fun() {
	Test t;
	return t;
}

有移动构造和没有移动构造,这里的不一样:

class Demo {
public:
    Demo() : num(new int(0)) {
        cout<< "construct!"<< endl;
    }

    Demo(const Demo &d) : num(new int(*d.num)) {
        cout<< "copy construct!"<< endl;
    }

    Demo(Demo &&d) : num(d.num) {
        d.num = nullptr;
        cout<< "move construct!"<< endl;
    }

    ~Demo() {
        cout<< "class destruct!"<< endl;
    }

private:
    int *num;
};

Demo getDemo() {
    Demo a;
    return a;
}

int main()
{
    auto b = getDemo();
    return 0;
}
i++/++i
int i = 1;
    int a = i++;
    i = 1;
    int b = ++i;

    i = 1;
    int c = ++i + ++i;

a = 1 b = 2 c = 6

操作符重载

参考:C++操作符重载

emplace右值引用
template//在std命名空间
typename remove_reference::type&&
move(T&& param)
{
    using ReturnType =                          //别名声明,见条款9
        typename remove_reference::type&&;

    return static_cast(param);
}
class Annotation {
public:
    explicit Annotation(const std::string text)
    :value(std::move(text)) 
    { … }                       
    …
private:
    std::string value;
};

参考:https://cntransgroup.github.io/EffectiveModernCppChinese/5.RRefMovSemPerfForw/item23.html
记住:
第一,不要在你希望能移动对象的时候,声明他们为const。对const对象的移动请求会悄无声息的被转化为拷贝操作。第二点,std::move不仅不移动任何东西,而且它也不保证它执行转换的对象可以被移动。

智能指针
processWidget(std::shared_ptr(new Widget),  //潜在的资源泄漏!
              computePriority());
// 解决方法1
processWidget(std::make_shared(),   //没有潜在的资源泄漏
              computePriority());
// 解决方法2
std::shared_ptrspw(new Widget);
processWidget(spw, computePriority());  // 正确,但是没优化,因为这里spw是一个左值,方法1中是一个右值参数
// 解决方法3
processWidget(std::move(spw), computePriority());   //高效且异常安全

因为std::shared_ptr(new Widget),是两个步骤:1、new Widget2、shared_ptr的构造函数,而std::make_shared()是一步操作。参考:条款二十一:优先考虑使用std::make_unique和std::make_shared,而非直接使用new
参考:https://blog.csdn.net/bureau123/article/details/121300979
https://zhuanlan.zhihu.com/p/416289479

// 两次分配内存,一次new Widget,一次shared_ptr构造函数中包含引用计数的控制块
std::shared_ptrspw(new Widget);
// 一次分配,分配的内存同时容纳了Widget对象和控制块
auto spw = std::make_shared();
Lambda表达式

Lambda实现原理:参考https://www.zhihu.com/question/57241113/answer/2440288161
Lambda捕获原理,在定义时捕获还是运行时捕获? 运行时,如果是定义时,那么某个捕获的变量如果变化了,那这个变化的值就得不到了。
下面这段代码有什么问题:

int i = 10;
    auto f = [=]() {
        i = 9;
    };
Lambda使用问题智能指针的实现
// 定义一个引用计数类,封装接口
class SharedCount
{
public:
    SharedCount() : m_count(1) {}

    void Add() { m_count++; }
    auto Reduce() { return --m_count; }
    auto GetCount() { return m_count; }

private:
    unsigned long m_count;
};


templateclass SharedPtr
{
public:
    // 默认构造
    SharedPtr() : m_ptr(nullptr), m_count(nullptr) {}
    // 普通指针初始化
    SharedPtr(T* t = nullptr) : m_ptr(t) {
        if (t != nullptr) {
            m_count = new SharedCount;
        }
    }
    ~SharedPtr() {
        if (m_ptr && m_count->Reduce() == 0) {
            delete m_ptr;
            m_ptr = nullptr;
            delete m_count;
            m_count = nullptr;
        }
    }

    // 拷贝构造
    SharedPtr(const SharedPtr&other) :
        m_count(other.m_count), m_ptr(other.m_ptr) {
        if (other.m_count) {
            other.m_count->Add();
        }
    }

    // 移动构造
    SharedPtr(SharedPtr&&other)
//        : m_count(std::move(other.m_count)),
//        m_ptr(std::move(other.m_ptr))
    {
        this->m_count = other.m_count;
        this->m_ptr = other.m_ptr;
        other.m_ptr = nullptr;
    }

    // 赋值运算
    SharedPtr& operator= (const SharedPtr& other) {
        if (m_ptr == other.m_ptr) {
            return *this;
        }
        // 目的对象不空
        if (m_ptr != nullptr) {
            if (m_count->Reduce() == 0) {
                delete m_ptr;
            }
        }
        // 赋值
        this->m_ptr = other.m_ptr;
        other.m_count->Add();
        this->m_count = other.m_count;
        return *this;
    }

    // 移动赋值运算符
    SharedPtr& operator=(const SharedPtr&&other) {
        if (m_ptr == other.m_ptr) {
            return *this;
        }

        if (m_ptr != nullptr) {
            if (m_count->Reduce() == 0) {
                delete m_ptr;
            }
        }

        m_ptr = other.m_ptr;
        m_count = other.m_count;
        other.m_ptr = nullptr;
        return *this;
    }

    // 解引用
    T* operator->() {
        return m_ptr;
    }

    T& operator* () {
        return *m_ptr;
    }

    SharedPtr& swap(SharedPtr&other) {

    }

    auto Get() { return m_ptr; }
    auto GetCount() { return m_count ? m_count->GetCount() : 0; }
private:
    T* m_ptr = nullptr;
    SharedCount* m_count = nullptr;
};
编译和内存知识

这部分需要完整看完【程序员的自我修养】

QT 使用qml有哪些问题win32窗口从点击到相应的过程

参考:https://leetcode.cn/circle/discuss/g4YxE2/

共享内存 共享内存上创建C++对象的问题

参考:https://blog.csdn.net/dickyjyang/article/details/21403451
https://www.cnblogs.com/yangru/p/3805192.html

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


当前标题:面试题目记录-创新互联
链接分享:http://www.tsicrk.com/article/ispod.html

其他资讯

让你的专属顾问为你服务

2.1494s