last day (15 days later) » 

10:07 PM
0
A: Can I use something other than dynamic_cast in this design?

KemyLandIf you can support the extra (minimal in general-purpose systems, enormous in real-time/embedded systems) runtime overhead, you can use dynamic_cast<> safely. However, you must be aware of this "small" detail. Given the expression dynamic_cast<T*>(expr), whenever the dynamic type of *expr is nei...

 
Can you please elaborate on second last bullet point?
 
@brainydexter: Do you mean the if that stuff is involved... point?
 
Yes : virtual Config &getConfig() = 0 in the Base,
Also, in my question, the dynamic casting will not happen frequently, init time perhaps - so I don't think runtime expense will be a problem. As a design, do you think there is a problem here ?
 
The expression virtual Config &getConfig() = 0 declares a pure virtual function named getConfig that returns a Config& (and thus, the return value can reference a given Derived object as I said you in another comment). The virtual specifier tells the compiler that the dynamic type of the object shall be queried whenever calling the function through a Base& or Base*. By default, a base class defines a function to be called if the real object's type doesn't overload the function. (To be continued...)
@brainydexter: Sorry, I had to cut the commnet in two due to it's length. Continuation: The = 0 specifier (called the pure specifier), marks the virtual function as pure. A pure virtual function does not have a default to call if the function isn't overloaded. Thus, a class containing a pure virtual function cannot be instantiated (it would be catastrophic!), and is said to be abstract. In the inheritance tree, all subclasses of an abstract Base will be implicitly abstract until all the pure virtual functions get fully implemented by a given subclass.
 
@KemyLand: I understand Pure virtual functions and inheritance. I didn't know one could use references, like pointers, to reference a base class object to a derived object
From your comment, I gather we can do this.
 
10:13 PM
@brainydexter: Perfect! Say me if you have any other doubt :).
 
first of all - thanks for taking the time to reply
I appreciate it.
We're on the same page for pointers vs references.
last question I have is.. In my question, I'm trying to understand if there is a downside of using dynamic_cast ?
in this particular case, I'm trying to understand if choosing to dynamic_cast<A0Config*>(baseConfig) is a bad thing to do within A0Device
@KemyLand my reasoning is: Since A0Config is created within A0Device and only A0Device uses A0Config, I think dynamic_cast should be fine
 
10:36 PM
@brainydexter: It's perfectly fine. Just take a note that if you're working on embedded or real-time systems, dynamic_cast<>() can create a pretty nasty overhead over static casts such as C-style casts or all the other C++-style casts. Note that virtual functions do have some overhead, although it's orders of magnitude less impactating than that of dynamic_cast<>().
If you're on general-purpose systems (such as desktops or servers), just do what your heart tells you to do, and ignore the insignificant overhead created by both methods.
 

last day (15 days later) »