繁体   English   中英

在 C++ 中,是否可以在 class 的构造函数中包含 lambda 表达式,该表达式可以访问 class

[英]In C++, is it possible to include a lambda expression in the constructor for a class that has access to the members of that class?

I have a class for holding a list of rgb colors in a particular way, and I'm interested in making a class that inherits the original, but instead of storing all the colors, trying to access the list runs a function to generate one on苍蝇。

struct rgb {unsigned char r,g,b};
subclass {
    std::function<rgb(int)> create_color;
    public: 
    subclass(std::function<rgb(int)> job){ 
        create_color = job;
    }
    get_color () {
    
    }
};
superclass {
    subclass* colorlist;
    public:
    superclass () {
        colorlist = new subclass([this] (int x) { //would like some way for "this" to be the subclass object
            return hsv_to_rgb(x % 360, 1.0, 1.0);
        }
    }
}

我认识到我可以像 std::function<rgb(int,&subclass)> 一样定义 function 然后将其作为变量传递,但是在 lambda 定义中使用 [this] 时它可以完全透明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM