invoking static methods of derived class

Discussion in 'C++' started by ketan, Sep 13, 2006.

  1. ketan

    ketan Guest

    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
     
    ketan, Sep 13, 2006
    #1
    1. Advertising

  2. ketan

    Guest

    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
     
    , Sep 13, 2006
    #2
    1. Advertising

  3. ketan

    Nate Barney Guest

    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
     
    Nate Barney, Sep 13, 2006
    #3
  4. ketan

    ketan Guest

    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
     
    ketan, Sep 13, 2006
    #4
  5. ketan

    Nate Barney Guest

    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();
    }
     
    Nate Barney, Sep 13, 2006
    #5
  6. ketan

    ketan Guest

    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.
    >
     
    ketan, Sep 13, 2006
    #6
  7. ketan

    Nate Barney Guest

    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
     
    Nate Barney, Sep 14, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SaravanaKumar
    Replies:
    6
    Views:
    9,410
    Tony Morris
    Oct 19, 2004
  2. mrstephengross
    Replies:
    5
    Views:
    598
    Larry I Smith
    May 18, 2005
  3. Replies:
    1
    Views:
    397
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    389
    Victor Bazarov
    May 23, 2007
  5. Kenneth McDonald
    Replies:
    5
    Views:
    325
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page