polymorphism

How to find out what type of object a pointer points to in C++?

穿精又带淫゛_ 提交于 2019-12-23 17:59:03
问题 Let's say I have class SuperClass { public: int a; } and class SubClass : SuperClass { public: int b; } and I took a pointer to an instance of the SubClass SubClass *subPointer and addressed that pointer to a SuperClass pointer SuperClass *superPointer = subPointer . Now of course I can always cast the superPointer object to a pointer of SubClass because the only thing it stores is an adress. But how would I know if the object superPointer is pointing to an instance of SubClass or is just a

How is dynamic polymorphism useful when I cant call derived class methods with base class reference

半城伤御伤魂 提交于 2019-12-23 17:22:54
问题 public class A { protected int x = 10; A() { System.out.println("Constructor A" ) ; } public void test() { System.out.println(" A " ); } public void Aex() { System.out.println(" Aex " ) ; } public void testg() { System.out.println("Hello"); } } public class B extends A { B() { System.out.println("Constructor B" ) ; } B(int num){ this.x = num ; //System.out.println(this.x); } public void test() { System.out.println(" B " ); } public int getx() { return this.x; } } public class C { public

Can CUDA kernels be virtual functions?

会有一股神秘感。 提交于 2019-12-23 16:08:19
问题 The question is quite straighforward, but let me give an overview of my framework. I have an abstract class AbstractScheme representing a type of computation (a kind of discretization for an equation, but this is not important). Each implementation has to provide a method to return the name of the scheme and has to implement a protected function which is the CUDA kernel. The base abstract class provides a public method which calls the CUDA kernel and returns how long it took for the kernel to

C# Generics Inheritance

岁酱吖の 提交于 2019-12-23 13:05:25
问题 I have the following class public class AccountingBase<TItemType> where TItemType : AccountingItemBase And in my AccountingItemBase i have the following property: public virtual AccountingBase<AccountingItemBase> Parent { get; set; } in my AccountingBase, I am trying to do the following item.Parent = this; Logically this should work, as TItemType inherits from AccountingItemBase, but instead i get the following error: > Error 1 Cannot implicitly convert type > 'TGS.MySQL.DataBaseObjects

Extending using C# generics?

≯℡__Kan透↙ 提交于 2019-12-23 12:27:51
问题 I want to create an extendable nested structure and it seems like I should be able to do this using generics, though I may not be using them "properly". I want to be able to create child classes from GroupType and/or OptionType. The problem is that I can't perform the new operation on the generic types even though I specified they could only be of a certain base type. Is there any way to do what I'm trying to do? public class AllInfo<GroupType, OptionType> where GroupType: GroupBase

Operator== in derived class never gets called

强颜欢笑 提交于 2019-12-23 12:23:50
问题 Can someone please put me out of my misery with this? I'm trying to figure out why a derived operator== never gets called in a loop. To simplify the example, here's my Base and Derived class: class Base { // ... snipped bool operator==( const Base& other ) const { return name_ == other.name_; } }; class Derived : public Base { // ... snipped bool operator==( const Derived& other ) const { return ( static_cast<const Base&>( *this ) == static_cast<const Base&>( other ) ? age_ == other.age_ :

Operator== in derived class never gets called

风格不统一 提交于 2019-12-23 12:22:14
问题 Can someone please put me out of my misery with this? I'm trying to figure out why a derived operator== never gets called in a loop. To simplify the example, here's my Base and Derived class: class Base { // ... snipped bool operator==( const Base& other ) const { return name_ == other.name_; } }; class Derived : public Base { // ... snipped bool operator==( const Derived& other ) const { return ( static_cast<const Base&>( *this ) == static_cast<const Base&>( other ) ? age_ == other.age_ :

C++ polymorphic load/save

ぐ巨炮叔叔 提交于 2019-12-23 12:13:36
问题 I'm saving and reloading a bunch of different objects all derived from a common base to a file, and obviously I need to store the class name (or something similar) in order to create the correct object type on reloading. Saving is easy: class Base { virtual string className() const = 0; void saveToFile() { write(className()); ... other writing stuff } } class Derived1: public Base { string className() const { return "Derived1"; }; ... } class Derived2: public Base { string className() const {

Why can't `&(?Sized + Trait)` be casted to `&dyn Trait`?

半腔热情 提交于 2019-12-23 11:34:13
问题 In the code below it is not possible to obtain a reference to a trait object from a reference to a dynamically-sized type implementing the same trait. Why is this the case? What exactly is the difference between &dyn Trait and &(?Sized + Trait) if I can use both to call trait methods? A type implementing FooTraitContainerTrait might e.g. have type Contained = dyn FooTrait or type Contained = T where T is a concrete type that implements FooTrait . In both cases it's trivial to obtain a &dyn

Why can't `&(?Sized + Trait)` be casted to `&dyn Trait`?

梦想的初衷 提交于 2019-12-23 11:33:23
问题 In the code below it is not possible to obtain a reference to a trait object from a reference to a dynamically-sized type implementing the same trait. Why is this the case? What exactly is the difference between &dyn Trait and &(?Sized + Trait) if I can use both to call trait methods? A type implementing FooTraitContainerTrait might e.g. have type Contained = dyn FooTrait or type Contained = T where T is a concrete type that implements FooTrait . In both cases it's trivial to obtain a &dyn