1

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 the fly.

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);
        }
    }
}

I recognize that I could define the function like std::function<rgb(int,&subclass)> and then pass this as a variable, but it can be entirely transparent when using [this] in the lambda definition

  • `[](subclass* ptr) { return [ptr](int x) { return /**/; } }` closest compromise i can think of ([full code](https://godbolt.org/z/hvnG713Y4)) – Borgleader Jul 02 '22 at 02:20
  • *"I have a class for holding a list of rgb colors in a particular way"* -- your code does not show such a class. If this class is relevant to the question, it should probably be in your code snippet. If it is not needed for your code snippet to be complete, then there is probably no need to mention it in your question. – JaMiT Jul 02 '22 at 02:20
  • *"a class that inherits the original,"* -- there is no inheritance demonstrated in your code, so it is difficult to see how your code connects to the textual description. That being said, based on the text, inheritance seems like a not-good choice, so it might be the text that should be revised instead of the code. *(It would be odd for a class that does not store a list of colors to at the same time be a class that does store a list of colors -- "A inherits from B" should be used to model "an A object is at the same time a B object".)* – JaMiT Jul 02 '22 at 02:23
  • I'm going to dump [Rule of Three](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) and [What is a smart pointer](https://stackoverflow.com/questions/106508/what-is-a-smart-pointer-and-when-should-i-use-one) here. Since I see a `subclass*` in your code, I suspect you'll be needing one or both of those links in a few days. – Silvio Mayolo Jul 02 '22 at 04:06

0 Answers0