Questions tagged [non-virtual-interface]

The non-virtual-interface pattern is the use of an abstract-base-class with public non-virtual functions that delegate to (pure) virtual functions that can be overridden by derived classes.

The advantages are that the base class is in full control. It can add pre- and postcondition checking, or separate processing into more steps, or refactor, or implement a fuller interface/implementation separation using the Pimpl idiom. The non-virtual-interface acts similarly to the Template Method pattern.

24 questions
151
votes
6 answers

What is the point of a private pure virtual function?

I came across the following code in a header file: class Engine { public: void SetState( int var, bool val ); { SetStateBool( int var, bool val ); } void SetState( int var, int val ); { SetStateInt( int var, int val );…
BeeBand
  • 10,953
  • 19
  • 61
  • 83
32
votes
2 answers

Non-virtual interface design pattern in C#/C++

When designing an interface, someone recommended to use the non-virtual interface pattern. Can someone briefly outline what the benefits of this pattern are?
user705414
  • 20,472
  • 39
  • 112
  • 155
12
votes
4 answers

No type named 'type' in CTRP derived class

I've been experimenting with the Curiously Recurring Template Pattern for a generic single-argument functor and have two implementations: one using a template template parameter which works and a second where I try to access the derived…
Greg von Winckel
  • 2,261
  • 2
  • 16
  • 14
10
votes
2 answers

C++: Difference between NVI and Template Method Patterns?

What is the difference between NVI ( Non-Virtual Interface ) and the Template Method patterns? They seem very similar and I've read both that they're basically the same and that they're subtly different with Template being somehow more general.
7
votes
4 answers

Is the Non-Virtual Interface (NVI) idiom as useful in C# as in C++?

In C++, I often needed NVI to get consistency in my APIs. I don't see it used as much among others in C#, though. I wonder if that is because C#, as a language, offers features that makes NVI unnecessary? (I still use NVI in C#, though, where…
Johann Gerell
  • 24,991
  • 10
  • 72
  • 122
6
votes
2 answers

Under the NVI idiom, why can't the virtual function be public?

C++ private and protected virtual method and Is there any valid reason for not using public virtual methods? are talking about Non-Virtual Interface (NVI) and non-public virtual function and their symbiosis. Scott Meyers also says in Effective C++…
Rich
  • 1,669
  • 2
  • 19
  • 32
5
votes
4 answers

java final methods vs c++ nonvirtual functions

are java final methods and c++ nonvirtual methods different or the same? How?
Glove
  • 960
  • 6
  • 17
  • 30
5
votes
2 answers

nonvirtual interface idiom for more than two levels of inheritance?

The non-virtual interface idiom describes how the virtual methods are nonpublic customisation points, and public methods are nonvirtual to allow the base class to control at all times how the customisation points are called. This is an elegant…
andreas buykx
  • 12,608
  • 10
  • 62
  • 76
4
votes
2 answers

Does the Non-Virtual Interface idiom not apply to pure virtual functions?

I used to write interface classes like this: class SomeInterface { public: virtual void doThings() = 0; virtual void run() = 0; }; class OtherInterface { public: virtual void doStuff() = 0; virtual void run() = 0; }; Which meant…
Eternal
  • 2,648
  • 2
  • 15
  • 21
4
votes
4 answers

C# call an interface method non-virtual implementation

I am new to C# and I don't understand why compiler does not complain on this code. Here is the hierarchy of classes: interface IAble { void f(); } class AAble : IAble { public void f() { Debug.Log("---->> A - Able"); …
Narek
  • 38,779
  • 79
  • 233
  • 389
4
votes
4 answers

How to implement an interface class using the non-virtual interface idiom in C++?

In C++ an interface can be implemented by a class whose methods are pure virtual. Such a class could be part of a library to describe what methods an object should implement to be able to work with other classes in the library: class Lib::IFoo { …
andreas buykx
  • 12,608
  • 10
  • 62
  • 76
3
votes
1 answer

Using the non-virtual-interface idiom, can/will my non-virtual function be inlined ?

I recently decided to use the non-virtual interface idiom (NVI) to design an interface in C++, mostly for the purpose of using a parameter with a default value (thus avoiding problems caused by the fact that default parameters are statically…
JBL
  • 12,588
  • 4
  • 53
  • 84
2
votes
2 answers

Non-virtual methods, static binding, and interface in C#

I understand that non-virtual methods are statically bound, which means, as far as I understand, that its known at compile-time itself as to which method will be invoked on which object. This decision is taken based on the static type of the…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
2
votes
0 answers

Is it possible to write a c++ class template that automatically wraps and virtualizes non virtual interfaces?

The Problem When writing unit tests one often needs mock objects. In order to make the prodcution object replaceable, the mock object class derives from the production object class and overrides some virtual functions. The problem arrises when the…
Knitschi
  • 2,822
  • 3
  • 32
  • 51
2
votes
2 answers

Bringing non-virtual interfaces and multi-level inheritance together

The Non-virtual Interface idiome (NVI) is pretty self explanatory: You don't write public virtual functions, but public functions that call a private virtual implementation function, like so: class Object{ virtual void v_load(); public: void…
iFreilicht
  • 13,271
  • 9
  • 43
  • 74
1
2