Hi, I remember from when I learned PHP, how easy and fun OOP was, and how to call member functions in conjuction (after eachother) on the same line, like this:

MyClass->Foo()->Bar()->Pancakes("flour etc");

To be able to do that, all you needed to do was make a return holding the class itself, like this:

class MyClass{
	function Foo(){
		echo "Inside Foo";
        return $this;
	}
	function Bar(){
		echo "Inside Bar";
        return $this;
	}
	function Pancakes($ingredients){
		echo "To make pancakes you need: "+$ingredients;
        return $this;
	}
}

My questions are:

  1. Is this achievable in C++?
  2. How would it look like; an example?
  3. Is there another way to do this?
  4. Are these approaches effective and useful?
  5. Recommendations, suggestions?

It's not required to answer all of them, just answer the ones you want. :)

Here's how I think it could look like:

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

class MyClass{
	static MyClass myclass;
	MyClass Foo();
	MyClass Bar();
	MyClass Pancakes(string ingredients);
};

MyClass MyClass::Foo(){
	cout<<"Inside foo";
	return this->myclass;
}

MyClass MyClass::Bar(){
	cout<<"Inside bar";
	return this->myclass;
}

MyClass MyClass::Pancakes(string ingredients){
	cout<<"To make pancakes you need: "+ingredients;
	return this->myclass;
}

int main(){
	MyClass mc;
	MyClass *ptr = &mc;
	ptr->Foo()->Bar()->Pancakes("flour etc");
	return 0;
}

Although I am quite sure this will give an error, I would at least approach it like so.
One other method doing this could be: creating an additional function with a string parameter that read and seperate each function for every -> or something.

Since I can't continue editing, and I don't really know the rules about 'double posting', I'll just add this and remove it if it's against them.

I think this is how you do it. All it needed was a static MyClass.

Here take a look:

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

class MyClass{
   public: 
	static MyClass myclass;
	MyClass Foo();
	MyClass Bar();
	MyClass Pancakes(string ingredients);
};

MyClass MyClass::Foo(){
	cout<<"Inside foo"<<endl;
	return this->myclass;
}

MyClass MyClass::Bar(){
	cout<<"Inside bar"<<endl;
	return this->myclass;
}

MyClass MyClass::Pancakes(string ingredients){
	cout<<"To make pancakes you need: "+ingredients<<endl;
	return this->myclass;
}

int main(){
	MyClass mc;
	mc.Foo().Bar().Pancakes("flour etc.");
	return 0;
}

Suggestions?
Opinions?
Ideas?

In my opinion, this technique is fun to use, but it isn't really necessary, is it?
Is it easy to read?
Is it cleaner?
Is it effective?

What if I want to return a value, do I attach it to the static MyClass or is it not possible?

you are close

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

class MyClass{
public:
	MyClass Foo();
	MyClass Bar();
	MyClass Pancakes(string ingredients);
};

MyClass MyClass::Foo(){
	cout<<"Inside foo\n";
	return *this;
}

MyClass MyClass::Bar(){
	cout<<"Inside bar\n";
	return *this;
}

MyClass MyClass::Pancakes(string ingredients){
	cout<<"To make pancakes you need: " << ingredients << "\n";
	return *this;
}

int main(){
	MyClass mc;
	mc.Foo().Bar().Pancakes("flour etc.");
	return 0;
}

Oh, I don't have to create a static Myclass?
Didn't know you could use this like *this :P
Ah well, I updated my recent post.

What do you think of this technique, is it useful, I mean, when you don't need to return something from a method?
Is it possible to return a value using this technique?

By the by, is this how they structured PHP?

>>What do you think of this technique, is it useful
I never used or seen that technique -- I would have just done this:

mc.Foo();
mc.Bar();
mc.Pancakes("flour etc.");

>>Didn't know you could use this like *this
That is a very useful technique to remember because its use a lot, especially when you want to override the << and >> operators.

>>By the by, is this how they structured PHP?
No idea.

So, now that you've seen it, would you use it, or rather stick with the conventional methods?

I would stick with the conventional method. Why? Because it makes the program easier to read and understand. And the next guy who has to maintain your program will hate you for that code. Use the KISS principal (Keep It Simple Stupid)

Haha, thanks for your input. I'll especially remember the KISS principle!

<marked as solved>

This question has already been answered. Start a new discussion instead.