Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > invoking static methods of derived class

Reply
Thread Tools

invoking static methods of derived class

 
 
ketan
Guest
Posts: n/a
 
      09-13-2006
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

 
Reply With Quote
 
 
 
 
ddtbhai@gmail.com
Guest
Posts: n/a
 
      09-13-2006
Hi Ketan,

if i understand correctly, you are trying to invoke a standard base
class function that will eventually invoke a dervied class
implementation of the function. This is where virtual functions have to
be used.

so, your code will sth like this :

class Vehicle
{
Vehicle();
virtual ~Vehicle();
public:
virtual void show() { std::cout << " Vehicle " << std::endl; }
virtual void stop() { std::cout << " Vehicle stopped" <<
std::endl; }
};

class Car : public Vehicle
{
public:
Car();
~Car();
void show() { std::cout << " Car " << std::endl; }
void stop(void) { std::cout << " Car stopped " << std::endl; }
};

int main()
{
Vehicle* vPtr = new Car( ); // usually u will manage such
instances with base class
// pointers so that you
don't have to know in advance
// what type they are.
vPtr->stop( ); // will call Car's method

}

Hope i've expressed myself in a simple fashion !
I don't think your situation requires the invocation of static
functions of derived classes in the base class... (which is a bad idea
generally)

ketan wrote:
> 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


 
Reply With Quote
 
 
 
 
Nate Barney
Guest
Posts: n/a
 
      09-13-2006
ketan wrote:
>
> 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.
>
> [ snip code ]
>


I think you're confused as to the purpose of static functions. What you
want in this case is nonstatic virtual functions. Something like:

#include <iostream>

class Vehicle
{
public:

virtual ~Vehicle() {} // note the virtual destructor here
virtual void show() { std::cout << " Vehicle " << std::endl; }
virtual void stop()
{ std::cout << " Vehicle stopped " << std::endl; }
};

class Car : public Vehicle
{
public:

virtual void show() { std::cout << " Car " << std::endl; }
virtual void stop()
{ std::cout << " Car stopped " << std::endl; }
};

class Bus : public Vehicle
{
public:

virtual void show() { std::cout << " Bus " << std::endl; }
virtual void stop()
{ std::cout << " Bus stopped " << std::endl; }
};

int main()
{
Vehicle v;
v.stop(); // call the function on the object, not the class
}

Read more about inheritance and virtual functions.

Hope this helps,
Nate
 
Reply With Quote
 
ketan
Guest
Posts: n/a
 
      09-13-2006
Hi Nate,

I am aware of virtual function usage.

But may be I was not clear in my requirement. Let me summarize again,

1) I need static method as It can be invoked anytime _without_
reference to the active object type. Since I cant access object
pointer.

2) I need to call this method on all derived classes. Assume that on
road, cars and busses both are running (assume that only one instance
of each type is running at given time). So when I call stop:: I need
both of them to stop.

So I am not looking for polymorphic or dynamic binding of methods.

I hope, I could explain my situation better.
Thx
Ketan

Nate Barney wrote:
> ketan wrote:
> >
> > 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.
> >

>
> Read more about inheritance and virtual functions.
>
> Hope this helps,
> Nate


 
Reply With Quote
 
Nate Barney
Guest
Posts: n/a
 
      09-13-2006
ketan wrote:
> Hi Nate,
>
> I am aware of virtual function usage.
>
> But may be I was not clear in my requirement. Let me summarize again,
>
> 1) I need static method as It can be invoked anytime _without_
> reference to the active object type. Since I cant access object
> pointer.
>
> 2) I need to call this method on all derived classes. Assume that on
> road, cars and busses both are running (assume that only one instance
> of each type is running at given time). So when I call stop:: I need
> both of them to stop.
>
> So I am not looking for polymorphic or dynamic binding of methods.


Well, I think you seriously need to rethink your design then. You
shouldn't be using static functions in this case, IMHO. What about a
Road class that has a container of Vehicles? Then you could have a
stop() method on the Road class that would iterate over the Vehicles and
call the virtual stop() function for each of them.

However, against my better judgment, I offer this as something that
might be interesting:

#include <iostream>
#include <vector>

class Vehicle
{
public:

typedef void(StopFunc)(void);
typedef std::vector<StopFunc*> StopVec;

void show() { std::cout << " Vehicle " << std::endl; }
static void addStopFunc(StopFunc *sf) { stopVec.push_back(sf); }
static void stop(void)
{
std::cout << " Vehicle stopped " << std::endl;

for (StopVec::iterator i = stopVec.begin();
i != stopVec.end(); ++i)
(*i)();
}

private:

static StopVec stopVec;
};

Vehicle::StopVec Vehicle::stopVec;

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::addStopFunc(Car::stop);
Vehicle::addStopFunc(Bus::stop);
Vehicle::stop();
}
 
Reply With Quote
 
ketan
Guest
Posts: n/a
 
      09-13-2006
Hi Nate,

Thanks for the detailed explanation.

Yes, I did rethink about my design and will do required changes. Also
discussed requirement and i think polymorphic invocation would suffice
(yes any one instance only).

It was nice to know what CAN'T be done or assert again that when you
run into such issues -- rethink design

thx
Ketan

Nate Barney wrote:
> ketan wrote:
> > Hi Nate,
> >
> >
> > So I am not looking for polymorphic or dynamic binding of methods.

>
> Well, I think you seriously need to rethink your design then. You
> shouldn't be using static functions in this case, IMHO. What about a
> Road class that has a container of Vehicles? Then you could have a
> stop() method on the Road class that would iterate over the Vehicles and
> call the virtual stop() function for each of them.
>


 
Reply With Quote
 
Nate Barney
Guest
Posts: n/a
 
      09-14-2006
Nate Barney wrote:
>
> What about a Road class that has a container of Vehicles? Then you
> could have a stop() method on the Road class that would iterate over
> the Vehicles and call the virtual stop() function for each of them.


I neglected to mention that if you do something like this, you'll need
to store a container of Vehicle*, not Vehicle, because otherwise you'll
have a slicing problem and polymorphism won't work. Additionally,
you'll need to make sure you delete the pointers before you clear the
container, either by explicitly iterating over them and deleting them
manually, or by storing some sort of smart pointer in the container.

Nate
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
Invoking templatized base class constructor from templatized derived class constructor mrstephengross C++ 5 05-18-2005 07:12 PM
Instantiating a static class( Class with all static members - methods and variables) SaravanaKumar Java 6 10-19-2004 08:20 AM



Advertisments