derived-class

What is the difference between a child of a parent class and the derived of a base class in VB.NET or C#?

孤者浪人 提交于 2019-12-21 09:07:55
问题 After asking the question Call a method that requires a derived class instance typed as base class in VB.NET or C# on Stack Overflow, I was informed that I had used the wrong terms when asking the question. I had used "parent" and "child" where I should have used "base" and "derived" instead. I have been unable to find a good description of the difference. This is what I know (or think I know) so far: A parent class contains the child class. Where as a derived class inherits from a base class

How could an instance of the base class hold an instance of the derived class?

核能气质少年 提交于 2019-12-21 04:24:16
问题 I have been a .Net coder (can not say I am a programmer) for 2 years. There is one question that I can not understand for years, that is how could an instance of the base class hold an instance of the derived class? Suppose we have two classes: class BaseClass { public A propertyA; public B propertyB; } class DerivedClass :BaseClass { public C propertyC; } How could this happen: BaseClass obj = new DerivedClass () I mean, the memory model of BaseClass , has no space for the newly added

Boost serialization of derived class with private members

让人想犯罪 __ 提交于 2019-12-20 04:48:09
问题 I try to serialize a class, say B (in file b.h), which is derived from another one, say A (in file a.h). Both classes have private members and I want to serialize both with the boost serialization library non-intrusively. The serialization/deserialization of A does work so far. For the same for the derived class one would use ar & boost::serialization::base_object<base_class>(*this); when the intrusive method is used, but where to put it in the non-intrusive case (save/load/serialize or all

Class base() constructor and pass this

不羁岁月 提交于 2019-12-19 20:43:42
问题 I am trying to implement good design patterns for a program I am writing. I have a class structure like this. abstract class SomeBase { public SomeObject obj { get; protected set; } protected SomeBase(SomeObject x) { obj = x; } //Other methods and fields... } public class SomeDerived : SomeBase { public SomeDerived() : base(new SomeObject(this)) { } } Now as I;m sure you now, you can't pass this in a contructor because the object hasn't bee initialized. But I was really hoping there was a

How do we call a virtual method from another method in the base class even when the current instance is of a derived-class?

喜夏-厌秋 提交于 2019-12-19 20:15:29
问题 How do we call a virtual method from another method in the base class even when the current instance is of a derived-class? I know we can call Method2 in the Base class from a method in the Derived class by using base.Method2() but what I want to do is calling it from the other virtual method in the Base class. Is it possible? using System; namespace ConsoleApplication1 { class Program { static void Main( string[] args ) { Base b = new Derived( ); b.Method1( ); } } public class Base { public

Simpler “Preventing derived classes” in C++

感情迁移 提交于 2019-12-19 16:34:09
问题 Going under the assumption that there is a legitimate reason for preventing derivation from some class, Bjarne gives a solution here for the answer to "Can I stop people deriving from my class?" However, I thought of: class final { protected: final() { } // line 3 }; class B : private virtual final { }; class D : public B { // line 9 }; int main() { B b; D d; // line 14 } When trying to compile, one gets: foo.cpp: In constructor ‘D::D()’: foo.cpp:3: error: ‘final::final()’ is protected foo

Can I have a base class where each derived class has its own copy of a static property?

冷暖自知 提交于 2019-12-19 07:49:48
问题 I have something like the following situation below: class Base { public static int x; public int myMethod() { x += 5; return x; } } class DerivedA : Base { } class DerivedB : Base { } I am trying to set this up so that each derived class has its own static instance of x, if I do something like this: DerivedA.x = 5; DerivedB.x = 10; then when I run: DerivedA.myMethod(); //The result will be 10 DerivedB.myMethod(); //The reusult will be 15 Can i do something like this? How can I setup the

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

Maybe my understanding of [class.access]/7 isn't correct, but

社会主义新天地 提交于 2019-12-18 15:17:10
问题 From [class.access]/7 we have the following sentence: Similarly, the use of A::B as a base-specifier is well-formed because D is derived from A , so checking of base-specifier s must be deferred until the entire base-specifier-list has been seen. class A { protected: struct B { }; }; struct D: A::B, A { }; See live example with clang. As a matter of fact, clang also complains about this snippet, where no deferment is necessary. class A { protected: struct B { }; }; struct D: A, A::B { }; Why