Skip to content

Instantly share code, notes, and snippets.

@tolmasky
Created July 22, 2014 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tolmasky/f2f79c7e56472bd47eb7 to your computer and use it in GitHub Desktop.
Save tolmasky/f2f79c7e56472bd47eb7 to your computer and use it in GitHub Desktop.
static strategy 1
// Strategy 1: No typecast magic
//
// To compile and run:
//
// $ g++ -W -Wall -Wextra -pedantic -std=c++0x -o static static.cpp && ./static
#include <string>
#include <iostream>
class G
{
private:
const int o_count;
public:
G(int anAmount = 1)
:o_count(anAmount)
{
}
G operator() (void)
{
return G(o_count + 1);
}
const char * operator() (const char * aPostfix)
{
int length = 1 + o_count + strlen(aPostfix) + 1;
char * string = new char[length];
string[0] = 'g';
string[length - 1] = '\0';
memset(string + 1, 'o', o_count);
memcpy(string + 1 + o_count, aPostfix, strlen(aPostfix));
return string;
}
};
// Special case g("al")
std::string g(const char * aPostfix)
{
return std::string("g") + aPostfix;
}
G g()
{
return G();
}
int main()
{
const char * result = g()()("al");
std::cout << result << std::endl;
std::cout << g("al") << std::endl;
std::cout << g()()("al") << std::endl;
std::cout << g()()()()()()()("al") << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment