Questions tagged [derived]

429 questions
77
votes
4 answers

How to avoid memory leaks when using a vector of pointers to dynamically allocated objects in C++?

I'm using a vector of pointers to objects. These objects are derived from a base class, and are being dynamically allocated and stored. For example, I have something like: vector Enemies; and I'll be deriving from the Enemy class and then…
akif
  • 12,034
  • 24
  • 73
  • 85
73
votes
10 answers

C# - Can publicly inherited methods be hidden (e.g. made private to derived class)

Suppose I have BaseClass with public methods A and B, and I create DerivedClass through inheritance. e.g. public DerivedClass : BaseClass {} Now I want to develop a method C in DerivedClass that uses A and B. Is there a way I can override methods…
Lyndon
  • 899
  • 3
  • 8
  • 9
57
votes
3 answers

When is upcasting illegal in C++?

I am pretty sure I understand the general difference between upcasting and downcasting, particularly in C++. I understand that we can't always downcast because casting a base class pointer to a derived class pointer would assume that the base class…
Connor Foley
  • 568
  • 4
  • 9
34
votes
6 answers

Overriding an abstract property with a derived return type in c#

I have four classes. Request, DerivedRequest, Handler, DerivedHandler. The Handler class has a property with the following declaration: public abstract Request request { get; set; } The DerivedHandler needs to override this property so that it…
Trevor
  • 13,085
  • 13
  • 76
  • 99
30
votes
3 answers

Use of "Public" in a derived class declaration?

Given this base class: class Employee { char* name; int age; public: Employee(char* name); void print(); }; With regards to the "public", what's the difference between this: class Manager : public Employee { EmployeeList…
user189515
26
votes
2 answers

Does a derived class automatically have all the attributes of the base class?

There seems to be no good online documentation on this: If I make a derived class, will it automatically have all the attributes of the base class? But what's the BaseClass.__init() for, do you also need to do it to other base class methods? Does…
liamel1i
  • 261
  • 1
  • 3
  • 3
21
votes
2 answers

Can I access a base classes protected members from a static function in a derived class?

I have a program where I need to make a base class which is shared between a dll and some application code. Then I have two different derived classes, one in the dll one in the main application. Each of these have some static member functions which…
David Snape
  • 225
  • 2
  • 9
21
votes
2 answers

Redefining vs. Overriding in C++

I am confused about the differences between redefining and overriding functions in derived classes. I know that - In C++, redefined functions are statically bound and overridden functions are dynamically bound and that a a virtual function is…
YelizavetaYR
  • 1,611
  • 6
  • 21
  • 37
20
votes
7 answers

Why would the conversion between derived* to base* fails with private inheritance?

Here is my code - #include using namespace std; class base { public: void sid() { } }; class derived : private base { public: void sid() { } }; int main() { base * ptr; ptr = new derived; // error:…
Bruce
  • 33,927
  • 76
  • 174
  • 262
18
votes
2 answers

C# XML serialization of derived classes

Hi I am trying to serialize an array of objects which are derived from a class and I keep hitting the same error using c#. Any help is much appreciated. obviously this example has been scaled down for the purpose of this post in the real world Shape…
111111
  • 15,686
  • 6
  • 47
  • 62
9
votes
5 answers

Why explicit conversion required to assign base class to derived class? But not required to do the reverse

I have a base class and a derived class as bellow public class animal { public string name { get; set; } } public class dog : animal { public int age { get; set; } public string type { get; set; } } uses: animal a = new animal(); dog…
paul sim
  • 463
  • 2
  • 10
  • 23
9
votes
2 answers

singleton template as base class in C++

According to C++ Singleton design pattern I wrote a singleton template template class Singleton { public: static T& getInstance() { static T instance; return instance; } …
JCRunner
  • 125
  • 1
  • 1
  • 7
9
votes
5 answers

Get derived type via base class virtual function

I am trying to get the derived type of an object via a base class virtual function. I have written this, which does not compile: struct base { virtual base& get_this() { return *this; } }; struct derived : base { virtual derived&…
linuxfever
  • 3,763
  • 2
  • 19
  • 43
9
votes
3 answers

Can Eclipse be configured to disable warnings for derived source files?

I am using ANTLR to generate Java source files. I can make Eclipse understand the generated files are derived, but it still gives me warnings about harmless things (e.g. unnecessary imports and so on). I would like to configure Eclipse to ignore…
Gabe Johnson
  • 1,393
  • 4
  • 16
  • 28
9
votes
2 answers

c++ custom output stream with indentation

I'm having some trouble trying to implement a custom stream class to generate nicely indented code in an output file. I've searched online extensively but there doesn't seem to be a consensus on the best way to achieve this. Some people talk about…
user1898153
  • 463
  • 6
  • 12
1
2 3
28 29