Skip to content

Instantly share code, notes, and snippets.

@goyusia
Last active October 21, 2015 15:48
Show Gist options
  • Select an option

  • Save goyusia/f2acbc34e22c14f06093 to your computer and use it in GitHub Desktop.

Select an option

Save goyusia/f2acbc34e22c14f06093 to your computer and use it in GitHub Desktop.
partial class in C++ (voodoo magic)
/*
Partial class in C++
This is voodoo magic. DO NOT USE IN YOUR CODE.
$ make run
## output
ctor
foo : 1
bar : 2
dtor
*/
#include "sample.h"
int main()
{
Sample sample;
sample.foo();
sample.bar();
return 0;
}
CXX = clang++
CXX_FLAGS = -W -Wall -std=c++11
all: sample.o partial_a.o partial_b.o main.o
clang++ $^ -o a.out $(CXX_FLAGS)
run: all
./a.out
sample.o: sample.cpp
$(CXX) -c $^ $(CXX_FLAGS)
partial_a.o: partial_a.cpp
$(CXX) -c $^ $(CXX_FLAGS)
partial_b.o: partial_b.cpp
$(CXX) -c $^ $(CXX_FLAGS)
main.o: main.cpp
$(CXX) -c $^ $(CXX_FLAGS)
clean:
rm -rf *.o
rm -rf a.out
#include "sample.h"
#include <cstdio>
void Sample::foo()
{
printf("foo : %d\n", foo_);
}
// sample :: partial
public:
void foo();
private:
const int foo_ = 1;
#include "sample.h"
#include <cstdio>
void Sample::bar()
{
printf("bar : %d\n", bar_);
}
public:
void bar();
private:
const int bar_ = 2;
#include "sample.h"
#include <cstdio>
Sample::Sample()
{
printf("ctor\n");
}
Sample::~Sample()
{
printf("dtor\n");
}
class Sample {
public:
Sample();
~Sample();
#include "partial_a.h"
#include "partial_b.h"
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment