Skip to content
Snippets Groups Projects
Commit 275513fa authored by Christian Werner's avatar Christian Werner
Browse files

update main.cpp

parent 2f44ce81
Branches
No related tags found
No related merge requests found
......@@ -3,13 +3,12 @@
#include "mbed.h"
#include <iostream>
namespace ost {
class Event {
public:
Event(int sourceId = -1):source{sourceId}{}
int GetSource() {return source;}
virtual ~Event();
virtual ~Event() = default;
protected:
int source;
......@@ -17,12 +16,12 @@ namespace ost {
class ButtonEvent : public Event {
public:
ButtonEvent();
ButtonEvent() = default;
};
class SystemEvent : public Event {
public:
SystemEvent();
SystemEvent() = default;
void SetSource(int id) {source = id;}
};
}
......@@ -30,22 +29,38 @@ namespace ost {
int main() {
using namespace ost;
using ost::Event; // required, because mbed.h (?) declares a different type "Event" in global namespace!
using ost::Event; // required, because mbed.h (?) declares a different type "Event" in global namespace!
constexpr double d{37.34f};
constexpr int ia[]{4, 5, 6};
int x; // TODO init with the integer part of d, use a type cast -- is it required?
float f; // TODO init with value of d, suppress possible warning "loss of precision", use a type cast -- is it required?
int *ip; // TODO init with address of 0-th element of ia, use a type cast -- is it required?
int ia[]{4, 5, 6};
//int x; // TODO init with the integer part of d, use a type cast -- is it required?
int x{static_cast<int>(d)}; // not reuqired, but useful to suppress potential warning
//float f; // TODO init with value of d, suppress possible warning "loss of precision", use a type cast -- is it required?
float f{static_cast<float>(d)}; // not reuqired, but useful to suppress potential warning
//int *ip; // TODO init with address of 0-th element of ia, use a type cast -- is it required?
int *ip = ia; // We do not need a type cast here, implicit type conversion is perfectly fine.
// However, using a static_cast is not wrong:
//int *ip = static_cast<int*>(ia);
std::array<const Event*, 10> ea{new Event(10), new ButtonEvent, new SystemEvent, new Event(20), new SystemEvent};
for (auto& e : ea) {
// TODO: call SetSource(200) on all instances of type SystemEvent. You will need two type casts for this...
SystemEvent* sep = dynamic_cast<SystemEvent*>(const_cast<Event*>(e)); // we need dynamic_cast for finding the relevant elements as well as
if (sep) { // sep will be nullptr, if dynamic_cast fails const_cast to temporarily modify const-ness of Event* type
sep->SetSource(200);
}
}
// print each byte of f in hex:
for (size_t i = sizeof(f) - 1; i >= 0; --i) {
printf("%hhx", static_cast<unsigned char>(0)); // TODO replace static_cast<unsigned char>(0) with correct expression. Use a type cast -- is it required?
printf("0x");
for (size_t i = sizeof(f); i-- > 0;) {
//printf("%hhx", static_cast<unsigned char>(0)); // TODO replace static_cast<unsigned char>(0) with correct expression. Use a type cast -- is it required?
printf("%hhx", reinterpret_cast<uint8_t*>(&f)[i]); // reinterpret_cast is required here!
}
printf("\r\n");
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment