inheritance

Is type checking ever OK?

余生颓废 提交于 2019-12-24 12:23:08
问题 Is type checking considered bad practice even if you are checking against an interface? I understand that you should always program to an interface and not an implementation - is this what it means? For example, in PHP, is the following OK? if($class instanceof AnInterface) { // Do some code } Or is there a better way of altering the behaviour of code based on a class type? Edit : Just to be clear I am talking about checking whether a class implements an interface not just that it is an

VS2008 C++ can't seem to inherit const overloaded method [duplicate]

蓝咒 提交于 2019-12-24 12:18:14
问题 This question already has answers here : Derived class not inheriting overloaded method from base class (2 answers) Closed 4 years ago . I was just surprised to find that the following does not compile in VS2008. The compiler complains that it cannot convert parameter 1 from const int to int& thus demonstrating that it is failing to look at the available method in the Base class. Is there a good reason for this or is it a deficiency not found in other compilers? struct Base { virtual void

Assigning a Parent object to a Child object Without casting in Java

狂风中的少年 提交于 2019-12-24 12:07:46
问题 I have a parent interface and a child interface which objects will implement. I've made a child interface because I want a specific VehicleEntity object say Truck to add itself to the HashMap in Car . A Truck will call VehicleManager's addToCar() method which add the Truck object into Car's hashMap. The issue I have is CarEntity ce = ve; . Netbeans is telling me to cast ve to CarEntity but I don't want to. Shouldn't the line of code be valid (assuming the the object the for loop is looking at

Construct class from superclass instance

风流意气都作罢 提交于 2019-12-24 11:44:56
问题 So you have some function, say Gtk.Builder.get_object() , which returns some widget. In our case a Gtk.Window() . I have a subclass of Gtk.Window() which adds some signal handlers. class Window(Gtk.Window): Is it possible to use the widget returned by Gtk.Builder.get_object() to construct Window() ? I think it should be using __new__() or something, but I can't figure it out. 回答1: I think using __new__ is exactly what you want to be doing. If you can set the __class__ attribute of the

Mappings are inconsistent with each other. Single Table Inheritance

梦想的初衷 提交于 2019-12-24 11:43:59
问题 I get a lot of mapping errors in Symfony's profiler. I only have this problem with associations with the table inheritance super class. All the other entities and relations are just fine and working perfectly. Errors: ***\ArticleBundle\Entity\Article The mappings ***\ArticleBundle\Entity\Article#buyer and ***\UserBundle\Entity\User#boughtArticles are inconsistent with each other. The association ***\ArticleBundle\Entity\Article#category refers to the inverse side field ***\ArticleBundle

Why virtual destructor?

时光毁灭记忆、已成空白 提交于 2019-12-24 11:36:33
问题 I am going through some code,plan to adapt it for my research.So header file looks like this #ifndef SPECTRALCLUSTERING_H_ #define SPECTRALCLUSTERING_H_ #include <vector> #include <eigen3/Eigen/Core> class SpectralClustering { public: SpectralClustering(Eigen::MatrixXd& data, int numDims); virtual ~SpectralClustering(); std::vector<std::vector<int> > clusterRotate(); std::vector<std::vector<int> > clusterKmeans(int numClusters); int getNumClusters(); protected: int mNumDims; Eigen::MatrixXd

Type inheritance in function arguments

流过昼夜 提交于 2019-12-24 11:23:08
问题 I'm having an issue with type in functions, I've managed to write the minimal code that explains the problem: immutable Inner{B<:Real, C<:Real} a::B c::C end immutable Outer{T} a::T end function g(a::Outer{Inner}) println("Naaa") end inner = Inner(1, 1) outer = Outer(inner) g(outer) Will lead to the method error MethodError: no method matching g(::Outer{Inner{Int64,Int64}}) So basically, I don't want to have to say what the types of Inner are, I just want the function to make sure that it's

php dynamic class inheritance

前提是你 提交于 2019-12-24 11:19:05
问题 I know I can generate a class at runtime by executing $obj = (object)array('foo' => 'bar');+ this way I can use echo $obj->foo; //bar What if want to make $obj inherits from an existing class? What I wanna achive: I'm forking paris project on github (https://github.com/balanza/paris). It's an active record class. I wonder I need to declare a class for every object, even if it's empty: class User extends Model{} I guess I might use dynamic object to avoid this boring stuff. 回答1: You could

Is there a convenient way to map all interface methods onto a subobject?

巧了我就是萌 提交于 2019-12-24 11:17:15
问题 Suppose I need to inherit from two classes in C#. This is not allowed, so I can do the following: inherit from one of the classes and include the other class as a member variable, inherit from its interfaces and reimplement all methods of those interfaces by redirecting them onto that member variable: interface SecondBaseInterface { void FirstMethod(); void SecondMethod(); }; class MyClass : FirstBaseClass, SecondBaseInterface { public void FirstMethod() { secondBase.FirstMethod(); } public

How to override member of base class after inheritance in C++

北慕城南 提交于 2019-12-24 11:11:44
问题 i have this : class A { public : A(int i ) : m_S(i) { m_Pa = new Foo(*this) ; } private : int m_S ; Foo* m_Pa; } and derived class class B : public A { public : B() : A (242) { // here i like to override the A class m_Pa member but i don't know how to do it right } } 回答1: your m_Pa should be protected than you can call like: B() : A (242), m_Pa(12) { } or B() : A (242) { m_PA = 55 } or you should make a public or protected function which changes m_Pa class A { public : A(int i ) : m_S(i) { m