æ§é ä½ã®æé»ã®ã³ã³ã¹ãã©ã¯ã¿ï¼
ã©ããã¦ãé«éåãããå¦çããã£ã¦ä¹
ã
ã«C++ãæ¸ãã¦ãã¾ãã
ããæ¸ãã¦ãã¦ããï¼ã¨æããã¨ããã£ãã®ã§è¨äºãæ¸ãã¦ã¿ã¾ãã
ãµã³ãã«ã³ã¼ãã¯ä»¥ä¸ã®ãããªãã®ã§ãã
#include<iostream> #include<string> #include<map> struct point { int x; int y; }; void disp(const point& pt) { std::cout << "(" << pt.x << ", " << pt.y << ")" << std::endl; } int main(){ //garbage value point pt1; disp(pt1); // (0, 0) std::map<std::string, point> m; disp(m["hoge"]); // (0, 0) implicit constructor? point pt2 = point(); disp(pt2); // (0, 0) initialize point pt3 = {}; disp(pt3); return 0; }
//garbage value
point pt1;
disp(pt1);
ãããã´ãã表示ããã®ã¯Cã®æè¦ã§è¨ãã°ç´å¾ã§ãã
ã¨ããã
// (0, 0) std::map<std::string, point> m; disp(m["hoge"]);
ãªãã¨ãã㯠"(0, 0)" ã¨è¡¨ç¤ºãããã®ã§ããã
std::map ã® operator[] ã¯ããã¼ã«å¯¾å¿ããã¨ã³ããªãåå¨ããªãå ´åã«ã¯ããã©ã«ãã³ã³ã¹ãã©ã¯ã¿ã§ãªãã¸ã§ã¯ããä½ãçãªç解ããã¦ãã¾ããããpt1ã®ã±ã¼ã¹ã¨æåãç°ãªãããã§ãã
ãã£ï¼ã¨æã£ã¦ä½¿ã£ã¦ããã³ã³ãã¤ã©ã®STLã®mapã®ã½ã¼ã¹ãªã©ãèªãã§ã¿ããè²ã
試ãã¦ããã¨ãã©ãã
// (0, 0) implicit constructor?
point pt2 = point();
disp(pt2);
ãã®ãããªæ¸ãæ¹ã§ã¯ "(0, 0)"ã«åæåããããããªã®ã§ããã
C++ã®æ§é ä½ã¯publicãããã©ã«ãã§ãã以å¤ã¯classã¨åãã¨ããã®ã¯ç¥ã£ã¦ãã¾ãã
ã¨è¨ããã¨ã¯Cçãªè¨è¿°ã§æ§é ä½ã宣è¨ããå ´åã¯ãã³ã³ãã¤ã©ãæé»ã®ã³ã³ã¹ãã©ã¯ã¿ããã³ãã¼ã³ã³ã¹ãã©ã¯ã¿ãªã©ãä½ã£ããããã®ã ããã¨ããæãè¾¼ãã§ããã®ã§ãããããã pt1 ã®æ¸ãæ¹ã¨ pt2 ã®æ¸ãæ¹ã¯ç価ã§ããã¨æã£ã¦ãã¾ããã
ã¾ããã ã以ä¸ã®ãããªæ¸ãæ¹(ãããpt1ãpt2ã¨ç価ã§ããã©ã«ãã³ã³ã¹ãã©ã¯ã¿ãå¼ã°ããã¨æã£ã¦ãã¾ãã)ã¯ã³ã³ãã¤ã«ã¨ã©ã¼ã«ãªã£ãã®ã§ä½ãåéããã¦ããã®ããããã¾ããã
// compile error
point pt4();
disp(pt4);
æ®éã§ããã° class ã«ãã¦ã³ã³ã¹ãã©ã¯ã¿ãæ示çã«ä¸ããã¹ããªã®ã§ãããããé«éåãç®çãªã®ã§å¯è½ã§ãããã¤ä»æ§ã«åã£ãæåã§ãããªãã°ä½åãªãã¨ãæ¸ããã«æ¸ã¾ãããã®ã§ãããããã¯ãã»ã»ã»
ããã°ã©ãã³ã°è¨èªC++ããã£ã¨çºããã ãã§ã¯ããã®æåãæ£å½åããè¨è¿°ã¯è¦ã¤ãããã¾ããã§ãããã¨ãªãã¨C++ã®ä»æ§æ¸ãèªãã¹ããªã®ã§ããããããã¾ãèªã¿ãããªãããèªãã§ãå¦çç³»ãä»æ§ã«åã£ã¦ãããã©ãããå®ãã§ã¯ãªãã¨ãããã