52 questions
0
votes
1
answer
46
views
What's the right way to determine which kind of cl_program I have?
The OpenCL API has one object which is sort of a "kitchen sink" for a lot of stuff: The program (with handle type cl_program). It can hold:
A textual program source ( ...
-1
votes
1
answer
65
views
WTF there are many definitions for polymorphism
I’m really confused about what polymorphism in OOP actually means. Everyone explains it differently, and I’m not sure which definition is correct:
In some places, polymorphism is explained as dynamic ...
2
votes
2
answers
90
views
Does the static type of an object coincide with the dynamic type, if the compiler can infer the dynamic type at compile type?
[defns.dynamic.type] reads
⟨glvalue⟩ type of the most derived object to which the glvalue refers
from which I understand that given
struct B { virtual ~B() = default; };
struct D : B {};
std::...
0
votes
1
answer
150
views
Runtime Polymorphism and Virtual function
Can we achieve runtime polymorphism(method overriding) without virtual function in C++
I think that since it is runtime it should only be possible through pointers using virtual functions but we can ...
0
votes
1
answer
122
views
How virtual keyword leads to dynamic binding?
I know the following
Compiler constructs a virtual table for every class containing at least one virtual function. It also adds a pointer(v_ptr) to the base class(supposing it has virtual function(s)) ...
0
votes
1
answer
51
views
Polymorphism with generics? [closed]
I have a class A -> class B (child of A) -> class C (child of B) and class A -> class D.
How can I write a function that return objects from class B and class D, example below.
How to ...
1
vote
1
answer
137
views
How do I call the function of the base class to show runtime polymorphism?
There is a class Base which has a function add that inputs integers from the users and prints their sum. There's another class called Derived which publicly inherits the Base class and it also has a ...
-1
votes
2
answers
110
views
How to bypass upcasting and achieve polymorphism without virtual and override in C#
I have three classes called Animal, Cat and Dog where Cat and Dog inherit from Animal:
public class Animal
{
public void Talk()
{
Console.WriteLine("Parent");
}
}
public ...
0
votes
3
answers
153
views
Virtual function in Inheritance in C++
**Read the below mention code **
#include<iostream>
using namespace std;
class FR
{
public :
virtual int fun()
{
cout<<"In FR Class "<<endl;
}
int ...
-1
votes
1
answer
180
views
Is modifying a function in derived class and accessing it by derived class object itself an example of runtime polymorphism
I was reading about function overriding and run-time polymorphism online.
While reading I found an example of function overriding on programiz where following example was given :
// C++ program to ...
0
votes
1
answer
64
views
Polymorphism within a class method
Considering a struct point
struct point{
int x;
int y;
int z;
}
and a class object
class object{
public:
std::vector<point> points;
int dimension_count;
...
0
votes
1
answer
394
views
Performance differences in run-time polymorphism between C and C++
I know benchmarking is a very delicate subject and simple, not-well-thought-out benchmarks are mostly meaningless for performance comparisons, but what I have right now is actually a pretty small and ...
0
votes
2
answers
869
views
Why am I not seeing runtime-polymorphic behavior using data members
I was studying OOP from here.
It says that runtime polymorphism is possible in C++ using data members.
Now,consider this code:-
#include <iostream>
#include <string>
class Animal { ...
1
vote
1
answer
155
views
Can late-binding in C++ be done without the need for heap-memory when using composition?
late-binding or runtime-polymorphism needs 2 things: a base pointer and a virtual method.
class FrameWorkClassBase {
public:
virtual void method() = 0;
};
class FrameWorkClassDerived: public ...
0
votes
1
answer
346
views
Can't a base class pointer which is pointing to derived class access the public data members of base class?
I have created a base class Polygon and declared two data members - height and width as public. Also a virtual function - calculateArea(). Then the two derived classes rectangle and triangle have a ...
0
votes
0
answers
161
views
Runtime polymorphism and operator overload
The problem is that in main function pointer to abstract class list calls operator+ of this class. But how can I call overloaded operators from child classes (queue and stack) with pointer to parent ...
5
votes
3
answers
793
views
C++: Is there a more elegant solution to this (multiple dispatch) runtime polymorphism?
The main problem is simple, really. Given a base (more abstract) class and multiple derived ones that need to interact with each other, how do you go about doing it?
To give a more concrete example, ...
-1
votes
1
answer
241
views
Compile time errors while implement Virtual Functions and Run-time polymorphism in C++
I created the following program to implement Run-time Polymorphism in C++
/* Consider a book shop which sells both books and video-tapes. Create a class know as media that storea the title
and price ...
0
votes
1
answer
1k
views
What is the overhead of dynamic_cast<>
So I have been programming in C++ for a while now, and I was told that using a dynamic cast to cast a pointer of an abstract class pointer to a different concrete class pointer was bad practice.
Shape*...
1
vote
0
answers
40
views
Why below code gives error even if the public function in derived class is the one being called..?
In the following code a public function has overriden a private virtual function in base class
ob->hello() shall call hello() in derived class which is public.
why do i still see an error that ...
4
votes
1
answer
3k
views
Virtual function calling a non-virtual function and vice versa
I have a class A as a base class of class B.
I have called the non-virtual function, abc(), within my virtual function, xyz(), as mentioned below.
Due to run-time polymorphism, B:xyz is called – I ...
0
votes
0
answers
73
views
How to upcast an Rc<dyn Foo> [duplicate]
Is it possible to upcast an Rc<dyn Foo>? If not, why not?
use std::rc::Rc;
trait Bar {}
trait Foo: Bar {}
fn upcast(this: Rc<dyn Foo>) -> Rc<dyn Bar> {
this
}
results in
...
11
votes
2
answers
5k
views
Using enums for dynamic polymorphism in Rust
When one already knows all the finite number of types involved in some code which needs dynamic polymorphism, using enum can be better for performances compared to using Box since the latter uses ...
1
vote
2
answers
186
views
Why does the base class pointer point to the pure virtual method in the base class instead of the overidden method in the derived class?
#include <iostream>
class A
{
public:
virtual ~A() = default;
virtual void foo(void) = 0;
};
class B : public A
{
private:
int x;
public:
B(int a) :...
-2
votes
1
answer
116
views
C++ template programming design question - return different type according to input file
A multimedia file may have data of different date types such as uint8_t, int16_t, float, etc. Below three examples show file content with the first byte indicate data type:
1st File: 0,<uint8 data&...
0
votes
1
answer
108
views
Runtime Polymorphism - Arrow operator accessing the wrong member?
I have the following classes in my program:
class base {
public:
int bval;
base() {
bval = 1;
}
};
class deri: public base {
int dval;
public:
deri() {
dval = 2;
...
0
votes
1
answer
36
views
Suppressed the virtual mechanism by storing object explicitly as base class? [duplicate]
As you'd expect, I've arrived at a question I can't answer and I can only guess.
Runtime polymorphism is the goal, using the virtual mechanism, but the result I'm getting is as if I called the method ...
0
votes
3
answers
392
views
Dynamic method dispatch and runtime polymorphism error
I am trying to understand dynamic method dispatch.
class A {
int i=10;
void callme() {
System.out.println("Inside A's callme method");
} }
class B extends A {
int j=20;
void callme() {
...
0
votes
1
answer
134
views
Runtime type selection based on map of types
Background
I am writing an application which utilizes USB devices. This includes device discovery of the USB devices my application can use, based on USB vendor id and product id. However these ...
1
vote
0
answers
133
views
how to typecast Parent to Child with IList in C# [duplicate]
Below is my DTO in C#.Net
public class Movie
{
public int Id { get; set;}
public IEnumerable<Blog> Blogs {get; set;}
}
public class Blog
{
public int Id { get; set;}
}
public class BlogCat1 ...
-1
votes
1
answer
109
views
how to call parent class mothod
I am studying run time polymorphism, I find one example like this
class Bike {
void run() {
System.out.println("running");
}
}
class Splender extends Bike {
void run(){
...
1
vote
2
answers
802
views
Elaboration: Method overloading is a static/compile-time binding but not polymorphism. Is it correct to correlate static binding with polymorphism?
Before I ask my question, let me to explain my understanding and opinion.
By only Overriding we can't achieve polymorphism unless there is upcasting. And as it can only seen at runtime people might ...
2
votes
3
answers
487
views
How does upcasting actually works in java
I have 3 classes where one is the super class and other two are sub classes. They have print() method common in them. While calling print() methods from main() I have upcasted the objects to super ...
3
votes
1
answer
2k
views
A Hashmap supporting String and &str
How do I define a HashMap supporting both String and &str in its key and content? I tried the following:
fn mapping<T: Into<String>>() -> HashMap<T, T> {
let mut map: ...
5
votes
1
answer
708
views
Are virtual tables part of the C++ standard?
I am reading about the implementation of late binding polymorphism in C++ and I learned about virtual tables and virtual pointers.
Then I searched on the C++ standard (ISO/IEC 14882:2011) to get ...
0
votes
2
answers
677
views
Number of vptr created if derived class also has a virtual function which is not present in Base class
class Base
{
public:
virtual void func1()
{
std::cout<<"Base func1"<<std::endl;
}
//virtual destructor
};
class Derived : public Base
{
...
0
votes
1
answer
138
views
Runtime Polymorphism in Java to access overridden method that has been overridden again
Due to Dynamic Method Dispatch, on creating an object using Superclass reference and calling a method through this object that has been overridden in the subclass, the overridden method in subclass ...
1
vote
2
answers
267
views
runtime polymorphic invocation of pure virtual function via std::reference_wrapper behaving inconsistently
I present to you this code riddle:
Using this compiler:
user@bruh:~/test$ g++ --version
g++ (Ubuntu 7.3.0-16ubuntu3) 7.3.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free ...
0
votes
2
answers
156
views
Runtime polymorphism c++11 and operator overloading
Let's say I am trying to implement some math vector class.
As vector interface will be used in multiple places: array based vector, matrices return columns and rows as vector interface objects and ...
-1
votes
2
answers
40
views
How do this casting work? About Polymorphism-CastingJava
Please don't be stuck in spelling mistakes. I didn't understand why Prof' saluer function is working with Prof arguments in the screen output.
Code output:
Mes hommages pour ma / mon collègue ...
0
votes
1
answer
87
views
Inheriting specifid methods in java
Is it possible in java to inherit some methods from the base class, but not all of them? Just to be clear, i will show you what I mean:
Suppose we have the base class Visitor
public abstract class ...
0
votes
2
answers
325
views
How to know whether it is a compile-time polymorphism or run-time polymorphism?
Consider a superclass:
class superclass
{
public void fun() {.....}
}
and it's subclass:
class subclass extends superclass
{
public void fun(){.......}
public static void main()
{
...
8
votes
5
answers
2k
views
Is method overriding always run time polymorphism?
Does runtime polymorphism always happen with method overriding, or does it happen only if method is called after assigning sub class object to super class variable, during method overriding?
class A {
...
0
votes
1
answer
170
views
Is downcasting run-time polymorphic?
Upcasting in OOPS makes runtime polymorphism possible by overriding methods of parent and child classes. Can downcasting in OOPS also be run-time polymorphic in any way?
0
votes
1
answer
33
views
Trouble with different kinds of polymorphism
From what I learned so far, there are two kinds of polymorphism, compile-time, and runtime.
In compile-time, the polymorphed function or operator is resolved by compiler, while in runtime, its ...
1
vote
0
answers
663
views
C++ - Static polymorphism vs Runtime polymorphism vs Lambda
I learned the difference between static and run-time polymorphism and since then I've been reading a lot of stuff on it.
Plus, since I found out that implementing a static polymorphic interface (in a ...
-3
votes
2
answers
104
views
List versus ArrayList [duplicate]
Which one is better and why ?
a) List<String> list = new ArrayList<>();
b) ArrayList<String> list = new ArrayList<>();
1
vote
1
answer
142
views
How do polymorphism and bytecode relate in python? [duplicate]
How does polymorphism work, under the hood, in python?
In python, if I have some function e.g.
def f(x):
return x + 2*x + 3*x + 4*x + 5*x + 6*x
then according to dis.dis(f) python translates ...
0
votes
4
answers
983
views
why cant we use a reference variable of super class to access methods of its subclass(methods not available in super class)?
I know that,No matter what the actual object is,that the reference variable refers to,The methods i can call on a reference is dependent on the declared type of the variable (in line 15 of code).I ...
0
votes
2
answers
6k
views
Why can't I assign a parent class to a variable of subclass type?
Why reference variable of child class can't point to object of parent? i.e
Child obj = new Parent();
However we can do vice versa
Kindly answer with memory view (heap)