answersLogoWhite

0


Best Answer

Virtual functions have many advantages, but here are a couple of their disadvantages:

  • slower -- The function call takes slightly longer due to the virtual mechanism,and it also makes it more difficult for the compiler to optimize because it doesn't know exactly which function is going to be called at compile time.
  • harder to debug -- In a complex system, virtual functions can make it a little more difficult to figure out where a function is being called from. Or, to figure out why a function isn't being called if someone overrode it with a new virtual function.
User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

A function must return a result. For it to be useful it must be part of an expression, i.e., not called as a standalone routine. For example, suppose you have a function called 'sum' that adds two numbers and returns the result.

The following code would make no sense because you don't keep the result:

sum (a, b) ;

It would be better to use it as:

result = sum (a,b) ;

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

The only disadvantage to a function is that it incurs a function call. However, simple function calls can be optimised away by the compiler if the function can be inline expanded. Usually the function needs to be simple, one or two statements at most, or one that is called infrequently. If the function is implemented at the point of declaration then it is automatically a candidate for inline expansion, otherwise you must precede the function's implementation with the inline keyword. However, declaring a function inline is no guarantee it will be inline expanded, it is merely a hint to the compiler that the function is a candidate for inline expansion. Declaring every function inline will only result in a much slower compilation and if the increased code size would be detrimental to overall performance then the compiler may decide the expense of a function call far outweighs any lost performance from inline expansion. Some compilers may allow you to force an inline expansion, but there are still certain functions that cannot be inline expanded, such as recursive functions with a depth greater than n recursions, where n is usually limited to 16. All in all it's generally best to let the compiler decide as its inline optimisers are highly advanced.

The main advantage of a function is that it helps streamline your code, making it more readable and much easier to manage. Unlike a goto statement, which branches off never to return (known as spaghetti code), a function call always returns to the caller, and can return a value if necessary (as well as returning additional values through output parameters). If you have a complex statement or series of statements that are called often then turning those statements into a function means you only have to call the function rather than continually retype the same statements over and over. Not only do you eliminate the chance of a typo, you reduce code maintenance because the code is all in one place. If you need to modify the code, you only have to do it once, rather than for every occurrence (which may lead to errors if you forget to update all occurrences).

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

The only disadvantage to a function is in actually calling the function. If the function can't be inline expanded, the overhead in making the call may be detrimental to performance and memory consumption (depending on the number and type of parameters it accepts). However, even for non-inline expanded functions, the advantage of having common code in one place rather than in several more than offsets any disadvantages incurred by making the call. Updating one function is much easier than trying to locate and edit all instances of common code. Of course, if the compiler can generate inline expanded code for a function, then so much the better. The only disadvantage is increased code size, but that's hardly a problem when the advantage is better performance.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Only that it's easy to get carried away writing new function signatures that may never be used in your programs. Although it can increase the flexibility of your function, there's little point in writing overloads you'll never use. Although the compiler can ignore them, you'll have wasted time writing them in the first place. Other than that, there are no disadvantages.

Note that if the only difference is the type of parameter rather than the number of parameters, function templates are a much better option. The compiler generates the overloads for you, on an as-required basis, and you've only got one function to maintain.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

The only disadvantage to inline functions is the increased code size, which can be detrimental to performance, even with the reduced number of function calls. However, declaring a function inline is no guarantee it will be inline expanded; the compiler is free to ignore the declaration if the increased code size would prove detrimental to code performance. The only functions that are guaranteed to be inline expanded are those declared in macros, hence non-trivial macros are generally frowned upon.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Disadvantages of using functions in c:

1)The execution time of function is higher.

2)Recursion is the biggest problem associated with functions.

This answer is:
User Avatar

User Avatar

jyoti gupta

Lvl 2
4y ago

using function unneccessarily will slow your program down...

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

disadvantages

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are disadvantages of virtual functions in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you represent a C plus plus non-pure virtual function in UML?

All virtual functions (including pure-virtual functions) are represented in italics. All non-virtual functions are represented normally. There is no differentiation between pure and non-pure virtual functions, however some people append "=0" to distinguish the pure-virtual functions.


Why functions are not used in c plus plus?

Of course they are used. Both stand-alone and class-member functions are used in C++.


Why does VisualStudio c plus plus not have an option to give functions It is in VS c Sharp?

Turn on the intellisense feature.


Why Member functions are not virtual by default?

It depends which language you are using. Java member functions are virtual by default but C++ member functions are not. Java takes the viewpoint that if any member function is declared virtual then all member functions should be declared virtual, so they may as well be virtual by default. However, C++ takes the view that a member function should only be declared virtual if there's a specific reason to declare it virtual. Not all functions are meant to be overridden. Indeed, not all classes are meant to act as base classes. So all member functions are non-virtual by default. Purists will argue that the C++ method is the correct method. After all, there's no point in having a virtual-table if it's never going to be used. Java places the onus on the programmer to eliminate an unused virtual-table, whereas C++ simply doesn't provide one unless you explicitly declare one. However, the real reason C++ uses non-virtual methods by default is because it has to maintain compatibility with a C struct. A C struct is not a class so it has no methods (and therefore no virtual methods). It is a "plain-old-data" or POD structure. In C++, however, a struct is a class. As such, by default, it has a compiler-generated default constructor, default copy and move constructors, default copy and move assignment operators and a default destructor. It also has public access by default. However, because the compiler-generated methods are all trivial member-wise implementations, a C++ struct is backwardly compatible with a POD. Thus C code can use a C++ struct just as if it were a C struct, because both use POD structures by default. If C++ used virtual member functions by default, a struct would not be a POD by default, it would be a base class by default.


Does the C plus plus programming language use a virtual machine?

No, it does not. But Microsoft Visual Studio 2008 allows you to connect to a virtual machine and run your projects "sandboxed".

Related questions

How do you represent a C plus plus non-pure virtual function in UML?

All virtual functions (including pure-virtual functions) are represented in italics. All non-virtual functions are represented normally. There is no differentiation between pure and non-pure virtual functions, however some people append "=0" to distinguish the pure-virtual functions.


Can there be friend functions in c plus plus?

Yes, there can be friend functions in C++.


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.


Printf and scanf Operators in C and C plus plus?

No, they are functions. Operators are -> or ++or /=


Why functions are not used in c plus plus?

Of course they are used. Both stand-alone and class-member functions are used in C++.


What is a method in c plus plus?

In C++, methods are simply class member functions.


How many main functions are used in c plus plus program?

One.


What are the in-built function in c plus plus?

C++ built-in functions are those functions that are provided for you as part of the language itself, and includes all of the C standard library functions (all of which were inherited from C) and is expanded upon by the C++ standard template library. C++ implementors may provide additional functions that are platform-specific, however these are not considered built-in functions becuase C++ is a cross-platform language. These are best described as 3rd party functions. The functions you yourself write are known as user-defined functions.


How do you link a C plus plus program to C functions?

It should work without any special action.


Why does VisualStudio c plus plus not have an option to give functions It is in VS c Sharp?

Turn on the intellisense feature.


Why Member functions are not virtual by default?

It depends which language you are using. Java member functions are virtual by default but C++ member functions are not. Java takes the viewpoint that if any member function is declared virtual then all member functions should be declared virtual, so they may as well be virtual by default. However, C++ takes the view that a member function should only be declared virtual if there's a specific reason to declare it virtual. Not all functions are meant to be overridden. Indeed, not all classes are meant to act as base classes. So all member functions are non-virtual by default. Purists will argue that the C++ method is the correct method. After all, there's no point in having a virtual-table if it's never going to be used. Java places the onus on the programmer to eliminate an unused virtual-table, whereas C++ simply doesn't provide one unless you explicitly declare one. However, the real reason C++ uses non-virtual methods by default is because it has to maintain compatibility with a C struct. A C struct is not a class so it has no methods (and therefore no virtual methods). It is a "plain-old-data" or POD structure. In C++, however, a struct is a class. As such, by default, it has a compiler-generated default constructor, default copy and move constructors, default copy and move assignment operators and a default destructor. It also has public access by default. However, because the compiler-generated methods are all trivial member-wise implementations, a C++ struct is backwardly compatible with a POD. Thus C code can use a C++ struct just as if it were a C struct, because both use POD structures by default. If C++ used virtual member functions by default, a struct would not be a POD by default, it would be a base class by default.


Does the C plus plus programming language use a virtual machine?

No, it does not. But Microsoft Visual Studio 2008 allows you to connect to a virtual machine and run your projects "sandboxed".