Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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