Last active
March 13, 2018 05:33
-
-
Save ohmiyaji/a1d0bb547d254c3c4fe279407e69d2df to your computer and use it in GitHub Desktop.
sanshou.cpp
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
//#include <stdio.h> | |
#include <string> | |
#include <iostream> | |
using namespace std; | |
class Hoge { | |
private: | |
string& s_; //隠蔽されたs_変数。mainのdataをいじりたいんで参照型。 | |
//文字列受け取るんでstring。 | |
public: | |
Hoge(string& s) : s_(s) {}//コンストラクタさんもmainのdataを | |
//いじりたいんで参照型。 | |
//文字列受け取るんでstring。初期化子コロンの右側では、 | |
//受け取ったら実態を隠蔽されたs_をsで初期化と。 | |
//代入ではなく初期化なので処理が速くなる。 | |
//なるほど、=は右辺優先なのと違い、コロンには左辺優先があるってことか。 | |
//参照は実態をいじるときは&を取ればいいからスッキリしてるってのがメリットか。 | |
void print() { | |
cout << "<" << s_ << ">" << endl; | |
//printf("<%s>\n", s_.c_str()); | |
//文字列を吐き出すのでstring関数のc_str()で変換。 | |
//< >の出力が楽になる。 | |
} | |
}; | |
int main() | |
{ | |
string data("abc"); | |
Hoge hoge(data); | |
hoge.print(); | |
data.clear(); | |
hoge.print(); | |
data += "def"; | |
hoge.print(); | |
data = "cde"; | |
hoge.print(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment