美文网首页
c/c++学习笔记3-基础关键字和要点

c/c++学习笔记3-基础关键字和要点

作者: scott_yu779 | 来源:发表于2017-11-22 21:50 被阅读0次

    异常相关

    • throw学习和理解
      简而言之就是 throw一个异常会退出这个函数的调用堆栈,直到其上级堆栈有捕获为止。如果都没有捕获,那么程序退出。退出错误为,unhandled exception。

    线程相关知识

    • 线程传参-类名后面是小括弧
    class background_task
    {
    public:
    void operator()() const
    {
    do_something();
    do_something_else();
    }
    };
    std::thread my_thread(background_task());
    

    这里相当与声明了一个名为my_thread的函数,这个函数带有一个参数(函数指针指向没有参
    数并返回background_task对象的函数),返回一个 std::thread 对象的函数,而非启动了一个
    线程。

    • 各种传参-作为笔记
      1、传递一个成员函数指针作为线程函数,并提供一个合适的对象指针作为第一个参数
    class X
    {
    public:
    void do_lengthy_work();
    };
    X my_x;
    std::thread t(&X::do_lengthy_work,&my_x); // 1
    

    这段代码中,新线程将my_x.do_lengthy_work()作为线程函数;my_x的地址①作为指针对象
    提供给函数。也可以为成员函数提供参数: std::thread 构造函数的第三个参数就是成员函数
    的第一个参数,以此类推
    2、

    c++的语义

    • 移动语义
      1、只能移动不能拷贝的
      下面都是可移动,但不可拷贝
      std::ifstream ,
      std::unique_ptr
      std::thread

    const的各种

    成员函数后面的const 表示此函数不会更改类的成员变量,如果有更改,则报错,起到保护作用
    如下例子所示

    template<typename   T,typename  Container=std::deque<T> >
    class   stack
    {
    public:
            explicit    stack(const Container&);
            explicit    stack(Container&&   =   Container());
            template    <class  Alloc>  explicit    stack(const Alloc&);
            template    <class  Alloc>  stack(const Container&, const   Alloc&);
            template    <class  Alloc>  stack(Container&&,  const   Alloc&);
            template    <class  Alloc>  stack(stack&&,  const   Alloc&);
            bool    empty() const;
            size_t  size()  const;
            T&  top();
            T   const&  top()   const;
            void    push(T  const&);
            void    push(T&&);
            void    pop();
            void    swap(stack&&);
    };
    

    相关文章

      网友评论

          本文标题:c/c++学习笔记3-基础关键字和要点

          本文链接:https://www.haomeiwen.com/subject/ygzovxtx.html