Wednesday, 11 September 2013

C++ calling objects from another class

C++ calling objects from another class

I asked this question here: C++ Method chaining with classes
In essesance, what I am trying to do is call a Constructor/Method from
another class using Method chaining. Let's say I have 2 classes:
class Signal {
public:
Signal() { } // constructor
Signal& ParseSignal() {
// In this method I want to call
// the constructor "Parse()"
}
protected:
std::vector<double> data;
};
And I have another class called Parse:
class Parse {
public:
Parse() {
// This is the implementation
// I need to access the "data" contained in class "Signal
};
My main objective would be to do the following in main:
Signal s = Signal().ParseSignal();
This would then accept the signal, and, Parse this.
Someone suggested that I should use CRTP however, due to the fact that the
base class (in this case Signal) has to have a template<> argument, this
is not possible due to other classes inheriting.
Is there another solution to this problem?
EDIT:
I have tried the following, however, it looks like a dirty implementation
and I cannot access the member variable:
class Parser {
public:
Parser() {
parse();
}
void parse() {
cout << "YES";
}
};
class Signal {
public:
friend class Parser;
Signal() { val = 0;}
Signal& Parse() {
Parser::Parser();
return *(this);
}
protected:
int val;
};

No comments:

Post a Comment