1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

arduinoでcast

Posted at

概要

arduinoでstatic_cast、reinterpret_cast、dynamic_castやってみた。

環境

arduino ide ver 1.6.4
arduino uno

写真

cast.JPG

サンプルコード

class Kitty {
public:
	int val = 0;
};
class Chobits : public Kitty {
public:
	int val = 1;
} obj ;

void setup()
{
	Serial.begin(115200);
	Serial.println("start");
	double d = 3.141592;
	float f = static_cast<double> (d);
	Serial.print("f => ");
	Serial.println(f);
	int n = static_cast<int> (d);
	Serial.print("n => ");
	Serial.println(n);
	long l = 42;
	short s = static_cast<short> (d);
	Serial.print("s => ");
	Serial.println(s);
	char c = static_cast<char> (d);
	Serial.print("c => ");
	Serial.println(c);
	unsigned int u1 = 137U;
	int n1 = static_cast<int> (u1);
	Serial.print("n1 => ");
	Serial.println(n1);
	int n2 = 139;
	unsigned int u2 = static_cast<unsigned int> (u1);
	Serial.print("u2 => ");
	Serial.println(u2);
	long lval = 200;
	int * i2 = reinterpret_cast<int *> (lval);
	Serial.print("i2 => ");
	Serial.println(* i2);
	Serial.print("obj.val => ");
	Serial.println(obj.val);
	Kitty * po = dynamic_cast<Kitty *> (&obj);
	Serial.print("po->val => ");
	Serial.println(po->val);
	Serial.println("end");
}
void loop()
{
}

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?