Created
July 22, 2014 19:26
-
-
Save tolmasky/f2f79c7e56472bd47eb7 to your computer and use it in GitHub Desktop.
static strategy 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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