answersLogoWhite

0


Best Answer

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

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

By dynamic constructor I assume you mean the C++ new operator. It works by calling malloc to allocate a contiguous block of memory for the specified class, returning a pointer to that memory, or NULL if the allocation fails. The amount of memory allocated is the sum of all member variables, plus padding where required. If the class is a derived class, the contiguous block of memory will be large enough to accommodate all the base class member variables as well, and the least-derived class constructor is called first to initialise those variables, working down the hierarchy to the most-derived class constructor. When the pointer is deleted, the most-derived class destructor is called first, working back up the hierarchy (providing the least-derived class destructor is declared virtual).

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

The amount of memory allocated was determined at the compile time or link time,source code could not locate,increase or decrease once memory is allocated. but however further development of hardware and software technologies and with functions like malloc(), new dynamically allotte memory as a program runs open possibilities that did not exit before.... those new features implemented dynamism in c++ ...... that makes concept of modularity fantastic.

there are 3 types of dynamism in object oriented design these are

1 dynamic typing

2 dynamic binding

3 dynamic loading

.....................................................................................

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

This constructor is used to allocate the memory to the objects at the run time..

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How dynamic constructor create in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
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.


Static vs dynamic binding in c plus plus?

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


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.


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.


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 a constructor to display a message in java?

(a) A constructor is similar to a method, but it has exactly the same name as the class (including the combination of uppercase and lowercase letters). (b) It is placed inside the class definition. (c) The constructor is invoked automatically when you create an object based on that class. (d) To display a message, you can use the command System.out.println("Put message here")