//I am trying to discover the equivalent of instanceof (of JAVA) for C++
//Well, I discover that typeid() could be an option...
//What are the problems in my code?
//Any advice to improve my code is welcomed!
//How are objects passed to a method of a class by default: by value or by reference? Can we pass an object of a derived class to the same parameter (of type of the mother class) of a function? (That's actually what I am trying to do in the main function...)
#include <iostream>
#include <typeinfo>
usingnamespace std;
class Vehicle
{
//...
};
class Car: public Vehicle
{
//...
};
class Counter
{
//...
public:
void countsWheels(Vehicle* v1)
{
if(typeid(*v1) == typeid(Car))
cout << "v1 is a car and has X wheels" << endl;
elseif(typeid(*v1) == typeid (Vehicle))
cout << "v1 is a vehicle and has Y wheels" << endl;
}
};
int main()
{
Counter* c1 = new Counter();
Vehicle* v1 = new Vehicle();
Car* car1 = new Car();
c1->countsWheels(v1);
c1->countsWheels(car1);//shows the same thing as the previous instruction. why?
return 0;
}
//ok, thank you. But the fact you used the destructor as virtual souldn't make the class abstract, and therefore not possibly instantiated? (NO!) The fact you used the destructer is just a case, right? We could use each function we would want (except the constructer)? What does "virtual" do in this case?
Virtual is what makes it call the correct function based on the true type of the object. Functions that are not virtual only the static type of the pointer/reference is used to decide which function to call.
#include <iostream>
class Base
{
public:
void print() { std::cout << "Base!" << std::endl; }
};
class Derived : public Base
{
public:
void print() { std::cout << "Derived!" << std::endl; }
};
int main()
{
Base base;
Derived derived;
base.print(); // prints "Base!"
derived.print(); // prints "Derived!"
Base& baseRef = derived;
baseRef.print(); // prints "Base!"
}
Notice how baseRef.print() calls the Base's print function despite that the object that baseRef refers to is a Derived object. If you change print() into a virtual function it would instead call the Derived's print function like you would expect.
The reason I chose the destructor was because it's often recommended to make the destructor virtual if you are going to inherit from the class because otherwise it will not be destructed correctly if deleted through a pointer to the base class.
1 2 3 4 5 6 7 8 9 10
Vehicle *v1 = new Car;
Car *v2 = new Car;
// This will not work if ~Vehicle() is not virtual (normally what
// happens is that only ~Vehicle() gets called but the destructor
// of Car and Car's data members are not called).
delete v1;
// This on the other hand will work even if the destructor is not virtual.
delete v2;
So, if we want to derive classes from a mother class it is adviseble to declare the desctrutor of the mother class as virtual (always?), so that when we want to delete all the objects created from the derived classes we just have to call the destructor of the mother class "delete objOfMother;"? otherwise, as you said, we have to call explicitly all the sons classes destructors
Functions that are not virtual only the static type of the pointer/reference is used to decide which function to call.
So, if we want to derive classes from a mother class it is adviseble to declare the desctrutor of the mother class as virtual (always?) ...
Yes that is my recommendation.
... so that when we want to delete all the objects created from the derived classes we just have to call the destructor of the mother class "delete objOfMother;"? otherwise, as you said, we have to call explicitly all the sons classes destructors
delete do more than just calling destructors and you should not have to call the destructor explicitly. If you choose not to make the destructor virtual you should not call delete unless you are 100% sure that the object pointed to is not of a derived class.
What do you mean with static type?
I mean the type that is known at compile time.
1 2
Vehicle* v1; // The type of v1 will always be 'pointer to class Vehicle'.
// Vehicle* is the static type of v1 no matter what v1 points to
in my case I created a pointer to an object of type Car (is car the static type?)...