derived-class

c++ , pthread and static callbacks. “this” returns a pointer to the base class inctead of the derived one [duplicate]

∥☆過路亽.° 提交于 2019-12-12 06:03:16
问题 This question already has an answer here : c++ , pthread and static callbacks. “this” returns a pointer to the base class inctead of the derived one (part 2) (1 answer) Closed 3 years ago . I am using as basis this cpp thread class. This I use as a base class for threads. Note that in my case Thread::main() is a virtual function (unlike in the link). So I basically use: class Thread { public: virtual void main() { cout << "This should not be run once derived and redefined." << endl; } void

C#: Pass derived class as parameter

僤鯓⒐⒋嵵緔 提交于 2019-12-12 05:06:16
问题 I have a base class that does calculations on image sizes. I'm deriving a class from that and have predefined image sizes that will be used in my code. While what I have works, I have a strong feeling that I'm not doing it properly. Ideally, I'd like to just pass DerviedClass.PreviewSize as the parameter to GetWidth without having to create an instance of it. class Program { static void Main(string[] args) { ProfilePics d = new ProfilePics(); Guid UserId = Guid.NewGuid(); ProfilePics.Preview

How to access the properties of an instance of a derived class which is passed as a parameter in the form of the base class

余生颓废 提交于 2019-12-12 03:59:46
问题 In C# I have a base class and a derived class. I have a function which has the base class as an input parameter public void SomeFunction(BaseClass InstanceOfDerivedClass) Is there a way I can access the properties specific to the derived class, even though it has been passed as a base class? Could I use GetType or Cast or something like that? I appreciate that the solutions may not be elegant but at the moment the alternative is to repeat this function many times for the different derived

Controller Action with Derived Classes

僤鯓⒐⒋嵵緔 提交于 2019-12-11 19:56:20
问题 I have one base class and two derived classes: public class UserModel { public int Id {get; set; } public string Name {get; set; } public UserType UserType {get; set;} } public class StudentModel : UserModel { public string StudentProperty {get; set;} } public class TeacherModel : UserModel { public string TeacherProperty {get; set;} } In my controller ProfileController.cs I have the following two actions: public virtual ActionResult Detail(int id) { var userModel = _userService.Get(id);

Raise event when property is changed

浪子不回头ぞ 提交于 2019-12-11 18:06:38
问题 I need to raise an event when the value of the property is changed. In my case this is when webView.Source is changed. I can't make a derived class because the class is marked as sealed. Is there any way to raise an event ? Thank you. 回答1: Raise event when property is changed For this scenario, you could create a DependencyPropertyWatcher to detect DependencyProperty changed event. The follow is tool class that you could use directly. public class DependencyPropertyWatcher<T> :

“new” modifier causes base implementations to have NULL property values

久未见 提交于 2019-12-11 10:17:12
问题 I'm trying to use Polymorphism to have a derived class operate with a derived property instead of the base property. I'm not sure how to put it in clearer words, so here's an example with the output: // setup output to demonstrate the scenario static void Main(string[] args) { var foo = new Foo(); var foobase = foo as FooBase; Console.WriteLine("Foo is null? {0}", foo == null); Console.WriteLine("Foo.Bar is null? {0}", foo.Bar == null); Console.WriteLine("FooBase is null? {0}", foobase ==

Accessing private members of a derived class C++ [duplicate]

◇◆丶佛笑我妖孽 提交于 2019-12-11 10:08:09
问题 This question already has answers here : Can't access derived class method from pointer of type base class (3 answers) Closed 5 years ago . I am trying to access derived class' private members via an object of the base class. Here is what I'm trying to do : class A { private: int a, b; public: A(_a, _b) : a(_a), b(_b) {} int getA() { return a; } int getB() { return b; } }; class B : public A { private: int c; public: B(_a, _b, _c) : A(_a, _b), c(_c) {} int getC() { return c; } }; vector<A*>

Generating a derived class instance from a string of its name. (i.e, reflection)

和自甴很熟 提交于 2019-12-11 08:56:41
问题 If I have a base class: class Base { public: virtual void Test()=0; }; and, in a dynamically loaded module (.so/.dll), I implemented a class derived from this: class SomethingFromBase : Base { ... }; and, the user, once this library is loaded, asks to create an instance of SomethingFromBase (let's say we get the name from cin.), and, we have no knowledge of SomethingFromBase (i.e, no way to just do if(inputstr == "SomethingFrombase") { ... } is there any way to create an instance of

C++ maintaining a mixed collection of subclass objects

时光总嘲笑我的痴心妄想 提交于 2019-12-11 01:49:08
问题 Apologies if I'm missing a fairly fundamental concept here, but I'm trying to work out how to maintain a collection of multiple class types (all derived from the same parent) and still have access to their subclass-specific methods when retrieving them from the collection. As context, I have one base class ("BaseClass") and a number of classes (say "SubClassA" through "SubClassZ") that all derive from this base class. I also need to maintain a central indexed collection of all SubClass

creating a derived class object with existing base class object?

拜拜、爱过 提交于 2019-12-10 20:34:51
问题 Is there a possibility of (or fast workaround for) creating an object defined as derived a class without creating base class object in memory; instead the derived object should refer to the actually existing object of base class ("take-over" its memory residence)? This is needed for speed reasons - creating new a derived object, copying data from base class object to it, and then destroying base object takes too much time. 回答1: You might want to consider composition instead of inheritance in