揮発性のメモ2

http://d.hatena.ne.jp/iww/

C++

呼び出しているライブラリと関数の一覧

nmでシンボルを抜き出して、フィルタをかけるMan page of nm nm -C hogehoge C C++ の関数名を可読にする nm コマンド – オブジェクトファイルのシンボル情報を表示する | Linuxコマンド.NET 呼び出してる関数⇒自分じゃ定義していない関数⇒未定義シンボル と…

error: cannot bind packed field 'a.hoge_t::n' to 'int&'

C++

パックした構造体のメンバーは、参照で利用できない。 #include <iostream> using namespace std; struct hoge_t{ int n; } __attribute__((__packed__)); void setan( int &n ){ n = 123; } int main() { hoge_t a; setan( a.n ); // ★コンパイルエラー cout << a.n <</iostream>…

構造体に関数を書く

C++

#include <iostream> using namespace std; typedef struct { int a; int b; int getA(){ return a; } int getB(){ return b; } } hoge_t; int main() { hoge_t aa = {}; aa.a = 123; cout << aa.getA() << endl; // 123 cout << aa.getB() << endl; // 0 return 0; }</iostream>

コンストラクタ、デストラクタが呼ばれる順番

C++

#include <iostream> using namespace std; class C { public: C(){ cout << "C start" << endl; } virtual ~C(){ cout << "C end" << endl; } }; class B { public: B(){ cout << "B start" << endl; } virtual ~B(){ cout << "B end" << endl; } }; class A { public</iostream>…

お互いがお互いをメンバーに持つクラス

C++

class BBB; class AAA { public: BBB *pb; // BBB b; // ←ここでインスタンスはつくれません int c; }; class BBB { public: AAA *pa; AAA a; // ←ここで持つのはセーフ int c; }; ポインタなら持てるけど、実体は持てません(コンパイルでエラー)

コンストラクタの中から仮想関数は呼べない

C++

#include <iostream> using namespace std; class hoge { public: int a; hoge(){ cout << "hoge" << endl; init(); } virtual void init(){ a = 123; } }; class piyo: public hoge { public: /* piyo(){ cout << "hoge" << endl; init(); } */ virtual void init(){ </iostream>…

デフォルトコンストラクタが呼ばれない

C++

#include <iostream> using namespace std; class hoge { public: hoge(){ cout << "hello"; } void run(char *text){ cout << text; } }; class piyo { public: piyo(int a){ cout << a << "world"; } void run(char *text){ cout << text; } }; int main() { hoge a(</iostream>…

char* A[] = {"Hello"};

C++

#include <iostream> int main() { char* A[] {"Hello"}; // =を書き忘れた printf("%s\n",A[0]); return 0; } C++11だとコンパイル通る。</iostream>

C++で引数のところでオブジェクトを作って渡す

C++

一時的にオブジェクトを作って渡すとき、C++ならいちいちnewとかせずに引数の中で使い捨てできる。 #include <stdio.h> class Pos_t { public: int x; int y; }; void show(const Pos_t &A) { printf("X=%d\n",A.x); printf("Y=%d\n",A.y); } int main() { printf("初</stdio.h>…

shared_ptrを返す

C++

http://d.hatena.ne.jp/iww/20120705/shared_ptr から改造してみる #include <iostream> #include <boost/shared_ptr.hpp> using namespace std; using namespace boost; class Hoge { private: Hoge(){} public: void echo(){ cout << "HELLO" << endl; } virtual ~Hoge(){ cout << "BYE" <<</boost/shared_ptr.hpp></iostream>…

ソートされないmultimap

C++

もうmapでもなんでもない #include <iostream> #include <vector> #include <string> using namespace std; int main(){ typedef pair<string,int> P; typedef vector<P> M; typedef M::iterator MIT; M list; list.push_back( P( "hoge", 1 ) ); list.push_back( P( "piyo", 2 ) ); list.push_back( P</p></string,int></string></vector></iostream>…

multimapで特定のキーのイテレータを取得する

C++

http://www.geocities.jp/ky_webid/cpp/library/020.html equal_range()を使って、ソート済み配列の先頭と末尾のイテレータを貰う。 書くことが多くて面倒くさい。

相互参照は実体ダメ

C++

class B; class A{ public: B b; // ←error: field ‘b’ has incomplete type B *pb; }; class B{ public: A a; A *pa; };

C++でatoi

C++

atoi()を使う #include <sstream> #include <iostream> #include <cstdlib> // ★← stdlib.hは使わない int main() { int a; string b = "aiueo"; a = atoi(b.c_str()); cout << a << endl; return 0; }</cstdlib></iostream></sstream>

constについて

C++

Hoge* a 制限なし constなポインタをつっこめない。 const Hoge* a データ(ポインタの先)が書き換え不可 a->bar = 1 は余裕でアウト a->foo() ってやるときは、foo()がconstじゃないとダメ Hoge* const a ポインタが書き換え不可 a++;とか出来ない。 const…

stringを継承する

C++

コンストラクタと代入演算子は継承されないので、親を呼ぶだけでもいいからちゃんと作る。 using namespace std; #include <string> class mystring : public string { public: mystring() : string() {} mystring(const char *str) : string(str) {} mystring(const</string>…

クラス名を表示する

C++

typeid演算子 - ゲームが作れるようになるまでがんばる日記 ふつうなら typeid(*p).name() で済むんだけど、gccとかだとゴミが付くのでそれをなんとかするという話 #include <iostream> #include <typeinfo> // classname用 #include <cxxabi.h> // classname用 using namespace std; class</cxxabi.h></typeinfo></iostream>…

gdbで文字列を表示する

C++

stringを見る (gdb) p a.c_str() $1 = 0x6131f8 "HOGEHOGE"

標準入力を文字列に入れる

C++

普通にやると改行で区切られ、行単位でしか取り込めない。改行コードも無くなる。 cin >> a; cout << "---" << a << "---" << endl; $ ./a.out hoge ←入力 ---hoge--- ←出力

shared_ptr

C++

letsboost::shared_ptr #include <iostream> #include <boost/shared_ptr.hpp> using namespace std; class Hoge { public: void echo(){ cout << "HELLO" << endl; } virtual ~Hoge(){ cout << "BYE" << endl; } typedef boost::shared_ptr<Hoge> Ptr; }; int main() { { Hoge::Ptr a; a = Hoge::P</hoge></boost/shared_ptr.hpp></iostream>…

リストを作って削除する

C++

listじゃなくてvectorだけど。 #include <iostream> #include <vector> using namespace std; class Hoge { public: int number; virtual ~Hoge(){ cout << number << endl; } typedef vector<Hoge*> List; typedef vector<Hoge*>::iterator Iterator; }; class Piyo : public Hoge { public:</hoge*></hoge*></vector></iostream>…

実体をまるごと返すとキャストされる

C++

#include <string> #include <iostream> using namespace std; class Hoge { public: virtual string to_str(){ return "hoge"; } }; class Piyo : public Hoge { public: virtual string to_str(){ return "piyo"; } }; Hoge *getp() { return new Piyo(); } Hoge get() { Piy</iostream></string>…

variant型

C++

int main() { Var a; a = "abc"; cout << a << endl; a = 1234; cout << a << endl; return 0; } $ ./a.out abc 1234

error: multiple types in one declaration

C++

「どれかのclass宣言の後ろに ; がついていない」というエラー