How to use static cast in C++

3 Answers

0 votes
#include <iostream>

using std::cout;
using std::endl;

int main()
{
	int n = 4;
	double result = static_cast<double>(4) / 10;

	cout << result << endl;

	return (0);
}


/*
run:

0.4

*/

 



answered Feb 23, 2018 by avibootz
0 votes
#include <iostream>

using std::cout;
using std::endl;

int main()
{
	int n = static_cast<int>(3.14);
	
	cout << n << endl;

	return (0);
}


/*
run:

3

*/

 



answered Feb 23, 2018 by avibootz
0 votes
#include <iostream>

using std::cout;
using std::endl;

struct A {
	void f() const {
		std::cout << "struct A" << endl;
	}
};
struct B : A {
	void f() const {
		std::cout << "struct B" << endl;
	}
};

int main()
{
	B b1;
	A& a = b1;
	a.f();

	B& b2 = static_cast<B&>(a); 
	b2.f();

	return (0);
}


/*
run:

struct A
struct B

*/

 



answered Feb 23, 2018 by avibootz

Related questions

1 answer 107 views
1 answer 110 views
1 answer 117 views
2 answers 166 views
1 answer 106 views
1 answer 171 views
...