non-virtual-interface

C# call an interface method non-virtual implementation

牧云@^-^@ 提交于 2019-12-12 11:19:51
问题 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"); } } class BAble : AAble { public void f() { Debug.Log("---->> B - Able"); } } execution code: IAble i = new BAble(); i.f(); On execution ---->> A - Able was printed. Why? How the compiler knows what function should be called? When the decision is made of what function to call -

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

怎甘沉沦 提交于 2019-12-07 03:05:35
问题 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 needed.) 回答1: I think the explanation is simply that in C#, "traditional" Java-style OOP is much more ingrained, and NVI runs counter to that. C# has a real interface type, whereas NVI relies on the "interface" actually being a base class. That's how it

No type named 'type' in CTRP derived class

ε祈祈猫儿з 提交于 2019-11-29 18:43:35
问题 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 Functor::type in the interface class. In the latter example, the compiler (gcc 5.4.0) reports error : no type named ' type ' in 'struct Cube< double >' template<class T, template<class> class Functor> class FunctorInterface_1 { private: const Functor<T> &f

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

倾然丶 夕夏残阳落幕 提交于 2019-11-29 11:35:58
In C++ an interface can be implemented by a class with all its methods 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 { public: virtual void method() = 0; }; : class Lib::Bar { public: void stuff( Lib::IFoo & ); }; Now I want to to use class Lib::Bar, so I have to implement the IFoo interface. For my purposes I need a whole of related classes so I would like to work with a base class that guarantees common behavior using the NVI idiom: class FooBase : public IFoo //

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

落爺英雄遲暮 提交于 2019-11-28 05:07:51
问题 In C++ an interface can be implemented by a class with all its methods 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 { public: virtual void method() = 0; }; : class Lib::Bar { public: void stuff( Lib::IFoo & ); }; Now I want to to use class Lib::Bar, so I have to implement the IFoo interface. For my purposes I need a whole of related classes so I would like to work

What is the point of a private pure virtual function?

≯℡__Kan透↙ 提交于 2019-11-26 19:15:05
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 ); } private: virtual void SetStateBool(int var, bool val ) = 0; virtual void SetStateInt(int var, int val ) = 0; }; To me, this implies that either the Engine class or a class derived from it, has to provide the implementation for those pure virtual functions. But I didn't think derived classes could have access to those private functions in order to reimplement them - so why make

What is the point of a private pure virtual function?

我是研究僧i 提交于 2019-11-26 06:53:21
问题 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 ); } private: virtual void SetStateBool(int var, bool val ) = 0; virtual void SetStateInt(int var, int val ) = 0; }; To me, this implies that either the Engine class or a class derived from it, has to provide the implementation for those pure virtual functions. But I didn\'t think