I basically have two classes: Signal and Parser they both have differences, however, they each share the data that Signal has.

I want to incorperate Method Chaining into the class, so my classes (at the moment look like the following):

class Signal {

    Signal() { 

    }

    Signal& Parse()
    {
       return *(this);
    }
};

And my other class looks like the following:

class Parse : public Signal {

    Parse() {

    }
};

This will therefore allow me to do the following in main: Signal s = Signal().Parse();

But what I need to do is in the class Signal when Parse is called, initialise a new object of the class Parse so it can do it's "business". The class constructor in Parse will use the data in Signal.

I know this is possible using CRTP, however, using CRTP would require me to have a template method for Signal, and, I cannot have this due to other classes inheirting from Signal.

Anyone know of an alternative to solving such problem?

Thanks

Recommended Answers

All 2 Replies

Came up with a solution:

#include <iostream>

using namespace std;

class Parser {

    public:

        Parser() { cout << "Parser constructor "; }
};

class Signal {

    public:
        friend class Parser; 
        Signal() { }

        Signal& Parse() {
            Parser* p = new Parser();
            return *(this);
        }
    protected:

};


int main(int argc, char *argv[]) {

    Signal s = Signal().Parse();
}

Probably not the best implementation, but, hey! If anyone has any more, feel free to post :)

Probably not the best implementation, but, hey! If anyone has any more, feel free to post :)

Definitely not, especially considering that it has a blatant memory leak.

Overall, I have a really hard time understanding what you actually want to accomplish. Even the title is confusing "CRTP without template arguments", in other words, "Curiously Recurring Template Pattern without templates", normal people would just call this "inheritance".

If what you want to do is somehow return a reference to an instance of Parser class via the Signal::Parse() function, and then copy that into a Signal object, then all you are doing is slicing an object. It doesn't "feel" right, to say the least.

I've read your posts over and over and I really can't understand what you want to the behavior to be. It is hard to understand behavior when all the functions are empty and thus, whatever happens, the effect is naught. Please provide a more concrete example, with data and observable behavior.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.