So I'm about to complete Prata's C ++ primer and I'm a UI for RTTI, he's one of downcasting The line appeared and just said that this is wrong but I want to see a better example.
Class Grand {Private: int hold; Public: Grand (Int H = 0): Hold (H) {} Virtual Voice Speak () Const {cout & lt; & Lt; "I'm a great class \ n"}} Virtual Ent Value () Const (return hold; } Zero cave () const {cout & lt; & Lt; "OK" & lt; & Lt; Andal;}}; Class spectacular: Public Grand {Public: Fantastic (Int H = 0): Grand (H) {} Speak zero () Const {COT & LT; & Lt; "I'm a fantastic class! \ N";} say virtual void () const {cout & lt; & Lt; "I have a great price" & lt; & Lt; Value () & lt; & Lt; "! \ N";} zero cum () const {cout & lt; & Lt; "Nozzle" & lt; & Lt; Andal;}}; Class Magnificent: Public Splendid {Private: Four F; Public: Full Hour; Brilliant (Int H = 0, Four C = 'A'): Superb (H), CH (C) {} Word Speak () CONST {COT & LT; & Lt; "I'm a fantastic class! \ N";} say zero () const {cout & lt; & Lt; "I Catch the Character" & lt; & Lt; F & lt; & Lt; "And integer" & lt; & Lt; Value () & lt; & Lt; "! \ N";} Bad Meh () const {cout & lt; & Lt; "OK" & lt; & Lt; Andal;}}; Grand * GetOne (); Int _tmain (int argc, _TCHAR * argv []) {/ * srand (time (0)); Grand * Page; Fantastic * ps; * / Grand * pg = new grand; Grand * ps = new brilliant; Grand * h = new brilliant; Brilliant * ps2 = (splendid *) page; Ps2- & gt; Gah (); Cout & lt; & Lt; Ps2- & gt; Hour & lt; & Lt; Endl; System ("pause"); }
So above, I am casting a foundation for a derivative which is not to be done entirely. However, in this example, what exactly am I limited? When I'm casting PG, I can still be used for all grand / spectacular / great qualities and methods through ps2. In other words, nothing here is unsuccessful, can someone give me an example or add something in that code which will clearly show me how to get rid of derived derivative things?
Do not use C style. They are not safe. C ++ has started 4 new dots which you are looking for, dynamic_cast is <>
fantastic * ps2 = dynamic_cast & lt; Fantastic * & gt; (PG); // If PG is a superb // (or // is a super class of super) it works fine // If PG is not a superb (in this case), then it will return the tap.
When you use C style, you are asking the compiler to ignore all the rules and you tell it (the compilers are happy). No check has been done to ensure that what you are doing. Any meaning
C ++ style is very limited and each castings have a specific category. The dynamic_cast class hierarchy is used to up and down.
Comments
Post a Comment