473,407 Members | 2,598 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,407 software developers and data experts.

Virtual function ambiguity

Why static function can not be declared as virtual .
Apr 29 '07 #1
2 1319
bartonc
6,596 Expert 4TB
Moved to C++ from Member Introductions.
Apr 30 '07 #2
Ganon11
3,652 Expert 2GB
Suppose I had two classes like this:

Expand|Select|Wrap|Line Numbers
  1. class myClass {
  2.    public:
  3.       static void myStatFunc() const { std::cout << "This is a static function!" << std::endl; }
  4.       virtual void myVirtFunc() const { std::cout << "Hello, User!" << std::endl; }
  5. };
  6.  
  7. class myNewClass: public myClass {
  8.    public:
  9.       static void myStatFunc() const {std::cout << "This is a static function!" << std::endl; }
  10.       virtual void myVirtFunc() const { std::cout << "What's up?" << std::endl; }
  11. };
A static function is one that is defined only one time. It is universal to the whole class. So if I wanted to call the static function from myClass, I'd type myClass::myStatFunc()

The virtual keyword forces the computer to check the actual version of an object to see which class's function to call. So if I had the following object:

Expand|Select|Wrap|Line Numbers
  1. myClass *myClassPointer = new myNewClass;
and called myClassPointer->myVirtFunc(), even though myClassPointer is technically a myClass object, the myNewClass function is called - that is, "What's up?" is printed.

Now, suppose a static function was virtual. What would be the point? You would still call it by typing myClass::myStatFunc() or myNewClass::myStatFunc(), and the type would be determined by the left-hand side of the :: operator. virtual functions are still dependent on a certain object of a class; static functions are universal to the classes. Thus, the concepts cannot be used on the same function (though both static and virtual functions can be included in the same class, as shown above).
Apr 30 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: qazmlp | last post by:
My class in a header file, contains inline virtual destructor. Is this Ok? Can it cause any problems? class base { public: base() { } virtual ~base { std::cout<<"Inside virtual destructor\n";...
5
by: Squeamizh | last post by:
The subject line probably isn't the best, but I don't know how else to describe my question. When I try to compile the code below, my compiler reports an error (error text is below code sample)....
16
by: plmanikandan | last post by:
Hi, I have doubts reg virtual constructor what is virtual constructor? Is c++ supports virtual constructor? Can anybody explain me about virtual constructor? Regards, Mani
17
by: Michael | last post by:
Hi, Could you pleaes let me know when I need to use virtual destctor function in the base class? Thanks in advance, Michael
2
by: Ashwin | last post by:
hi guys, can anyone explain this class Base { public: virtual void foo() = 0; virtual void bar() = 0; };
12
by: mijobee | last post by:
I'm very new to c++ and just writing some code to learn. I've run into a problem, with a javaish design, and want to know if there is any possible solution without modifying the design. I've read...
3
by: Juha Nieminen | last post by:
It occurred to me while developing an application: What happens if two (completely independent) base classes have the exact same virtual function, and then a derived class is derived from both,...
1
by: chsalvia | last post by:
Is virtual inheritance ambiguity resolution ever performed statically, i.e. at compiler time? Or is it always resolved at runtime? In other words, if you use virtual inheritance, but don't use...
4
by: Anarki | last post by:
##include <iostream> using namespace std; class A { }; class B:virtual public A { }; class C:virtual public A
4
by: paul | last post by:
I was trying out the following sample code from Scotty Meyers: Effective C++ book: #include <iostream> class B { public: virtual void f() const { std::cout << "B::f()" << std::endl; } };
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.