Skip to content

Instantly share code, notes, and snippets.

@krysseltillada
Created July 10, 2015 02:55
Show Gist options
  • Save krysseltillada/3d0c655304f48f93d66a to your computer and use it in GitHub Desktop.
Save krysseltillada/3d0c655304f48f93d66a to your computer and use it in GitHub Desktop.
function methods in series of calls
#include <iostream>
#include "Sample_c.h"
int main()
{
Sample_c cl1("kryssel");
cl1.f1('a').display().f2('b').display().f3('c').display().f4('d'); /// this would call first left most to rightmost functions
/// how? because f1 returns *this which is cl1 and other functions too and returns a reference which is that object
/// not a copy of cl1 which is annoying
return 0;
}
#include <iostream>
#include <string>
#include "Sample_c.h"
Sample_c::Sample_c (std::string dat) :
data_member1 (dat) { }
Sample_c &Sample_c::f1 (char c1)
{
this->data_member1 = c1;
return *this;
}
Sample_c &Sample_c::f2 (char c2)
{
this->data_member1 = c2;
return *this;
}
Sample_c &Sample_c::f3 (char c3)
{
this->data_member1 = c3;
return *this;
}
Sample_c &Sample_c::f4 (char c4)
{
this->data_member1 = c4;
return *this;
}
Sample_c &Sample_c::display ()
{
std::cout << this->data_member1 << std::endl;
return *this;
}
#ifndef SAMPLE_C_H
#define SAMPLE_C_H
#include <iostream>
#include <string>
class Sample_c
{
public:
Sample_c () = default;
Sample_c (std::string);
Sample_c &f1 (char);
Sample_c &f2 (char);
Sample_c &f3 (char);
Sample_c &f4 (char);
Sample_c &display ();
private:
std::string data_member1 = "sample";
};
#endif // SAMPLE_C_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment