C++

32ビット固定小数3次元ベクトルクラス

C++

#ifndef VECTOR3FIX_H #define VECTOR3FIX_H #include <iostream> #include <math.h> class Vector3fix { public: int x; int y; int z; Vector3fix() : x(0), y(0), z(0) {} Vector3fix(int x, int y, int z) : x(x << 16), y(y << 16), z(z << 16) {} Vector3fix(float x, fl</math.h></iostream>…

32ビット単精度浮動小数3次元ベクトルクラス

C++

#ifndef VECTOR3F_H #define VECTOR3F_H #include <iostream> #include <math.h> class Vector3f { public: float x; float y; float z; Vector3f() : x(0.0f), y(0.0f), z(0.0f) {} Vector3f(float x, float y, float z) : x(x), y(y), z(z) {} Vector3f(const Vector3f& star</math.h></iostream>…

new演算子の例外処理

C++

new演算子が投げるbad_alloc例外を捕獲する。 int main(void) { double *p; do { try { p = new double[10000]; } catch (bad_alloc ba) { cout << "Memory allocation failed." << endl; return 1; } } while(p); }

C++でSingletonパターンの実装

C++

・・・をしてみた。 #include <iostream> using namespace std; class Singleton { private: Singleton() {} Singleton(const Singleton& s) {} Singleton& operator = (const Singleton& s) {} public: static Singleton* getInstance() { static Singleton instance;</iostream>…

「とりあえずオブジェクトを宣言しといて、インスタンス化するのは後回し」みたいなことをやる

C++

#include <iostream> class Hoge { public: int x, y; Hoge(int i) { x = i; y = i * i; } }; int main(void) { Hoge *hoge; hoge = new Hoge(3); cout << hoge->x << hoge->y; } C++で、Javaのような「とりあえずオブジェクトを宣言しといて、インスタンス化するのは</iostream>…

C++でJavaのMathクラスみたいなやつを

C++

とりあえずここまで作った #ifndef UTILMATH_H #define UTILMATH_H #include <time.h> class UtilMath { public: UtilMath() { srand(time(NULL)); // 乱数の種を生成 } const static float pi = 3.1415926535f; float toDegrees(float radians) { return radians * </time.h>…

インスタンス化のときの注意

C++

/* Vector3.h */ class Vector3 { public: float x, y, z; Vector3(); }; /* Vector3.cpp */ #include "vector3.h" Vector3::Vector3() { this->x = 0.0f; this->y = 0.0f; this->z = 0.0f; } /* main.cpp */ { Vector3 vec; // ←できる Vector3 vec(); // ←…

C++

.../iostream : error :expected unqualified-id before "namespace" クラス記述のうしろにセミコロンを忘れてると出る

C++

C++の演算子のオーバーロードは、使う側は楽だけど、作る側ははげしく面倒くさい Javaに慣れてるせいか、可読性もよくない気がするし

イテレータ

C++

の使い方。 for (vector<int>::iterator i = vec.begin(); i != vec.end(); i++) { *i = 7; } 各要素はポインタのように扱えばおk。 参考: STL超入門</int>

vector の使い方基礎

C++

vector で動的配列を扱うことができる。 #include <iostream> #include <vector> using namespace std; int main(void) { // 大きさ 3 の動的配列を作る vector<int> vec(3, 0); // 大きさ 5 にリサイズし、増えた分に 3 を入れる vec.resize(5, 3); // リストの最後に 7 をプッシュ</int></vector></iostream>…

C++の数字と文字列の変換操作

C++

あとからサッと確認するための、ごくシンプルなコードを記載。 その1 - 数字を文字列に変換 #include <iostream> #include <string> #define SIZE(arg) (sizeof(arg) / sizeof(arg[0])) using namespace std; int main(void) { string str = ""; int x[] = {11, 22, 33, 44, 5</string></iostream>…