why not virtual static method

Discussion in 'C++' started by Tony Johansson, Aug 21, 2005.

  1. Hello Experts!

    I know that you can't have virtual static methods and I know what a static
    method is.

    A static method exist only one time no matter how many object you have. You
    have this static method even if you have no objects at all. Static methods
    belongs only to the class and not to the object you have.
    They can only access static members.

    But assume this you have a class called Shape here and a method called draw
    which should be
    declared as virtual static and not pure virtual. So this class Shape is a
    concrete class.
    Assume you have a derived class called Circle and in this class you override
    this method draw.
    I know that this example is not so god because how do you draw a Shape. I
    just want to know
    if there is any contradiction to the language to have virtual static method.
    I mean that you inheric types and not object. You always have a type even if
    the method is declared static.

    //Tony
    Tony Johansson, Aug 21, 2005
    #1
    1. Advertising

  2. Tony Johansson

    Rolf Magnus Guest

    Tony Johansson wrote:

    > Hello Experts!
    >
    > I know that you can't have virtual static methods and I know what a static
    > method is.
    >
    > A static method exist only one time no matter how many object you have.
    > You have this static method even if you have no objects at all. Static
    > methods belongs only to the class and not to the object you have.


    Right.

    > They can only access static members.


    Well, unless they get an object somehow, like:

    class Foo
    {
    public:
    static void do_something(Foo& obj)
    {
    obj.x = 5;
    }

    int x;
    };

    > But assume this you have a class called Shape here and a method called
    > draw which should be declared as virtual static and not pure virtual. So
    > this class Shape is a concrete class.
    > Assume you have a derived class called Circle and in this class you
    > override this method draw.
    > I know that this example is not so god because how do you draw a Shape. I
    > just want to know if there is any contradiction to the language to have
    > virtual static method. I mean that you inheric types and not object. You
    > always have a type even if the method is declared static.


    Ok, let's assume you have the class Shape and some classes Circle, Rectangle
    and Triangle that are derived from it. Now you call Shape::draw(), which
    derived class draw() should be called and how should that be decided?
    Rolf Magnus, Aug 21, 2005
    #2
    1. Advertising

  3. On Sun, 21 Aug 2005 09:18:41 GMT, "Tony Johansson"
    <> wrote:

    >Hello Experts!
    >
    >I know that you can't have virtual static methods and I know what a static
    >method is.


    [snip...]

    >if there is any contradiction to the language to have virtual static method.
    >I mean that you inheric types and not object. You always have a type even if
    >the method is declared static.


    Google: "virtual static" (method OR function)

    --
    Bob Hairgrove
    Bob Hairgrove, Aug 21, 2005
    #3
    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. =?ISO-8859-1?Q?Thomas_Gagn=E9?=

    Why do static and non-static method names collide?

    =?ISO-8859-1?Q?Thomas_Gagn=E9?=, Jul 2, 2003, in forum: Java
    Replies:
    12
    Views:
    5,632
    cgbusch
    Jul 5, 2003
  2. Paul Opal
    Replies:
    12
    Views:
    948
    Paul Opal
    Oct 11, 2004
  3. Markus Dehmann
    Replies:
    5
    Views:
    2,687
    red floyd
    Jul 1, 2004
  4. jlopes
    Replies:
    7
    Views:
    422
    jlopes
    Nov 19, 2004
  5. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,845
    Smokey Grindel
    Dec 2, 2006
Loading...

Share This Page