Skip to content

Instantly share code, notes, and snippets.

@KentaYamada
Last active September 3, 2016 04:46
Show Gist options
  • Save KentaYamada/844a523b7de07ee723ad159644c837f2 to your computer and use it in GitHub Desktop.
Save KentaYamada/844a523b7de07ee723ad159644c837f2 to your computer and use it in GitHub Desktop.
ユニットテストかそうでないかでアクセシビリティを切り替える
#include "hoge.h"
Hoge::Hoge()
{}
void Hoge::greeting(std::string name)
{
std::cout << "Hello, " << name << std::endl;
}
void Hoge::hello_greeting(std::string name)
{
this->greeting(name);
}
#ifndef __HOGE_H__
#define __HOGE_H__
#include <iostream>
#include <string>
class Hoge
{
#ifdef TEST
public:
#else
private:
#endif // end of TEST
void greeting(std::string name);
public:
Hoge();
void hello_greeting(std::string name);
};
#endif // end of file
#include "hoge.h"
int main(void)
{
//MakefileでTESTを定義しないので、greeting関数を呼び出そうとするとコンパイルエラー
auto hoge = new Hoge();
hoge->hello_greeting("hoge");
delete hoge;
return 0;
}
CXX=g++
CFLAGS=-Wall -O2
main: main.o hoge.o
$(CXX) $(CFLAGS) main.o hoge.o -o main
test: test_main.o hoge.o
$(CXX) $(CFLAGS) test_main.o hoge.o -o test
test_main.o: test_main.cpp hoge.h
$(CXX) -D "TEST" -c test_main.cpp
main.o: main.o hoge.h
$(CXX) -c main.cpp
hoge.o: hoge.cpp hoge.h
$(CXX) -D "TEST" -c hoge.cpp
clean:
$(RM) *.o test_main main
#include "hoge.h"
int main(void)
{
std::string name = "hoge";
auto hoge = new Hoge();
//MakfileでTESTを定義するから外部から見えるようになる
hoge->greeting(name);
hoge->hello_greeting(name);
delete hoge;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment