inheritance

Accessing indirect super class variable that has been hidden in a third extended class

久未见 提交于 2020-01-11 12:05:14
问题 Suppose i have a code as below : class A { int a = 1; } class B extends A { int a = 2; } class C extends B { int a = 3; void print_it() { int a = 4; // Local variable "a" to the " print_it " method System.out.println(a); //Should be 4 System.out.println(this.a); //Should be 3 System.out.println(super.a); //Should be 2 System.out.println("HOW DO I PRINT \" a \" OF THE \" CLASS A \" "); //I need to print 1 } public static void main(String[] argue) { C obj = new C(); obj.print_it(); } } How can

Force template parameter class to inherit from another templated class with partially fulfilled parameters

╄→гoц情女王★ 提交于 2020-01-11 11:56:48
问题 So I have the following two classes: template < class Cost > class Transformation { public: virtual Cost getCost() = 0; }; template < class TransfCl, class Cost > class State { protected: State(){ static_assert( std::is_base_of< Transformation< Cost >, TransfCl >::value, "TransfCl class in State must be derived from Transformation< Cost >" ); } public: virtual void apply( const TransfCl& ) = 0; }; But would like to, instead, be able to drop the Cost template parameter to State , because State

Virtual functions with varying of function parameters in derived class by const type in “function parameter” will break virtual mechanism? [duplicate]

末鹿安然 提交于 2020-01-11 11:41:28
问题 This question already has answers here : Top-level const doesn't influence a function signature (7 answers) const qualifier disappears from pure virtual function [duplicate] (1 answer) Closed 2 years ago . A C++ guru told that varying the function parameter type with const in derived class, will break the virtual call mechanism. I tried a simple program ( forgive for non standard code, written purely for test) , which prove otherwise. The function parameter change by const value will not

C#, access child properties from parent reference?

强颜欢笑 提交于 2020-01-11 11:28:25
问题 public void GetProps(Parent p){ // want to access lots of child properties here string childProp1 = p.prop1; bool childProp2 = p.prop2; bool childProp3 = p.prop3; } However compiler complains that "Parent does not contain definition prop1" The function would take in different subtypes of Class Parent. All the subclasses have this public override string prop1 { get; set; } Is there a way of accomplishing this? EDIT: To make the question clearer I current have a giant if-elseif where i do

twig inheritance and symfony2 controller variables

安稳与你 提交于 2020-01-11 09:50:20
问题 Im trying my first project using symfony2 + twig. I created basic twig template with defined blocks. It basically looks like this {% block content %} some content... {% endblock %} {% block footer %} {{ footer.content}} {% endblock %} I want footer to be same for all pages. Footer is loaded from DB and its set in controller. I wanted inherit from template described above to other pages but I have to always set footer in controller otherwise variable is not defined. My questions is if exists

Java inheritance: How to achieve something similar to “multiple inheritance” when it is not allowed in Java?

半腔热情 提交于 2020-01-11 08:59:14
问题 This is a question mostly about Java inheritance. I am developing a program which has 2 windows, both of which will be developed in separate classes which will extend JPanel. The first class is "FileSub1" and the second one is "FileSub2". There are a lot of methods that are common to these two classes, so I would like to create a class called "Files" and make "FileSub1" and "FileSub2" its subclasses. But Java doesn't support multiple inheritance! What can I do here? 回答1: I don't see why you

Optional parameters and inheritance

孤人 提交于 2020-01-11 08:43:47
问题 I understand optional parameters, and I quite like them, but I'd just like to know a bit more about using them with an inherited interface. Exhibit A interface IMyInterface { string Get(); string Get(string str); } class MyClass : IMyInterface { public string Get(string str = null) { return str; } } Now I would've thought that the Get method in MyClass inherits both of the interface's methods, but... 'MyClass' does not implement interface member 'MyInterface.Get()' Is there a good reason for

Why Java does not allow to extend array type

丶灬走出姿态 提交于 2020-01-11 08:35:10
问题 As an experiment, I tried to extend an int -array like this: public class IntArrayExtension extends int[]{ // additional fields and methods. } to add some methods related to sorting, swapping, sub-array building etc. in the class itself. But I got this error while compiling: IntArrayExtension.java:1: unexpected type found : int[] required: class public class IntArrayExtension extends int[]{ ^ 1 error I am curious to know: why Java does not allow to extend an array? 回答1: Extending a

Ambiguous definition of operator() with multiple inheritance

旧时模样 提交于 2020-01-11 08:23:06
问题 I compile this code with GCC (4.2.1 Apple build 5664) #include <cstddef> using std::size_t; template <char I> struct index { }; struct a { void operator()(size_t const &) { } }; struct b { template <char I> void operator()(index<I> const &) { } }; struct c: public a, public b { }; int main (int argc, char const *argv[]) { c vc; vc(1); return 0; } and give me the following error: main.cpp: In function ‘int main(int, const char**)’: main.cpp:22: error: request for member ‘operator()’ is

Ambiguous definition of operator() with multiple inheritance

孤者浪人 提交于 2020-01-11 08:23:05
问题 I compile this code with GCC (4.2.1 Apple build 5664) #include <cstddef> using std::size_t; template <char I> struct index { }; struct a { void operator()(size_t const &) { } }; struct b { template <char I> void operator()(index<I> const &) { } }; struct c: public a, public b { }; int main (int argc, char const *argv[]) { c vc; vc(1); return 0; } and give me the following error: main.cpp: In function ‘int main(int, const char**)’: main.cpp:22: error: request for member ‘operator()’ is