Tudvari
Tudvari

Reputation: 2915

dynamic_cast or redundancy?

In my system low hiearchy objects talk to the high hiearchy object via calling function of the +1 level hiearchy object, which calls a function of the +1 level hiearchy object, etc etc etc, until the function calling stops at the recipent.

There is a Message abstract class, and there are lots of derivated classes, which hold different kinds of datas. Like:

etc etc

And those methods I mentioned before want Message objects so this chaincalling is done through one kind of method instead of creating methods for all of the Message types.

The problem comes, when the recipent receives the Message object.

The recipent wants to know what's in that message, so it can process receives message as the message type requires it.

(like it decreases the integer by 1 in FruitMessages, divides the coordinates in CoordinateMessages, etc.)

Currently I have two ideas, but maybe none of them is correct and I should use a third one. (tell me please)

My question is that is it worth the redundancy?

What should I do?

Upvotes: 1

Views: 119

Answers (2)

Joseph Thomson
Joseph Thomson

Reputation: 10393

I would recommend using the typeid operator to check the type of the message. This way, you avoid repeatedly calling dynamic_cast until you get the correct type.

void process_message(Message const& msg) {
  if (typeid(msg) == typeid(FruitMessage)) {
    auto& fruit_msg = static_cast<FruitMessage const&>(msg);
    …
  }
}

Even better, you could use the C++17 std::any container type. An any object can be copied around like a non-polymorphic value, and does not require the use of a virtual base class. If you don't have access to a C++17 library implementation, you could use boost::any instead.

void process_message(std::any const& msg) {
  if (msg.type() == typeid(FruitMessage)) {
    auto fruit_msg = std::any_cast<FruitMessage>(msg);
    …
  }
}

As for whether using an enum field would be faster than typeid or any, you'll have to do the benchmarking yourself.

Upvotes: 0

user3237732
user3237732

Reputation: 2028

Both ways are OK. Redundancy vs speed is very common problem in Software development. I would choose dynamic_cast as redundancy is the first step to bugs, but it's really up to you and depends on your performance requirements. I saw very similar problem while working with Akka, they usually use dynamic_cast (I mean java/scala analogues)

Upvotes: 2

Related Questions