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
2
votes
3 answers

Clone method with a non virtual interface and some private variables

I have an abstract class say Figure and some derived classes: Circle, Square, ... The class figure implements: private: virtual double varea()=0; double multiplier; public: virtual Figure * clone()=0; double area() { return…
DarioP
  • 5,377
  • 1
  • 33
  • 52
2
votes
1 answer

Private inheritance and non-virtual interfaces

So I've been interested in D for a while now and I messed about with it a while ago. I've started to look at it again and I really do like what it is trying to achieve, but I have a qualm about one favourite C++ design option of mine... non-virtual…
Dennis
  • 3,683
  • 1
  • 21
  • 43
1
vote
0 answers

NVI doesn't prevent name hiding; why not use virtual final instead?

Consider the following: // ConsoleApplication1.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include using namespace std; class Foo { public: // NVI bool method() { …
1
vote
4 answers

Non-virtual derivation: what do I really get from the compiler?

I am wondering what is produced by the compiler when using non-virtual derivation: template< unsigned int D > class Point { int[D]; // No virtual function // ... }; class Point2 : public Point<2> {}; class Point3 : public Point<3>…
Julien-L
  • 5,267
  • 3
  • 34
  • 51
1
vote
0 answers

Virtual template workaround for multi level NVI

I'm trying to build a class that will act as a base class for any type I want to serialize in a private project I'm doing. I'm trying to make the class work with at least boost serialization archives and QDataStream by providing functionality for…
user2962533
  • 397
  • 1
  • 5
  • 18
1
vote
4 answers

private overrides of private methods pattern? (ANSWER: NVI)

What's the accepted jargon (if any) for describing methods meant to be invoked only virtually and from other methods in the base? I've occasionally seen this referred to as a callback, but that seems to stray pretty far from the original definition…
0
votes
3 answers

Non-Virtual Interface - how to invoke the correct virtual function

I have a hierarchy that looks something like this: class Base { public: void Execute(); virtual void DoSomething() = 0; private: virtual void exec_(); }; class Derived : public Base { public: //DoSomething is implementation specific…
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
0
votes
2 answers

Android activity life cycle: why isn't calling the super method first enforced?

One of the requirements of basic Android development (according to the Google docs) is that when you override the activity's lifecycle methods (onCreate, onResume, onPause, etc.), you must call the parent's method first: @Override protected void…
orfdorf
  • 980
  • 9
  • 13
0
votes
2 answers

Adding invariants in non virtual interface idiom

Suppose I have the following hierarchy using the NVI idiom : class Base { public: virtual ~Base() {} void foo() { cout << "Base::foo" << endl; foo_impl(); } private: virtual void foo_impl() = 0; }; class A : public…
1
2