derived-class

C# accessing protected member in derived class [duplicate]

梦想的初衷 提交于 2019-11-28 07:12:41
问题 This question already has answers here : Why can't I access C# protected members except like this? (7 answers) Closed 6 years ago . I wrote the following code: public class A { protected string Howdy = "Howdy!"; } public class B : A { public void CallHowdy() { A a = new A(); Console.WriteLine(a.Howdy); } } Now, in VS2010 it results in the following compilation error: Cannot access protected member 'A.a' via a qualifier of type 'A'; the qualifier must be of type 'B' (or derived from it). This

Get Caller derived-class when calling a base-class static method

大城市里の小女人 提交于 2019-11-28 01:27:18
问题 I was wondering if it's possible (even via reflection et similia) to get the caller derived-class inside of a called base-class static method. For example, I've a base-class with a static method defined: public MyBaseClass { public static void MyBaseClassStaticMethod() { /** ... **/ } } and a derived-from-it class: public MyDerivedClass : MyBaseClass { } then I call: MyDerivedClass.MyBaseClassStaticMethod() Is it possibile, inside of method MyBaseClassStaticMethod , to know which is the

In C# 4.0, is it possible to derive a class from a generic type parameter?

大兔子大兔子 提交于 2019-11-27 23:29:05
I've been trying this, but I can't seem to figure this out. I want to do this... public abstract class SingletonType<TSingleton, TBaseClass> : TBaseClass where TSingleton : TBaseClass, new() where TBaseClass : class { static TSingleton _singleton; public static TSingleton Singleton => _singleton ?? (_singleton = new TSingleton()); } The plan was to use it like this which would sort of 'wrap' the singleton pattern around a base class... public class SingletonFoo : SingletonType<SingletonFoo, Foo> { } However, I keep getting this Cannot derive from 'TBaseClass' because it is a type parameter Um.

C#: How do I call a static method of a base class from a static method of a derived class?

喜你入骨 提交于 2019-11-27 17:29:20
问题 In C#, I have base class Product and derived class Widget. Product contains a static method MyMethod(). I want to call static method Product.MyMethod() from static method Widget.MyMethod(). I can't use the base keyword, because that only works with instance methods. I can call Product.MyMethod() explicitly, but if I later change Widget to derive from another class, I have to revise the method. Is there some syntax in C# similar to base that allows me to call a static method from a base class

Why we do create object instance from Interface instead of Class?

大城市里の小女人 提交于 2019-11-27 17:03:07
I have seen many times an Interface instance generated from a class. Why does use an Interface in this way? An Interface instance created only itself with the help of the derived class and we can access only this interface members through this instance. How does this give an advantage? I'm so confused.. interface IPrint { void Print(); } class Sample : IPrint { public void Print() { Console.WriteLine("Print..."); } public void Sample() { Console.WriteLine("Sample..."); } } class Program { static void Main(string[] args) { IPrint print = new Sample(); print.Print(); } } Interfaces define that a

Deriving Class from Generic T

↘锁芯ラ 提交于 2019-11-27 14:56:43
问题 I have a parameterized hibernate dao that performs basic crud operations, and when parameterized is used as a delegate to fulfil basic crud operations for a given dao. public class HibernateDao <T, ID extends Serializable> implements GenericDao<T, ID> I want to be able to derive Class from T at runtime to create criteria queries in Hibernate, such that: public T findByPrimaryKey(ID id) { return (T) HibernateUtil.getSession().load(T.getClass(), id); } I know: T.getClass() does not exist, but

Disabling inherited method on derived class

跟風遠走 提交于 2019-11-27 13:55:25
Is there any way to, in a Java derived class, "disable" a method and/or field that is otherwise inherited from a base class? For example, say you have a Shape base class that has a rotate() method. You also have various types derived from the Shape class: Square , Circle , UpwardArrow , etc. Shape has a rotate() method. But I don't want rotate() to be available to the users of Circle , because it's pointless, or users of UpwardArrow , because I don't want UpwardArrow to be able to rotate. I don't think it is possible. However you can further refine the Shape class by removing the rotate()

How to resolve “pure virtual method called”

此生再无相见时 提交于 2019-11-27 12:59:35
问题 I understand why this is happening, but I'm stuck trying to resolve it...here is what my code is doing when the error is generated (thus, leading to a crash) when my program exits... pure virtual method called SomeClass::~SomeClass() { BaseClassObject->SomePureVirtualMethod(this); } void DerivedClass::SomePureVirtualMethod(SomeClass* obj) { //Do stuff to remove obj from a collection } I never have a call to new SomeClass but I have a QList<SomeClass*> which I append SomeClass* objects to. The

How to define sealed class in C++?

穿精又带淫゛_ 提交于 2019-11-27 11:11:56
How to stop the class to be inherited by other class. Nawaz C++11 solution In C++11, you can seal a class by using final keyword in the definition as: class A final //note final keyword is used after the class name { //... }; class B : public A //error - because class A is marked final (sealed). { // so A cannot be derived from. //... }; To know the other uses of final, see my answer here: What is the purpose of the "final" keyword in C++11 for functions? C++03 solution Bjarne Stroustrup's code : Can I stop people deriving from my class? class Usable; class Usable_lock { friend class Usable;

C++ Access derived class member from base class pointer

隐身守侯 提交于 2019-11-27 06:23:32
If I allocate an object of a class Derived (with a base class of Base ), and store a pointer to that object in a variable that points to the base class, how can I access the members of the Derived class? Here's an example: class Base { public: int base_int; }; class Derived : public Base { public: int derived_int; }; Base* basepointer = new Derived(); basepointer-> //Access derived_int here, is it possible? If so, then how? No, you cannot access derived_int because derived_int is part of Derived , while basepointer is a pointer to Base . You can do it the other way round though: Derived*