answersLogoWhite

0


Best Answer
Answer

A virtual function must be declared as a non-static member method of a class that you expect to act as a base class. Declaring a virtual function adds some overhead as a result of creating the v-table (virtual method table), but most of that overhead is paid with the first virtual function (subsequent virtual functions just add a new entry to the already-existing v-table). However, do not declare a virtual function unless you expect that function to be overridden. Bear in mind that overriding an overloaded, non-virtual function "hides" all the overloads in the base class.

If a virtual function must be overridden, declare it as pure-virtual instead. You do not need to implement the method in the base class, but you will be reminded to provide an implementation in the derived class at compile time if one does not exist, even if you provide a default implementation in the base class. Bear in mind that base classes with one or more pure-virtual methods become abstract -- they cannot be instantiated.

If there is any virtual function or pure-virtual function, there must also be a virtual destructor, as well as a public or protected default constructor (a constructor with no arguments). When a derived class is constructed, it calls the base class constructor, which calls its base class constructor. Derived classes are constructed in sequence, beginning with the least-derived class. Destruction is the reverse -- the most-derived class is destroyed before the base classes are destroyed.

Virtual functions can be invoked just like any other class member method, both via an object reference's member operator (.), and the indirection operator (-->) for pointers to objects. It does not matter whether the reference or pointer refers to a base class or a derived class; the v-table decides which override (where one is provided) will actually execute, starting from the most-derived override and working back towards the base class, the least-derived. With appropriate use of virtual functions, dynamic casting can be avoided completely (dynamic casting should never be employed as it completely defeats the point of having a v-table in the first place).

Answer1.the virtual function should be a member of some class 2.they cannot be a static member

3.they are accessed by using object pointers

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Rules of virtual function
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Computer Science

What is the difference between a virtual channel and virtual path?

A virtual channel defines a single point to point connection, identified by its virtual channel identifier (VCI). A virtual path however, is a bundle of virtual channels that share the same end-point. Hence, a virtual path can be considered as a container that contains several virtual channels. Each virtual path is identified by its unique virtual path identifier (VPI). sridhara................


What does the virtual server 2005 do?

The Microsoft Virtual Sever deals with creating virtual machines for several Windows operating systems. The Virtual Server 2005 R2 SP1 is the latest version.


What does the computer use when RAM is full?

Virtual memory, the answer is virtual memory.


What happens if you have no virtual memory?

If you are using a modern version of Windows (Windows 95 or latter) you will be using virtual memory. Virtual memory is NOT the pagefile and it is not an extension to physical RAM but a system that is completely integrated into the system. Applications access virtual memory exclusively, no exceptions, ever. You can not disable virtual memory. You can disable the pagefile (not recommended) but this will not disable virtual memory. The system provides a virtual environment to processes that is completely independent of how much RAM is in the system. This is an advanced system that provides many important advantages to applications and users.


Is there any virtual graphics card like?

virtual? no. it needs to physically exist to do something

Related questions

A pure virtual function is a virtual function that has?

A pure-virtual function is a function that must be overridden in derived classes. You simply add "=0" to the end of the function declaration. class AbstractClass { public: virtual void DoSomething()=0; // Pure-virtual. };


What is virtual function table?

A virtual function table is a table of pointers to functions.


What is the difference between virtual function and function overriding?

Virtual Functions and Pure Virtual Functions are relevant in the context of class inheritance.Unlike Virtual Functions, Pure Virtual Functions do not require a body. This implies that when a base class defining such a function is inherited, the derived class must implement that function. Furthermore, the base class becomes abstract; meaning you cannot create an instance of the base class even if a body is implemented for the function. You are expected to derive from abstract classes; only the derived classes that implement all the inherited Pure Virtual functions can be instantiated.Here are some examples of Virtual and Pure Virtual function signatures:- Virtual Function: E.g. virtual void myFunction();- Pure Virtual Function: E.g. virtual void myFunction() = 0;


Can you have inline virtual functions in a class?

No, inlining is done at compile time whereas virtual functions are resolved at run time(late binding). So, virtual functions can't be inlined. Both properties are orthogonal.Inlining is a mere suggestion the compiler may ignore it if it is declared with virtual function.


The rules for constructing a function can be referred to as the function's?

Syntax refers to the rules for constructing a function.


Which binding virtual function used?

Virtual functions are dynamically bound at runtime.


How do virtual functions differ from pure virtual functions?

Virtual functions is a function that can be overridden in inheriting class with the same signature (function name, parameters number, parameters types and return type);Pure virtual function is function that does not have implementation and if class has pure virtual function is called abstract. It is not possible to instantiate that class. Some other class must inherit it and define the body for it (implement). In other words class only have function prototype/declaration(signature) and no definition(implementation).


What types of functions cannot be made virtual?

Static member functions, member function templates and constructors cannot be virtual.


What is meant by extension?

extensible means to enhance the program with new capabilities. In c++, extensibility can be achieved by using virtual function. now first we discuss what is virtual function in c++. " C++ virtual function is a member function of a class, whose functionality can be over-ridden in its derived classes. The whole function body can be replaced with a new set of implementation in the derived class." Through this u can be able to promote extensibility in your program by using Virtual function. Very simple concept.


How dynamic binding acheived in c plus plus?

Dynamic binding is achieved via virtual functions and the virtual table that is associated with every class that declares or inherits a virtual function. The virtual table (or v-table) maps every virtual function (including pure-virtual functions) to a function pointer that points to the most-derived overload. This makes it possible to invoke specific behaviour even when the runtime type of the object is unknown to the caller.


Would you be able to function in society without rules?

would we be able to function in society without rules


What is virtual function in c plus plus?

A virtual function is a member function of a class, whose functionality can be over-ridden in its derived classes. It is one that is declared as virtual in the base class using the virtual keyword. The virtual nature is inherited in the subsequent derived classes and the virtual keyword need not be re-stated there. The whole function body can be replaced with a new set of implementation in the derived class