answersLogoWhite

0

Dynamic casting is a type-safe method of casting one object type (a base class) to another object type (a derived class) in order to access methods that are specific to the derived class, but are not available to the base class. If the typecast fails for any reason, the return is NULL. If the cast is successful, the return is a pointer to the derived class.

Although there's nothing to prevent you dynamically casting objects in this way, it is considered bad programming practice, and is, in fact, wholly unnecessary. Virtual methods in the base class automatically give us access to more specific derived class methods from within the base class itself. In short, never dynamically cast an object. If a base class designed by a third-party has no suitable virtual method, then simply derive your own base class from it and provide your own virtual method.

By way of example, suppose you have a base class called animal from which you derive a cat and a dog. Cats and dogs make different sounds, so while it's tempting to create a Bark() method for the dog and a Meow() method for the cat, this only works when you actually have a pointer or a reference to a cat or a dog.

But what if you have a pointer or reference to the animal base class? Even if you know the animal is really a dog, how will you make it bark? The base class doesn't know it's really a dog, so there is no bark method to call. It may also be tempting to put both methods in the base class but, if we're not careful there's always a risk a cat will bark and a dog will meow.

So it appears the only solution is to dynamically cast the animal to a dog. If it turns out to be a cat, the result will be NULL and you'll be forced to dynamically cast a second time, this time to a cat. This doesn't sound so bad with only two animals to consider, but how about an entire zoo full of animals? Is it a lion, a tiger, a mouse, an elephant, a snake or something else entirely? Dynamic casting will get the job done, but it's a lot of work when you have to do this for every method in every derived class where cats and dogs are expected to act differently (such as Play() and DoBad()).

The correct way to deal with this is to include a pure-virtual method in the base class. All animals make a noise so simply declare a pure-virtual MakeNoise() method in the base class and implement it in each type of animal. A dog's MakeNoise() method will bark, it's Play() method will make it fetch a ball and it's DoBad() command will make it chase cars. All the things we expect of a dog.

Now when you have a pointer or a reference to an Animal, simply calling the base class method will invoke the correct override according to the actual type of animal it refers to. No need to dynamically cast, and absolutely no need to ever know what type of animal you're actually referring to. If it's a dog, it'll bark, fetch balls and chase cars. If it's a cat it'll meow, play with string and throw up in your shoes!

It's important to remember that the whole point of using virtual and pure-virtual functions is to allow the v-table (the virtual table) to determine the actual runtime type of an object and let it work out which override to call. It is not your responsibility as a programmer to force those methods out. The base class does not know and should not care whether it is a cat or a dog or a duck-billed platypus -- and nor should you! Your only concern is that the correct method be called and the v-table does that for, and a good deal more easily than dynamic casting ever can.

User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

Can you write own constructor in c plus plus?

Yes.


True or False A C plus plus class constructor cannot return a function value?

True - A C++ constructor cannot return a value.


What is the difference between implicit and explicit call of constructor in c plus plus?

An implicit constructor call will always call the default constructor, whereas explicit constructor calls allow to chose the best constructor and passing of arguments into the constructor.


Can constructor be declared as constant in c plus plus?

No. Constructors initialise objects and, by definition, must be able to modify the member variables. Uninitialised members are a disaster waiting to happen even without a constructor declared const! Thankfully, the compiler won't permit a const constructor.


What is the difference between constructor and friend function in c plus plus?

A constructor is a method that fires when the object is instantiated. A friend function is a function that has special access to the object. They are two different types of things, and cannot be further differenced.


Static vs dynamic binding in c plus plus?

Static binding occurs at compile time. Dynamic binding occurs at runtime.


How can a constructor be invoked at the time of inheritance in C Plus Plus?

It cannot. Inheritance is a compile-time operation. Constructors are invoked at runtime at the point of instantiation.


How do you invoke the constructor function in c plus plus?

There is no such thing as a constructor function in C++ (constructors have no return value, not even void, and cannot be called like regular functions). Constructors are invoked rather than called directly, either by declaring a static variable of the class type, or via the C++ new operator.


How do you create .exe file in c plus plus?

You can create an exe-file from your C++ source, if you have a compiler.


How constructor called?

You cannot invoke a constructor explicitly. It will get invoked implicitly when you call the new keyword on the class to create an object of the class. Ex: private ClassExample obj = new ClassExample(); here this new keyword usage on the ClassExample class will invoke the constructor of this class and create an object of that class.


Is overriding a dynamic polymorphism in c plus plus or not?

In C++, overriding and function, method, or operator is a different thing than (dynamic) polymorphism, so overriding a polymorphic method is almost entirely possible.


Constructor and destructor invocation in c?

Not possible in C.