Git Product home page Git Product logo

cpp_new_features's Issues

lambda使用错误:章节”C++11常用新特性(二)“

在 ”C++11常用新特性(二)“ 章节中,说道:
`class Filter
{
public:
Filter(int divisorVal):
divisor{divisorVal}
{}

std::function<bool(int)> getFilter() 
{
    return [=](int value) {return value % divisor == 0; };
}

private:
int divisor;
};`

这个类中有一个成员方法,可以返回一个lambda表达式,这个表达式使用了类的数据成员divisor。而且采用默认值方式捕捉所有变量。你可能认为这个lambda表达式也捕捉了divisor的一份副本,但是实际上并没有。因为数据成员divisor对lambda表达式并不可见,你可以用下面的代码验证:*******.


我验证过了, [=] 能捕获到 类数据成员,比如下面没问题:
`#include
#include

using namespace std;

class Demo {
public:
Demo():m_i(100) {}
virtual ~Demo() {}

void func(int x) {
auto f1 = [=]{ std::cout << &m_i << std::endl; return m_i + x; };
std::cout << f1() << std::endl;
std::cout << &m_i << std::endl;
}
std::function<int(int)> getFunc() {
return [=](int val) { return val + m_i; };
}
void setData(int d) {
m_i = d;
}

private:
int m_i;
};

int main() {
Demo d;
d.func(10);
std::function<int(int)> ff;
{
Demo dd;
ff = dd.getFunc();
dd.setData(400);
}
std::cout << "-------" << std::endl;
//
std::cout << ff(200) << std::endl;
return 0;

}`

long int

初级课程里面介绍 long int 是8字节,实际应该是 4字节

这是错别字吗

md里的“ACE : C++面向对象网络变成工具包”
是不是指 “编程工具包”?

7.7 指针和函数 输出最好修改一下哟

`int main() {

int a = 10;
int b = 20;
swap1(a, b); // 值传递不会改变实参

swap2(&a, &b); //地址传递会改变实参

cout << "a = " << a << endl;

cout << "b = " << b << endl;

system("pause");

return 0;

}修改成这样子会不会更好点呢?int main() {

int a = 10;
int b = 20;
swap1(a, b); // 值传递不会改变实参

cout << "swap1 a = " << a << endl;

cout << "swap1 b = " << b << endl;

swap2(&a, &b); //地址传递会改变实参

cout << "swap2 a = " << a << endl;

cout << "swap2 b = " << b << endl;

system("pause");

return 0;

}`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.