Versuchen, einen Weg zu finden, meine dynamische Umwandlung zu arbeiten, aber ich bekomme Laufzeitfehler. Es springt beim Ausgeben zum else-Anweisungsblockwert (wenn es ein if-Blockwert sein soll, wird aber nicht einmal verwendet, wenn ich es innerhalb der abgeleiteten Klasse aufruft. Es zeigt also den falschen Wert an und verwendet es auch gar nicht in der . Berechnung Warum ist das Danke für die HilfeVerwenden von dynamischer Umwandlung, um unterschiedliche Werte für Variable in abgeleiteter Klasse zurückzugeben
class Package
{
protected:
string name_and_address = "?";
double cost = 0.0;
double discount = 0.0;
double discount_rate = 0.0;
bool overnight_delivery = false;
bool insured = false;
string package_contents = "?";
double shipcost = 0.0;
public:
Package() {};
~Package() {};
protected:
virtual double calculate_cost() = 0;
class Video_Games {}; //defined the classes
class Genius_Phone {};
class Sausage {};
class Albums {};
// here I'm trying to change "shipcost" inside each of the derived classes
// to their respective and appropriate dollar values.
// However, I am getting a runtime error wherein it will jump DIRECTLY
// to the else statement 50.00 value for Video_Games
// and not even calculate the value as 50.00. SO it's doubly useless.
// It just skips to the else value and doesn't even factor
// that into the Video_Games calculation when I try to test it.
virtual double shipping_cost() {
if (dynamic_cast<Video_Games*>(this))
return 4.99;
else if (dynamic_cast<Genius_Phone*>(this))
return 25.00;
else if (dynamic_cast<Sausage*>(this))
return 9.00;
else
{
// should be assigned to class Albums,
// but is somehow being triggered by class Video_Games.
// Not sure why this is.
return 50.00;
}
}
};
class Video_Games :public Package
{
private:
int num_games = 0;
public:
Video_Games(string location, int number_of_games, bool express, bool insurance)
{
num_games = number_of_games;
name_and_address = location;
overnight_delivery = express;
insured = insurance;
package_contents = to_string(num_games) + " Video Game(s)";
cost = calculate_cost();
discount = calculate_discount();
shipcost = shipping_cost();
}
~Video_Games() {};
protected:
double calculate_cost()
{
cost = num_games * 19.99;
// this is where the magic should happen.
// shipcost here should be 4.99, but instead it's not even being used here.
// In fact - it's empty. I'm not sure why shipcost
// when set equal shipping_cost() is not returning that appropriate value.
// Very baffling.
if (overnight_delivery) { cost += shipcost; }
if (insured) { cost *= 1.06; }
return cost;
}
};
Das liegt daran, dass Sie es falsch machen. Sie sollten sich diese Seite ansehen, um sie zu benutzen: http://en.cppreference.com/w/cpp/language/dynamic_cast – Asesh
Es ist lustig, dass Sie diese Seite verlinkt haben. Ich habe es auf diese Weise versucht, und ich habe das gleiche Endergebnis bekommen. Vielleicht könntest du mir zeigen, wie ich diesen Code auf diese Weise faktorisieren kann? – polymorphism
Nicht verwandt mit der Frage - aber warum verwenden Sie nicht eine Art polymorphe 'GetShippingCost()' Funktion? Es wäre viel besser lesbar und würde dich davon abhalten, diese hässliche Konditionals/'dynamic_cast' Mischung zu haben. – pSoLT