473,408 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

why not virtual static method

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
Aug 21 '05 #1
2 3142
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?

Aug 21 '05 #2
On Sun, 21 Aug 2005 09:18:41 GMT, "Tony Johansson"
<jo*****************@telia.com> 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
No**********@Home.com
Aug 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: john smith | last post by:
I'm wondering if it's possible to declare a pure virtual member function? Ie is: class A{ public: virtual static void f() const = 0; }; legal? I'm getting compile errors for code that used...
12
by: cppaddict | last post by:
Hi, I know that it is illegal in C++ to have a static pure virtual method, but it seems something like this would be useful when the following 2 conditions hold: 1. You know that every one...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
14
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using...
2
by: Tony Maresca | last post by:
Don't know if there are any Delphi/Object Pascal converts here, but being one, I sorely miss the feature that permits class (e.g., static) methods to be virtual and be overridden in derived...
0
by: Peter Morris [Droopy eyes software] | last post by:
I have solved this problem but it involved a static class, reflection, and a lot of code. I wanted to post it here because the solution using virtual class methods takes only a few lines of code....
8
by: Karsten Schramm | last post by:
Hi, when I run the following code: using System; namespace ConsoleApplication22 { class Program {
0
by: akshaycjoshi | last post by:
I am reading a book which says Even though unboxed value types don't have a type object pointer, you can still call virtual methods (such as Equals, GetHashCode, or ToString) inherited or...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.