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.
1,141
questions
1
vote
0
answers
23
views
Best way to store std::vector of derived class in a host parent class
I want to store a std::vector<> containing objects which have a common base class, within a host class. The host class should remain copiable since it is stored inside a std::vector<> of ...
-4
votes
0
answers
48
views
Program stops compiling when I add function to a class [duplicate]
I have a virtual base class, base_class, that uses as attribute a class type object of class colours. From the virtual base class, I derived another class, derived_class.
The debugger doesn't show any ...
0
votes
1
answer
50
views
C++ map with constructor and parameter
I want to have an unordered map with a string and a std::function that call a constructor a class to make a shared_ptr. But I'm stuck, I don't know what I should add as a second member of my map I've ...
0
votes
0
answers
33
views
How to construct a certain derived class by calling the base class constructor with a certain argument
So I want to know if its possible to create a certain derived class by constructing the base class with a certain argument. For example:
lets say;
class Base
{
Base::Base(char derived_selector){......
3
votes
0
answers
44
views
How should I write my Factory Class - Generates derived objects
To start, I'll lay out my general setup and describe the goal of the classes, as I feel its important for the question:
This is designed to dynamically cache files and folders as needed by the ...
0
votes
1
answer
34
views
DElem<T,N> derives from BElem<T> and DContainer<DElem<T,N>> derives from BContainer<BElem<T>> How to code it?
The question is easy to explain in code.
I have coded several template classes that they derive from a unique template class:
template<typename T,unsigned N>
struct DElem :
public BElem<...
0
votes
2
answers
32
views
Access attribute of derived class when base class instance is passed
I have got an Array of a Base-Class that contains objects of two derived classes.
Problem: How do I access the attributes of one of the derived classes when the passed object does not specify the type?...
1
vote
1
answer
65
views
In C#, get a List<DerivedClass> from a List<BaseClass> [duplicate]
I have a Class, Food which has two derived classes: Meat and Vegetables.
If I have a List of Foods, I can get a (sub)list of Foods that are Meat using
List<Food> allTheFood = GetListOfFood();
...
-1
votes
0
answers
24
views
Passing in a derived class as a parameter to a generic function [duplicate]
Goal is to pass in an object, given it is derived from a base class, as a parameter to a generic function. I think I am misunderstanding something about go.
I have a Derived class
type UserItem struct ...
0
votes
1
answer
52
views
Template class implementation has compilation issues
I am creating an object using static member function of a class. and then calling a function inside.
int main(){
int a = 49;
auto foo = Foo::createFoo(a);
foo->study();
}
Implementation
for ...
0
votes
0
answers
35
views
Trying assign a value to base class atribute (from the derived class method) causes Segmentation Fault
I have this base class:
class Netpbm {
protected:
string magicNumber;
int width;
int height;
public:
Netpbm();
~Netpbm();
virtual void dump_data() = 0;
virtual void read_file(string ...
-1
votes
1
answer
24
views
access struct of base class with a template varaible
I want to create a derived class of base class that has a template and to keep the template option
but I cannot access a struct that is part of the base class
here is an example:
template <typename ...
0
votes
1
answer
39
views
How select derived props in Aggregate function with 2 class
Plz help with System.Linq.Aggregate
I have the following class
public class MainClass
{
public ClassA someProp { get; set; }
public ClassA someProp2 { get; set; }
}
public class ClassA
{
...
1
vote
3
answers
67
views
Better way to get property from derived class
i have next class
public class MainClass
{
public ClassA someProp { get; set; }
}
public class ClassA
{
public virtual Type Types => Type.None;
}
public class ClassB:ClassA
{
public ...
-1
votes
1
answer
45
views
reaseat shared_ptr<Derived> from function accepting shared_ptr<Derived>
I'm trying to reseat a shared_ptr of a derived class from a function that accept a shared_ptr of a base class.
This answer is relevant, but it does not cover the fact that I need to reseat the pointer ...
0
votes
1
answer
50
views
C# record calls ToString in the base record instead of the one in the derived record
I have the following two records:
internal record Token
{
private string content;
public string Content
{
get => content;
init => content = value ?? throw new ...
0
votes
0
answers
42
views
VS2010 C++ - problem casing based class pointer to derived class pointer
Using VS2010 and C++, I am using a supplier library to interface to their USB industrial camera.
The library has an base abstract class for data stream sinks called GrabberSinkType, and one of derived ...
1
vote
0
answers
49
views
Can derived class member functions return different types than the base class?
I am running into a design issue with my code and I am not sure where to go. I am attempting to write a basic I/O class to write vectors of data into a text file for convenience in my research. In ...
0
votes
1
answer
44
views
Call operator<< for a base class from within operator<< for a derived class, when they are not class members in C++
I have a base b and a derived d classes.
Within operator<< for d (which is not a class member) I want to call operator<< for b, plus other actions.
I used static_cast for that.
Is there ...
0
votes
2
answers
58
views
Calling an overridden function from a base constructor
It makes sense that I should not call an overridden function from a base constructor, since the derived class is not yet constructed.
But I want to use this design pattern, where each derived class ...
0
votes
2
answers
108
views
Creating a std::vector of derived classes
Assume that I have an Abstract Class
class AbstractClass {
public:
virtual int get() const = 0;
};
and two different Derived classes
class DerivedClassA : public AbstractClass {
public:
int ...
-2
votes
1
answer
48
views
Python multiple inheritance confusion [duplicate]
I've been trying to understand how to make sure all super().__init__()s run when writing a class that inherits from two unrelated classes. Based on answers such as this and others like it, calling ...
0
votes
2
answers
67
views
Is it good practice to declare derivate classes in the same C++ header? [closed]
I'm declaring a pure virtual class that will provide a unified interface for a handful of derived classes. My instinctual way to organize this would be to create a base folder with the header for the ...
1
vote
1
answer
66
views
can derived class access base class non-static members without object of the base class
can derived class access base class non-static members without object of the base class
class base
{
public:
int data;
void f1()
{
}
};
class derived : base
{
public :
void ...
0
votes
1
answer
81
views
Not calling parent's class constructor in derived class in c++
I have a problem which I can't solve.
I'm creating an cd class. This cd have 10 char items saved on it. The constructor completes the table.
class CD {
protected:
char* data_ = new char[10];
...
0
votes
0
answers
118
views
C++ How to override virtual functions with struct arguments? [duplicate]
I have a virtual function in a base class, taking a parent struct as an argument:
class Parent
{
struct foo_t {...};
virtual void set_foo(foo_t &foo) = 0;
};
which I want to override in a ...
1
vote
0
answers
48
views
Avoid duplicating template arguments in declaration of template partial specialization of derived class
I basically wanted to do partial specialization of functions (on non-type template parameters). Because partial specialization of functions is not allowed in C++ for some reason, I decided to put the ...
0
votes
1
answer
40
views
Printing variables of different derived class objects inside a single vector
So I have this simple code with one base class and 2 derived classes. Each derived class has it's own variable and the base class has an id variable which should be shared with all the elements I ...
1
vote
2
answers
51
views
How would one succinctly compare the values of and call the functions of many derived classes' base class?
I have a 2d physics engine that I've been programming in C++ using SFML; I've implemented a rough collision detection system for all SandboxObjects (the base class for every type of physics object), ...
0
votes
3
answers
81
views
How to seperate definition and implementation of a derived class constructor?
I would like to learn how to define a derived class constructor in one file so that I could implement it in another file.
public:
Derived(std::string name) : Base(name);
~Derived();
Destructor works ...
0
votes
2
answers
32
views
can you help me with the copy c'tor for derived class?
I have this base class:
class LevelPlayer
{
protected:
int level;
int id;
public:
LevelPlayer():id(-1){}
LevelPlayer(int level,int id):level(level),id(id){}
virtual ~LevelPlayer()=...
0
votes
0
answers
47
views
Using a derived generic class as a property in a derived class C# - is this an antipattern?
I've been fighting windmills trying to wrap my head around this problem in C#. The short version is that I have two sets of classes in C#. The first set are objects (which inherit from a base object ...
0
votes
2
answers
88
views
Accessing vectors declared in header file C++ (not global)
I've submitted an assignment which has met all the criteria except that I'm not allowed to have global variables except if they're constants, and I had my vector set up as a global variable.
I'm now ...
0
votes
0
answers
65
views
how to differentiate two derived classes in c++
Let's consider two derived classes (f.e Sheep and Dog) of abstract class Animal. How to compare if two objects of derived classes are actually the same class?
void Organism::collision()
{
//...
0
votes
2
answers
80
views
Can I define a virtual function (or similar) to return of vector of derived class members?
I have a base class B with derived classes X, Y and Z (in fact, more than 20 derived classes). Each class has a tag() function that identifies which (derived) class it is. My program stores instances ...
0
votes
1
answer
41
views
C++ Accessing a reference a derived object in a vector of base object across classes
I am attempting to make a vector that stores a base class and then passes it to another class which then accesses the derived class from the vector of base classes I have found multiple Stack Overflow ...
0
votes
1
answer
49
views
LogRecord attributes in a derived logger
I want to use a logger which also knows to handle my additional log levels verbose and trace. Therefore I created a custom logger deriving from a base logger
import logging
class DefaultLogger(...
0
votes
1
answer
59
views
Performance comparison about template parameters of container?
I am developing an application that holds a vector of different types, like below
#include <vector>
#include <variant>
#include <any>
class Symbol{};
class Terminal : public Symbol{
...
1
vote
0
answers
103
views
Is there a way to override default template arguments in derived types?
When deriving from a base class with member function templates, is there a valid way to provide different default arguments?
class Base {
public:
// ...
template < typename T >
T ...
1
vote
1
answer
56
views
Reference promotion for a derived class with no data members
I have an interesting problem involving a hierarchy of classes in a library that I maintain. A very simplified view of the situation is as follows:
class Base {
// private data + public interface to ...
0
votes
1
answer
89
views
Adding derived class object to vector<unique_ptr> of base class
So in my code I'm trying to add unique_ptr to objects from derived class to vector of base class. I get this error:
E0304 no instance of overloaded function "std::vector<_Ty, _Alloc>::...
0
votes
0
answers
30
views
Is it possible to force a derived class to override the entire base class in Unity?
I currently have a Unity 2019 project that utilizes a series of external Unity plugins (or packages):
-project
-source
-sourceClassA
-plugins
-pluginA
-...
6
votes
1
answer
152
views
Why is this compiling successfully?
What is the reason which why this code compile :
#include <iostream>
using namespace std;
class being {
public:
void running(char c) {
cout << "No one know ";
}
};
...
1
vote
2
answers
57
views
Method of base class getting called
I have the following code:
#include<iostream>
using namespace std;
struct Base{
void f(int x){
cout<<"B";
}
};
struct Derived: public Base {
virtual void f(...
-2
votes
1
answer
142
views
Please explain the outcome of Shape r = new Square() and why Square's methods aren't available
I'm working through Jeff Fritz's c# tutorial videos, and there is some code like this that uses an abstract class:
public abstract class Shape {}
public class Rectangle : Shape {}
public class Square :...
1
vote
1
answer
55
views
C++: How can I forward-declare derived classes that appear in a static method of a base class?
Just doing a simple exercise where I'm translating ideas I learned from another language to C++.
I have an abstract class Number, which has two derived classes, PositiveNumber and NegativeNumber. ...
-1
votes
1
answer
96
views
C++: How to iterate over a list of class types for typeid verification and downcasting?
I would like to perform a down casting at execution time.
For what I read, if I want to do it, I need to compare the typeid of my polymorphic pointer with those of my derived classes, then do the ...
-2
votes
1
answer
55
views
Why cant i use base constructor in c# [duplicate]
let's say we have two classes
class baseClass
{
public baseClass()
{
}
public baseClass(int value)
{
}
}
class derivedClass
{
}
is there a way to call the parametrized ...
0
votes
1
answer
37
views
Change the field of a derived class object but the change was recovered after returning
I override the create function.
void IBlock::create() {
Cell a;
a.setCoords(0, 3);
a.setblank(false);
Cell b;
b.setCoords(1, 3);
b.setblank(false);
Cell c;
c.setCoords(...
-2
votes
2
answers
196
views
assign different derived classes to base class based on condition [duplicate]
I have a base class and two derived classes:
public class base1
{
public int property1;
}
public class child1 : base1
{
public int property2;
}
public class child2 : base1
{
public int ...