Questions tagged [derived-class]

In Object Oriented languages, derived class can inherit properties and/or member functions from a base class, also called super class. cf inheritance and polymorphism.

In Object Oriented languages, derived class can inherit properties and/or member functions from a base class, also called super class. cf inheritance and polymorphism.

1192 questions
6
votes
3 answers

Static Instance Base/Derived class

I would like to write a static instance property in a base class and derive this, but I am facing some problems. Here is the code for the base class - I currently have: public abstract class ResourceInstance { private static T _instance; …
Rand Random
  • 7,300
  • 10
  • 40
  • 88
6
votes
2 answers

Custom (derived) List

Feel free to load your guns and take aim, but I want to understand why you shouldn't do this. I have created a custom class designed to replace any instances of List (which I use to update XML objects behind them): public class ListwAddRemove :…
user1830285
  • 578
  • 8
  • 24
6
votes
2 answers

Virtual method called from derived instead of base

Can someone explain to me why is the overridden method being called when I cast the class into the base one: class Base { public virtual void VirtualMethod() { Console.WriteLine("Base virtual method"); } …
Thanatos
  • 1,176
  • 8
  • 18
6
votes
4 answers

Derived Class Constructor Calls

If I have a base class: class Base{ ... }; and a derived class class Derived : public Base{ ... } does this derived class always call the default constructor of the base class? i.e. the constructor that takes no parameters? For example If I…
kjh
  • 3,407
  • 8
  • 42
  • 79
6
votes
1 answer

Error: Invalid base class C++

Could anyone, please, explain what can cause this error? Error: Invalid base class I've got two classes where one of them is derived from second: #if !defined(_CGROUND_H) #define _CGROUND_H #include "stdafx.h" #include "CGameObject.h" class…
dziwna
  • 1,212
  • 2
  • 14
  • 25
6
votes
2 answers

override List with List

I have base classes like this: public class Scene { public IList Models {get; set;} } public class SceneModel { } and derived classes like this: public class WorldScene : Scene { public override IList Models {get;…
kaykayman
  • 373
  • 2
  • 13
6
votes
3 answers

Using a generic base class as a parameter of a method

I have the following classes public class A { } public class B : A { } public class C1 : B { } public class C2 : B { } What I would like to do, is have a method which can take any class derived from B, like C1 or C2 as a…
mat
  • 1,645
  • 15
  • 36
6
votes
2 answers

Overloading a method in subclass (Enum vs int)

Why the following two code samples produce different output? Case 1 enum EnumType { First, Second, Third } class ClassB { public string Func(int index) { return "Func(int)"; } public string Func(EnumType type) …
alexm
  • 6,854
  • 20
  • 24
5
votes
1 answer

Why is this compiling successfully?

What is the reason which why this code compile : #include using namespace std; class being { public: void running(char c) { cout << "No one know "; } }; class human :public being { public: using being::running; void…
5
votes
2 answers

Would a derived class ever have an implicit copy constructor or assignment operator when it's deleted in the base class?

Qt defines Q_DISABLE_COPY as follows: #define Q_DISABLE_COPY(Class) \ Class(const Class &) = delete;\ Class &operator=(const Class &) = delete; Q_DISABLE_COPY is used in the QObject class, but the documentation for it says that it should be…
5
votes
2 answers

create instance of unknown derived class in C++

let's say I have a pointer to some base class and I want to create a new instance of this object's derived class. How can I do this? class Base { // virtual }; class Derived : Base { // ... }; void someFunction(Base *b) { Base…
Ben
  • 4,486
  • 6
  • 33
  • 48
5
votes
1 answer

Automatic conversion from derived class to base class' member variable's type

Long story short: I'd like to understand why the D::operator B() const conversion operator is not used in the last line in the code below, which thus fails when compiling with g++ -std=c++17 source.cpp (compiling with g++ -std=c++2a deleteme.cpp is…
Enlico
  • 23,259
  • 6
  • 48
  • 102
5
votes
1 answer

Constraint syntax with generics, also deriving from a class

I'm having a heck of an issue with the following: I have a generic class, with a constraint, that derives from a non-generic interface: public abstract class DrilldownBase where W : class, IDrilldown This code is not correct through, because…
greggorob64
  • 2,487
  • 2
  • 27
  • 55
5
votes
2 answers

C# optional parameters: Why can I define the default different on interface and derived class?

With C#, we now can have optional parameters, and give them default values like this: public abstract class ImporterBase : IImporter { public void ImportX(bool skipId = true) { //.... } } Now, suppose in the interface we derive…
Marcel
  • 15,039
  • 20
  • 92
  • 150
5
votes
1 answer

C# - Readonly properties and fields cannot be set in derived class constructors

I have a class structure like the following abstract class AMyAbstractClass { public readonly int MyReadonlyField; public int MyReadonlyProperty { get; }//read-only auto-property (syntactic sugar) } class MyConcreteClass : AMyAbstractClass…
Elaskanator
  • 1,135
  • 10
  • 28