Connecting Tech Pros Worldwide Forums | Help | Site Map

why not virtual static method

Tony Johansson
Guest
 
Posts: n/a
#1: Aug 21 '05
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



Rolf Magnus
Guest
 
Posts: n/a
#2: Aug 21 '05

re: why not virtual static method


Tony Johansson wrote:
[color=blue]
> 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.[/color]

Right.
[color=blue]
> They can only access static members.[/color]

Well, unless they get an object somehow, like:

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

int x;
};
[color=blue]
> 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.[/color]

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?

Bob Hairgrove
Guest
 
Posts: n/a
#3: Aug 21 '05

re: why not virtual static method


On Sun, 21 Aug 2005 09:18:41 GMT, "Tony Johansson"
<johansson.andersson@telia.com> wrote:
[color=blue]
>Hello Experts!
>
>I know that you can't have virtual static methods and I know what a static
>method is.[/color]

[snip...]
[color=blue]
>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.[/color]

Google: "virtual static" (method OR function)

--
Bob Hairgrove
NoSpamPlease@Home.com
Closed Thread