Having this example code here:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
namespace autos
{
enum CarTypes
{
SUVType,
SportType,
UnknownType
};
class Car
{
public:
Car();
virtual ~Car();
CarTypes type;
int powerCV;
};
Car::Car()
{
type = CarTypes::UnknownType;
}
Car::~Car() {}
class SUV : public Car
{
public:
SUV();
virtual ~SUV();
bool fourXFour;
int trunkSizeLiters;
bool dvdInBackSeat;
};
SUV::SUV()
{
type = CarTypes::SUVType;
powerCV = 0;
fourXFour = false;
trunkSizeLiters = 0;
dvdInBackSeat = false;
}
SUV::~SUV() {}
class Sport : public autos::Car
{
public:
Sport();
virtual ~Sport();
bool convertible;
bool twoSeats;
int maxSpeed;
};
Sport::Sport()
{
type = autos::CarTypes::SportType;
powerCV = 0;
convertible = false;
twoSeats = false;
maxSpeed = 0;
}
Sport::~Sport() {}
} /* end namespace autos */
autos::Car ManufactureCar()
{
srand(time(NULL));
int typeCode = rand() % 2 + 1;
if (typeCode == 1)
{
autos::SUV newSUV;
newSUV.powerCV = 300;
newSUV.fourXFour = true;
newSUV.trunkSizeLiters = 500;
newSUV.dvdInBackSeat = true;
return newSUV;
}
autos::Sport newSport;
newSport.powerCV = 550;
newSport.maxSpeed = 300;
newSport.twoSeats = true;
newSport.convertible = true;
return newSport;
}
int main(int argc)
{
autos::Car newCar = ManufactureCar();
if (newCar.type == autos::CarTypes::SportType)
{
std::cout << "A new SPORT car was manufactured: " << std::endl;
autos::Sport& sportCar = dynamic_cast<autos::Sport&>(newCar);
std::cout << "Power (CV): " << sportCar.powerCV << std::endl;
std::cout << "Convertible: " << sportCar.convertible << std::endl;
std::cout << "Maximum speed: " << sportCar.maxSpeed << std::endl;
std::cout << "Two seats: " << sportCar.twoSeats << std::endl;
}
else if (newCar.type == autos::CarTypes::SUVType)
{
std::cout << "A new SUV car was manufactured: " << std::endl;
autos::SUV& suvCar = dynamic_cast<autos::SUV&>(newCar);
std::cout << "Power (CV): " << suvCar.powerCV << std::endl;
std::cout << "4x4: " << suvCar.fourXFour << std::endl;
std::cout << "Trunk size (Liters): " << suvCar.trunkSizeLiters << std::endl;
std::cout << "DVD in backseats: " << suvCar.dvdInBackSeat << std::endl;
}
else
{
std::cout << "ERROR: Unknown car manufactured." << std::endl;
}
}
I´ve looked at this post here and the program shown compiles, but it does throw an exception (Access violation
) when the dynamic_cast
is called....
I´ve tried using pointers: autos::SUV* suvCar = dynamic_cast<autos::SUV*)(&newCar)
and got also access violation.
Why am I´m not being able to cast the data to the derived type ?
Is there a better way to get the real type or dynamic_cast
is the only/better option here ?
[EDIT - SOLUTION]
Problem solved using smart pointers:
std::shared_ptr<autos::Car> ManufactureCar()
{
srand(time(NULL));
int typeCode = rand() % 2 + 1;
if (typeCode == 1)
{
autos::SUV newSUV;
newSUV.powerCV = 300;
newSUV.fourXFour = true;
newSUV.trunkSizeLiters = 500;
newSUV.dvdInBackSeat = true;
auto ret = std::make_shared<autos::SUV>(newSUV);
return std::dynamic_pointer_cast<autos::Car>(ret);
}
autos::Sport newSport;
newSport.powerCV = 550;
newSport.maxSpeed = 300;
newSport.twoSeats = true;
newSport.convertible = true;
auto ret = std::make_shared<autos::Sport>(newSport);
return std::dynamic_pointer_cast<autos::Car>(ret);
}
int main(int argc)
{
std::shared_ptr<autos::Car> newCar = ManufactureCar();
if (newCar->type == autos::CarTypes::SportType)
{
std::cout << "A new SPORT car was manufactured: " << std::endl;
std::shared_ptr<autos::Sport> sportCar = std::dynamic_pointer_cast<autos::Sport> (newCar);
std::cout << "Power (CV): " << sportCar->powerCV << std::endl;
std::cout << "Convertible: " << sportCar->convertible << std::endl;
std::cout << "Maximum speed: " << sportCar->maxSpeed << std::endl;
std::cout << "Two seats: " << sportCar->twoSeats << std::endl;
}
else if (newCar->type == autos::CarTypes::SUVType)
{
std::cout << "A new SUV car was manufactured: " << std::endl;
std::shared_ptr<autos::SUV> suvCar = std::dynamic_pointer_cast<autos::SUV> (newCar);
std::cout << "Power (CV): " << suvCar->powerCV << std::endl;
std::cout << "4x4: " << suvCar->fourXFour << std::endl;
std::cout << "Trunk size (Liters): " << suvCar->trunkSizeLiters << std::endl;
std::cout << "DVD in backseats: " << suvCar->dvdInBackSeat << std::endl;
}
else
{
std::cout << "ERROR: Unknown car manufactured." << std::endl;
}
}