K
ketan
Hi All,
I have a situation where in I need to call static methods of the
derived class. I checked previous posts, but could not get any
satisfactory reply.
My situation can be simulated with following code.
//
// Does not compile. Not correct C++ syntax - shows what needs to be
done.
//
#include <iostream>
class Vehicle
{
public:
void show() { std::cout << " Vehicle " << std::endl; }
void stop(void)
{
std::cout << " Vehicle stopped " << std::endl;
Car::stop();
Bus::stop();
}
};
class Car : public Vehicle
{
public:
Car();
~Car();
void show() { std::cout << " Car " << std::endl; }
// knows how to stop Car
static void stop(void) { std::cout << " Car stopped " << std::endl;
}
};
class Bus : public Vehicle
{
public:
Bus();
~Bus();
void show() { std::cout << " Bus " << std::endl; }
static void stop(void) { std::cout << " Bus stopped " << std::endl;
}
};
int main()
{
Vehicle v;
Vehicle::stop();
}
Situation can be something like this, assume that some car or bus are
moving and we don't know which are moving (each class internally tracks
all active instant etc.) So we want to call a static method which will
ensure that any type - Car or Bus if active will stop.
So I need to call some static method in base class, which internally
can assure that all derived types will stop if they are moving.
I am not able to figure out how to solve this problem or any alternate
way to solve the base problem.
Thanks
Ketan
I have a situation where in I need to call static methods of the
derived class. I checked previous posts, but could not get any
satisfactory reply.
My situation can be simulated with following code.
//
// Does not compile. Not correct C++ syntax - shows what needs to be
done.
//
#include <iostream>
class Vehicle
{
public:
void show() { std::cout << " Vehicle " << std::endl; }
void stop(void)
{
std::cout << " Vehicle stopped " << std::endl;
Car::stop();
Bus::stop();
}
};
class Car : public Vehicle
{
public:
Car();
~Car();
void show() { std::cout << " Car " << std::endl; }
// knows how to stop Car
static void stop(void) { std::cout << " Car stopped " << std::endl;
}
};
class Bus : public Vehicle
{
public:
Bus();
~Bus();
void show() { std::cout << " Bus " << std::endl; }
static void stop(void) { std::cout << " Bus stopped " << std::endl;
}
};
int main()
{
Vehicle v;
Vehicle::stop();
}
Situation can be something like this, assume that some car or bus are
moving and we don't know which are moving (each class internally tracks
all active instant etc.) So we want to call a static method which will
ensure that any type - Car or Bus if active will stop.
So I need to call some static method in base class, which internally
can assure that all derived types will stop if they are moving.
I am not able to figure out how to solve this problem or any alternate
way to solve the base problem.
Thanks
Ketan