Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ dynamic casting
    primarykey
    data
    text
    <p>I have a main abstract class that I am provided and must create subclasses based on this class (THIS CANNOT BE MODIFIED):</p> <pre><code>class Spaceship { protected: string m_name; // the name of the ship int m_hull; // the hull strenght public: // Purpose: Default Constructor // Postconditions: name and hull strength set to parameters // -- INLINE Spaceship(string n, int h) { m_name = n; m_hull = h; } // Purpose: Tells if a ship is alive. // Postconditions: 'true' if a ship's hull strength is above zero, // 'false' otherwize. // -- INLINE bool isAlive() { return (m_hull &gt; 0); } // Purpose: Prints the status of a ship. // -- VIRTUAL virtual void status() const = 0; // Purpose: Changes the status of a ship, when hit by a // weapon 's' with power level 'power' // -- VIRTUAL virtual void hit(weapon s, int power) = 0; string getName() const { return m_name; } }; //Spaceship </code></pre> <p>So is an example of my child classes:</p> <pre><code>class Dreadnought: public Spaceship { int m_shield; int m_armor; int m_type; public: Dreadnought( string n, int h, int a, int s ): Spaceship( n, h ),m_shield( s ),m_armor(a),m_type(dreadnought){} virtual void status() const { // implementation not shown to save space } virtual void hit(weapon s, int power) { // implementation not shown to save space } int typeOf(){ return m_type; } }; </code></pre> <p>in my main code I have an dynamic array of different types of Spaceships:</p> <pre><code>Spaceship ** ships; cin &gt;&gt; numShips; // create an array of the ships to test ships = new Spaceship * [numShips]; </code></pre> <p>Then i get input from the user to declare different types of ships in this array like:</p> <pre><code>ships[0] = new Dreadnought( name, hull, armor, shield ); </code></pre> <p>my question is when I go to delete the array the correct destructor is not called, instead Spaceships is called, will this create a memory leak because the member vars "m_shield, m_armor" are not deleted and left hanging? If so is there a better way to get the type than using a var m_type and calling:</p> <pre><code>if( ships[i]-&gt;typeOf() == 0 ) delete dynamic_cast&lt;Frigate*&gt;(ships[i]); else if( ships[i]-&gt;typeOf() == 1 ) delete dynamic_cast&lt;Destroyer*&gt;(ships[i]); else if( ships[i]-&gt;typeOf() == 2 ) delete dynamic_cast&lt;Battlecruiser*&gt;(ships[i]); else if( ships[i]-&gt;typeOf() == 3 ) delete dynamic_cast&lt;Dreadnought*&gt;(ships[i]); else delete dynamic_cast&lt;Dropship*&gt;(ships[i]); </code></pre> <p>Question #2 in the Spaceship class i declared: virtual int typeOf() = 0; and commented it out, is there a way i can implement this function in the child classes without declaring in the parent class so i can use it like shown above? when i dont declare it i get the compiler error:</p> <p>error: 'class Spaceship' has no member named 'typeOf'</p> <p>I assume this again has something to do with dynamic casing.</p> <p>any help would be great,</p> <p>Thanks nat</p> <p>edit:</p> <p>to clairify my first question would i have a memory leak if I just did:</p> <p>delete ships[i];</p> <p>or should i do:</p> <p>delete dynamic_cast(ships[i]);</p> <p>to remove member vars that are only in the derived classes?</p> <p>Thnaks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload