inheritance

How to override a virtual function with a non-virtual function?

↘锁芯ラ 提交于 2020-01-02 06:58:35
问题 Refer to this question: Hide virtual function with non-virtual override And this question: override on non-virtual functions A function that overrides a virtual function is virtual too, even though it's not explicitly declared virtual. My technical question is: Is there away to make that overriding function non-virtual (and applies that to classes lower in the hierarchy)? In other words, can I turn the "virtuality" off? Obviously we can override a non-virtual function with a virtual function.

Using base class as generic for IEnumerable<T>

☆樱花仙子☆ 提交于 2020-01-02 05:34:09
问题 I have a good understanding of OOP in general, inheritance and polymorphism, interfaces, etc. I encountered a strange situation and I don't understand why it does not work at all... EDIT : Ok, I found out that covariance (or contravariance?) may solve this problem, but crucially we're still using .NET 2.0 How can I solve this without moving to C# 4.0 ? Here is the situation. Given these two classes : public class CustomCollectionType<T> : IEnumerable<T> { /* Implementation here, not really

Are derived classes considered friends?

时光总嘲笑我的痴心妄想 提交于 2020-01-02 05:06:29
问题 If I create base class A and A is a friend of class B, can a class derived from A access B to its liking, or else what is it allowed? Thanks 回答1: struct A{}; struct Ader : A{}; struct B{ friend struct A; }; No. Friendship is not inherited in C++. It is also not transitive. Ader can not access B as a friend unless explicitly given friendship by B, just because it's base A is a friend of B . 回答2: No, that's not allowed. Check here. 来源: https://stackoverflow.com/questions/4083838/are-derived

Are derived classes considered friends?

て烟熏妆下的殇ゞ 提交于 2020-01-02 05:06:08
问题 If I create base class A and A is a friend of class B, can a class derived from A access B to its liking, or else what is it allowed? Thanks 回答1: struct A{}; struct Ader : A{}; struct B{ friend struct A; }; No. Friendship is not inherited in C++. It is also not transitive. Ader can not access B as a friend unless explicitly given friendship by B, just because it's base A is a friend of B . 回答2: No, that's not allowed. Check here. 来源: https://stackoverflow.com/questions/4083838/are-derived

“Overloading” pure virtual function with different set of arguments

妖精的绣舞 提交于 2020-01-02 04:53:10
问题 Consider following code sample #include <iostream> using namespace std; class Color { public: virtual void mixColors(Color &anotherColor) = 0; }; class RGB : public Color { public: void mixColors(RGB &anotherColor); }; void RGB::mixColors(RGB &kol) { return RGB(0xABCDEF); } I perfectly know why this code is not working ( mixColors() in RGB is not implementing pure virtual function, because it has different set of arguments). However I would like to ask if is there another approach to solve

Java: Overriding or Overloading method?

[亡魂溺海] 提交于 2020-01-02 04:48:05
问题 I have a method, in a class called "PlaceParser" that extends "ModelParser": protected Place parseModel(JSONObject element) ... A Place is a sub class of Model. Should the @Override annotation be added to the above code? As the method has a different return type, does this still count as overriding the base class method with the same name and arguments / does the return type alter the 'signature'? The "ModelParser" method looks like this "ModelT" also extends "Model": protected abstract

What can be the bad example of inheritance in Java?

試著忘記壹切 提交于 2020-01-02 04:45:11
问题 I know the advantages of inheritance in Java, but it is somewhat difficult for me to accept that it has disadvantages too. Can anybody give me a bad example of inheritance in Java? 回答1: Stack extends Vector . A stack is not a vector. Properties extends Hashtable . A table of properties is not a hash table. See this answer for a quote from Effective Java. It was easy to write the Stack implementation by using what is already implemented in Vector (likewise for Properties ), but it created

Cast to concrete class and call method in Java

二次信任 提交于 2020-01-02 03:56:08
问题 Lets say we have a baseclass called A and some subclasses ( B , C , D , etc.). Most subclasses have the method do() but the baseclass does not. Class AA provides a method called getObject() , which will create an object of type B , or C or D , etc., but returns the object as type A . How do I cast the returned object to the concrete type and call its do() method, if this method is available? EDIT: I'm not allowed to change the implementation of Class A , the subclasses or AA , since im using

Objective C class inheritance with factory methods

佐手、 提交于 2020-01-02 03:37:16
问题 I would like to inherit from a framework class that has a factory method. How can I make the factory method return an object of my inherited class type? I found this useful article which describe a similar situation but in their case you have control over the superclass. How could I write, say, a subclass of UIImage that imageNamed: would return an object of my subclass type? 回答1: I would like to inherit from a framework class that has a factory method. How can I make the factory method

C++ member function pointers in class and subclass

偶尔善良 提交于 2020-01-02 03:27:07
问题 I have one base class which holds a map for function pointers like this typedef void (BaseClass::*event_t)(); class BaseClass { protected: std::map<std::string, event_t> events; public: // Example event void onFoo() { // can be added easily to the map } }; Handling this works prefect, but now i want to make BaseClass an abstract base class to derive from like this: class SpecificClass : public BaseClass { public: void onBar() { // this is gonna be difficult! } }; Although i can access the map