base-class

Does a derived class object contain a base class object?

人盡茶涼 提交于 2019-12-19 04:51:24
问题 Consider the following sample code below: #include <iostream> using namespace std; class base { public: base() { cout << "ctor in base class\n"; } }; class derived1 : public base { public: derived1() { cout <<"ctor in derived class\n"; } }; int main() { derived1 d1obj; return 0; } Questions When d1obj is created, the constructors are invoked in the order of derivation : base class constructor is called first and then the derived class constructor. Is this done because of the following reason

Does a derived class object contain a base class object?

孤者浪人 提交于 2019-12-19 04:51:09
问题 Consider the following sample code below: #include <iostream> using namespace std; class base { public: base() { cout << "ctor in base class\n"; } }; class derived1 : public base { public: derived1() { cout <<"ctor in derived class\n"; } }; int main() { derived1 d1obj; return 0; } Questions When d1obj is created, the constructors are invoked in the order of derivation : base class constructor is called first and then the derived class constructor. Is this done because of the following reason

Can a Base Class Method return the type of the derived class?

丶灬走出姿态 提交于 2019-12-18 16:45:53
问题 Based on the other posts I have read, it seems that this may not be possible, but I thought I would post what I am trying to do and see if anyone knows of a solution. I am trying to add a "Clone()" method to classes generated from a Telerik Open Access domain model. No problem. I was able to figure out how to add a base class to the generated entity models so that I could identify those classes by their base class. (All entities inherit from a base class) I want ALL these entity model classes

How to call an explicitly implemented interface-method on the base class

£可爱£侵袭症+ 提交于 2019-12-17 18:16:12
问题 I have a situation, where two classes (one deriving from the other) both implement the same interface explicitly: interface I { int M(); } class A : I { int I.M() { return 1; } } class B : A, I { int I.M() { return 2; } } From the derived class' implementation of I.M() , I'd like to call the implementation of the base class, but I don't see how to do it. What I tried so far is this (in class B): int I.M() { return (base as I).M() + 2; } // this gives a compile-time error //error CS0175: Use

Will the base class constructor be automatically called?

你。 提交于 2019-12-17 08:16:17
问题 class Person { public int age; public Person() { age = 1; } } class Customer : Person { public Customer() { age += 1; } } Customer customer = new Customer(); Would the age of customer be 2? It seems like the base class's constructor will be called no matter what. If so, why do we need to call base at the end sometimes? public Customer() : base() { ............. } 回答1: This is simply how C# is going to work. The constructors for each type in the type hierarchy will be called in the order of

How to avoid error “Constructor on type 'MyType' not found” when inheriting a base class

核能气质少年 提交于 2019-12-13 11:52:51
问题 I have a Visual Studio 2010 Windows Forms app which includes a Form base class that other classes will inherit. The base class' constructor takes a parameter that the child classes will pass to the base class. Example: public partial class BaseForm : Form { public BaseForm(int number) { InitializeComponent(); } } public partial class ChildForm : BaseForm { public ChildForm(int number) : base(number) { InitializeComponent(); } } The problem that I'm running into is, when I attempt to open the

How to query for all base classes of a class at compile time?

谁说胖子不能爱 提交于 2019-12-12 13:08:08
问题 With std::is_base_of<A,B>::value one can check if a class A is a base class of class B . Is it also possible to query the compiler for all base classes of a class B , e.g., something like base_classes_of<B> returning a std::tuple containing all base classes of B ? Is there evtl. a non-standard extension in g++ that can accomplish this ? If this is not possible at all, does anyone know why? It sounds like a rather fundamental piece of information the compiler easily should have available?

How to make user control partial classes aware of controls declared in the base class?

假装没事ソ 提交于 2019-12-12 10:49:53
问题 Do we have to do something special to have ASP.NET partial classes aware of controls that are declared in our user control's base classes? The partial classes keep generating declarations for controls in the base class which mean the controls in the base class get hidden and are null. 回答1: The CodeFileBaseClass attribute can be applied to @Page or @Control declarations to make the ASP.NET runtime aware of any controls declared in your base class. MSDN describes it as follows: Specifies the

How to override parameter defined in interface method with richer type?

拜拜、爱过 提交于 2019-12-12 08:57:47
问题 I have these: public class TennisPlayer { } public class RogerFederer : TennisPlayer { } public class RafaelNadal : TennisPlayer { } And then I have some classes with methods, like these: public abstract class Manager { protected abstract void ScheduleFriendlies(TennisPlayer player); } public class RafaelNadalManager : Manager { public void ScheduleFriendlies(RafaelNadal rn) { //throw new NotClayException(); } } public class RogerFedererManager : Manager { public void ScheduleFriendlies

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