C++新特性学习笔记

new feature
VS Support For C++11/14/17 Features (Modern C++)

1.auto:自动类型(deduce actual type from its initializer)

#include <map>
#include <vector>

int myAdd(int x, int y)
{
    return x + y;
}
typedef int (*MYADDFUN)(int, int);

void feature_auto_test()
{
    auto a = 8LL;
    auto f = new MYADDFUN;
    *f = myAdd;
    auto b = (*f)(1, 2);
    TRACE(_T("%d\n"), b);

    std::map<int, int> map;
    map.insert(std::make_pair(11, 1));
    map.insert(std::make_pair(21, 2));
    for (auto it = begin(map); it != end(map); ++it)
    {
        TRACE(_T("(%d, %d)\n"), it->first, it->second);
    }
}

2.nullptr:可以给指针类型和bool类型,但不可以给intigral type

#include <memory>
int fun1(std::shared_ptr<int> p)
{
    if (nullptr == p)
        return 1;
    return *p + 1;
}
void feature_nullPtr_test()
{
    int* p1 = NULL;
    int* p2 = nullptr;
    if (p1 == p2)
    {
        TRACE(_T("equal\n"));
    }

    TRACE(_T("%d\n"), fun1(nullptr));

    bool b = nullptr;
    if (!b)
    {
        TRACE(_T("false\n"));
    }
    int p3 = nullptr;//error: A native nullptr can only be converted to bool or,
    //using reinterpret_cast, to an **integral type**
}

3.Range-based for loops:collection/array

void feature_rangeBasedForLoop_test()
{
    std::map<std::string, std::vector<int>> map;
    std::vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    map["one"] = v;

    for (const auto& item : map)
    {
        TRACE("%s:\n", item.first.c_str());
        for (auto v : item.second)
        {
            TRACE("  %d\n", v);
        }
    }

    int arr[] = { 1,2,3 };
    for (int& e : arr)
    {
        TRACE("%d\n", e * e);
    }
}

4.identifier - overwrite : indicate that a method is supposed to be an override of a virtual method in a base class 如果基类没有完全相同的虚函数,就会报错

class B 
{
public:
   virtual void f(short) {std::cout << "B::f" << std::endl;}
};

class D : public B
{
public:
   virtual void f(int) override {std::cout << "D::f" << std::endl;}//error: 
   //'D::f' : method with override specifier 'override' did not override any 
   //base class methods
};

5.identifier - final : indicate that a derived class shall not override a virtual method 限制方法被派生类重载

class B 
{
public:
   virtual void f(int) {std::cout << "B::f" << std::endl;}
};

class D : public B
{
public:
   virtual void f(int) override final {std::cout << "D::f" << std::endl;}
};

class F : public D
{
public:
   virtual void f(int) override {std::cout << "F::f" << std::endl;}
};
D::f重载了B::f, 但因为D::f有final, 所以F::f没有重载基类的方法。

6.Strongly-typed enums : enum class

传统的枚举是没有封装起来的,可以直接访问,而enum class则不再直接暴露枚举值

enum class Weekdays{Monday,Tuesday,Wednesday};
Weekdays ev = Weekdays::Tuesday;

7.Smart pointers : unique_ptr/shared_ptr/weak_ptr

  • unique_ptr: should be used when ownership of a memory resource does
    not have to be shared (it doesn’t have a copy constructor), but
    it can be transferred to another unique_ptr (move constructor
    exists).

  • shared_ptr: should be used when ownership of a memory resource should
    be shared (hence the name).

  • weak_ptr: holds a reference to an object managed by a shared_ptr, but
    does not contribute to the reference count; it is used to break
    dependency cycles (think of a tree where the parent holds an owning
    reference (shared_ptr) to its children, but the children also must
    hold a reference to the parent; if this second reference was also an
    owning one, a cycle would be created and no object would ever be
    released).

7.1.Smart pointer : std::unique_ptr/std::move/std::unique_ptr.get()

disscusion

int fun2(int* p)//raw pointer
{
    if (nullptr == p)
        return 1;
    return *p + 1;
}
void feature_uniquePtr_test()
{   //std::unique_ptr  //smart pointer
    //std::unique_ptr.get() //observing pointer(raw pointer)
    //std::move     //transfer ownership
    std::unique_ptr<int> p1(new int(42));
    std::unique_ptr<int> p2 = std::move(p1); 

    int nRet = 0;
    if (p1)
    {
        nRet = fun2(p1.get());
    }

    (*p2)++;

    if (p2)
    {
        nRet = fun2(p2.get());
    }
}

7.2.Smart pointer : std::shared_ptr/std::make_shared

int fun3(int* p)//raw pointer
{
    if (nullptr == p)
        return 0;

    (*p)++;

    return *p;
}
int fun4(std::shared_ptr<int> p)
{
    if (nullptr == p)
        return 0;

    (*p) *= 2;

    return (*p);
}
void feature_sharedPtr_test()
{
    std::shared_ptr<int> p1(new int(88));//auto p1 = std::make_shared<int>(88);
    std::shared_ptr<int> p2 = p1;
    int nRet = fun3(p1.get());//89
    nRet = fun4(p2);//178
}
make_shared<T> is a non-member function and has the advantage of allocating memory 
for the shared object and the smart pointer with a single allocation, 
as opposed to the explicit construction of a shared_ptr via the contructor, 
that requires at least two allocations. In addition to possible overhead, 
there can be situations where memory leaks can occur because of that.

7.3.Smart pointer : std::weak_ptr/lock/expired/reset

void feature_weakPtr_test()
{
    auto p = std::make_shared<int>(88);

    std::weak_ptr<int> wp = p;

    auto sp1 = wp.lock();//convert to shared_ptr
    TRACE(_T("%d\n"), *sp1);
    wp.reset(); //release resource, convert to null weak_ptr object
    if (wp.expired())//return true if resource no longer exists
    {
        TRACE(_T("expired\n"));
        auto sp2 = wp.lock();//lock on an expired weak_ptr return empty shared_ptr
        if (nullptr == sp2)
        {
            TRACE(_T("nullptr\n"));
        }
    }
}

8.Lambdas

Lambda Expression Syntax

9.std::begin()/std::end()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!
很高兴回答你关于野火嵌入式Linux学习的问题!以下是一些学习笔记的建议: 1. 了解嵌入式系统:首先,你需要了解嵌入式系统是什么以及它们与桌面操作系统的区别。嵌入式系统通常用于特定的应用领域,例如智能家居、汽车电子和工业控制等。 2. 学习Linux基础知识:野火嵌入式Linux是基于Linux内核的操作系统,所以你需要掌握Linux的基础知识,包括文件系统、进程管理、设备驱动程序等。 3. 硬件平台了解:野火嵌入式Linux有不同的硬件平台,例如野火开发板。你需要学习如何操作和配置这些硬件平台,并了解它们的特性和限制。 4. 交叉编译环境设置:为了在PC上开发嵌入式系统,你需要设置一个交叉编译环境,以便能够编译和调试嵌入式应用程序。这涉及到安装和配置交叉编译工具链。 5. 内核定制和驱动程序开发:学习如何定制Linux内核以满足特定需求,并开发设备驱动程序以支持外部硬件。 6. 应用程序开发:掌握嵌入式应用程序的开发技术,包括使用C/C++语言、Makefile和调试工具。 7. 调试和故障排除:学会使用调试工具和技术来定位和解决嵌入式系统中的问题。 8. 实际项目经验:通过参与实际的嵌入式项目或完成一些小型项目来应用你的知识和技能。 这些只是一些学习笔记的建议,野火嵌入式Linux学习需要不断的实践和探索。希望这些对你有帮助!如果你有任何进一步的问题,欢迎继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值