Skip to content

Instantly share code, notes, and snippets.

@dskkato
Created March 9, 2019 14:25
Show Gist options
  • Save dskkato/814e5685b61474342d5e30c9dfe2853b to your computer and use it in GitHub Desktop.
Save dskkato/814e5685b61474342d5e30c9dfe2853b to your computer and use it in GitHub Desktop.
#include "mystruct.h"
#include <iostream>
#include <string>
using mystruct::MyStruct;
using std::string;
int main() {
const string name("hoge");
constexpr unsigned char age = 8;
MyStruct m(name, age);
std::cout << "Hello, " << m << std::endl;
}
.PHONY: all clean
all: a.out
./a.out
clean:
rm a.out
a.out: main.cpp mystruct.h mystruct.cpp
@echo "Compiling..."
$(CXX) -std=c++14 main.cpp mystruct.cpp -o a.out \
-Wall -Wextra
#include "mystruct.h"
namespace mystruct {
using std::ostream;
using std::string;
MyStruct::MyStruct(const string &name, const unsigned char age)
: name(name), age(age){};
ostream &operator<<(ostream &os, const MyStruct &m) {
os << "(" << m.name << ", " << static_cast<unsigned int>(m.age) << ")";
return os;
}
} // namespace mystruct
#ifndef _MYSTRUCT_H_
#define _MYSTRUCT_H_
#include <iostream>
#include <string>
namespace mystruct {
class MyStruct {
public:
MyStruct() = delete;
MyStruct(const std::string &, const unsigned char);
friend std::ostream &operator<<(std::ostream &, const MyStruct &);
private:
const std::string name;
const unsigned char age;
};
} // namespace mystruct
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment