1,556 questions
3
votes
0
answers
88
views
C++'s virtual methods implementation in C [duplicate]
In C++, I can create an array of pointers to a base class and call overridden methods, my goal is to mimic this type of behavior:
Animal* zoo[3];
zoo[0] = new Cat("Whiskers");
zoo[1] = new ...
2
votes
4
answers
215
views
Inheritance - intended polymorphism isn't working
I have a simple c++ base class and two derived classes:
class base;
class der1;
class der2;
class base{
public:
virtual void act(base*); // ignore for now
virtual void print(){
cout &...
3
votes
2
answers
335
views
Why is `std::visit` so inefficient?
I found that use std::variant+std::visit is inefficient (performance almost like virtual function), but when I replace std::visit by x.index(), the performance is as better as CRTP.
My code:
#include ...
14
votes
2
answers
802
views
MSVC calls wrong virtual method when storing a pointer to virtual method in a static inline variable
I'm encountering unexpected behavior on MSVC when storing a pointer to a virtual method in a static inline variable. The issue does not occur on GCC or Clang.
Specifically, when I store a pointer to a ...
7
votes
2
answers
186
views
C++ class with multiple inheritance and covariant return types
I am trying to understand a compile error from the latest version of
VC++. The error occurs in a class that inherits from two base classes and
includes a virtual function that overrides functions in ...
3
votes
1
answer
154
views
Implicitly deleted function overrides deleted virtual function
This is more of a theoretical question about the interpretation of the standard in one particular case, given below:
struct B;
struct A {
virtual bool operator ==(const B&) const = delete;
};
...
33
votes
2
answers
2k
views
What's the point of deleted virtual functions?
Apparently you can =delete virtual functions:
struct A
{
virtual void foo() = delete;
};
struct B : A
{
void foo() override = delete;
};
Interestingly, both functions have to be =deleted, or ...
2
votes
2
answers
227
views
How can I omit unused virtual functions when linking?
I use C++ with arm-none-eabi-gcc compiler to write software for a MCU.
For this I use a wrapper which can use different implementations. The issue is when one of the functions of the wrapper is used, ...
0
votes
1
answer
1k
views
Call to virtual method during construction bypasses virtual dispatch C++
I have the following code:
class IInterface {
public:
virtual ~IInterface() {}
virtual void doSomething() = 0;
};
class Concrete : public IInterface {
public:
Concrete() : data{0} {...
0
votes
0
answers
91
views
Magic way to make :: base (e.g. Animal) class know class of its REAL self derive instance (Dog) upon "new Dog()"
I have to do a prematurely optimization improve performance of virtual function calling.
Animal::f1(int) is a virtual function. It is implemented by many classes e.g. Dog,Cat and Murderous_Hippo.
In ...
0
votes
2
answers
190
views
When is virtual dispatch faster than function templates in C++ runtime?
Most knows that template meta programming is in general faster than virtual dispatch in C++ due to types of templates were decided in compile time while virtual functions required runtime lookup on ...
0
votes
0
answers
103
views
Static C++ check for pure virtual functions passed in constructor to std::bind?
C++ compilers warn if pure virtual functions are directly called in a constructor/destructor (but not indirectly).
Binding a pure virtual function and storing the result in a class member and calling ...
1
vote
0
answers
106
views
How to measure difference in performance between virtual function+inheritance and std::function member w/o inheritance?
tl;dr
Is the benchmark I present below a fair way to compare inheritance-based vs std::function-based approach to polymorphism?
Full question
If one needs different objects that implement the same ...
2
votes
1
answer
87
views
Overriding derived virtual method in a templated class
I have the code below where I want foo to be implemented by the derived class but used within baseclass by bar methods. But I need foo to handle two cases where one of the inputs and the output type ...
5
votes
0
answers
122
views
Why assembly differ when derived class method calls base class implementation of pure virtual method versus implementing it directly in derived class?
Code 1:
#include <iostream>
struct Interface
{
virtual void pr_fn() = 0;
virtual void pr_fn2() = 0;
virtual void pr_fn3() = 0;
};
struct Base : Interface
{
void pr_fn2() final
...
-1
votes
2
answers
115
views
What is the difference between (this->*&A::main)(); and this->A::main(); in C++ when dealing with virtual functions? [duplicate]
I'm trying to understand the difference between two ways of calling a function in C++, especially when dealing with virtual functions. Here is a simple code example:
#include <iostream>
class A ...
0
votes
3
answers
143
views
How to avoid calling a virtual function in destructor when the base class need to know info about the derived when destructing?
Currently I met with the following situation:
I have a base class Base with an void* V member, it may be A*, B*, C* actually, and three (only three, fixed number) categories of class will derive from ...
0
votes
0
answers
390
views
No viable conversion from 'unique_ptr<Derived>' to 'unique_ptr<Base>'
I have a simple example of using virtual functions. Here is my structure:
class Derived : public Base {
public:
Derived(int x, int y): Base(x, y) {}
int func(int n) {
return ...
0
votes
0
answers
80
views
Virtual function with template [duplicate]
I have following piece of code:
class Foo
{
private:
FooProperties;
};
class Bar
{
private:
BarProperties m_prop;
};
class Baz
{
private:
BazProperties
};
class Properties Handler
{
};
...
0
votes
0
answers
58
views
Virtual Template Best Practice when Realization has small cardinality
I have the virtual template problem, i.e.:
class Base{
public:
// template<bool flag> virtual void eval()=0; // line A
};
class Derived: public Base{
public:
template<bool flag> ...
2
votes
2
answers
62
views
LSP and virtual methods
I came across this example when I was trying to learn more about LSP, I am new to the OOPs world. You can find my question after the example. The example is as follows.
In mathematics, a Square is a ...
0
votes
1
answer
73
views
Accessing Private Members of Derived Class through Base Class Pointer with Virtual Function in C++
I have a scenario in C++ where I'm accessing private members of a derived class through a base class pointer, and it seems to work without any errors. Here's a simplified version of the code:
#include ...
4
votes
1
answer
167
views
Why is 'Placement New' for classes with virtual Members only working when buffer is on local stack?
I have some C++-Code which uses the Placement New operator to create an instance of a class with virtual members in an existing buffer. It works as expected when the buffer is on the local stack of ...
0
votes
0
answers
28
views
Unexpected vTable look-up for non-virtual method in C++ [duplicate]
The code snippet below explains the problem:
class base_obj_with_virtual_func
{
public:
int variable_int;
virtual void print_name() { std::cout << "Base obj " << this <...
0
votes
1
answer
87
views
C++, Using derived classes virtual functions with derived class type pointer as argument
I am trying to understand how derived classes with pointers and virtual functions work. What I want to do is use a virtual function from the base class such that it takes pointer of the class type as ...
-3
votes
2
answers
108
views
C++ virtual function not overriding [closed]
#include <iostream>
using namespace std;
class character {
private:
public:
character(string n, int h, int a){
}
virtual int getHealth() = 0;
virtual int getAttack() = 0;
virtual ...
1
vote
2
answers
121
views
Why is the size of this subclass the same as the base class even though it adds a member variable?
In the following code, why is the size of Parent the same as the size of Child (even though Child adds a member variable)? Another weird behaviour: if I comment out the line where b is defined, the ...
3
votes
0
answers
136
views
Overloading the shift operator << in C++ [closed]
As a university assignment I have to implement the ostream class of C++ standard library. I have header files with the function declarations and .cpp files, where I have to implement the functions ...
2
votes
2
answers
108
views
Is it allowed to pass "this" of derived class to constructor of base class?
Here is the code I originally wanted to write:
class A {
public:
A(someType someData) {
this->init(someData);
}
virtual void init(someType) = 0;
}
This is not allowed, ...
2
votes
1
answer
274
views
Calling a C++ virtual method from Rust throws an Access violation error even after it executed successfully
I'm trying to call a virtual method of a C++ object from Rust. I'm getting output, but after the execution of this method, it throws an exception.
Unhandled exception at 0x00000001 in testvirtual.exe: ...
1
vote
1
answer
186
views
Why both codes add -visibility=hidden, the normal functions compile incorrectly, but the virtual functions compile correctly?
I reading this page.
I follow the answer step by step to test, alse add -fvisibility=hidden to make all symbols hidden, and then I extended the code that is in the answer.
//rectangle.h
#pragma once
...
-5
votes
2
answers
257
views
Why does C++ not have Extend feature to virtual functions? [duplicate]
C++ allows defining default behavior of member functions using the virtual keyword, but what if I want to extend a member function to perform additional functionality? For example, I have the ...
0
votes
1
answer
73
views
Base class method not called on object of derived type: polymorphism without pointer and reference [duplicate]
Consider a Base class and a derived class that inherits from it called Child.
Assume the Base class has a member function declared virtual, and that Child overrides that function.
I would like to ...
0
votes
1
answer
77
views
How to use virtual function in the base class?
Consider the following two structures:
struct Potential{
virtual double energy(int full_system, int particle_id)=0;
double energy(int full_system){
int n_particles=100;
double ...
1
vote
1
answer
69
views
Example of "implicit ODR-use of a non-pure virtual member function that happens to be deleted"
There is a sentence in the cppreference.com description of Deleted functions:
However, implicit ODR-use of a non-pure virtual member function that happens to be deleted is allowed.
Could you provide ...
3
votes
2
answers
471
views
Benchmarking C++ virtual functions
I think that every C++ programmer at some point heard the statement "virtual functions are slow". So I decided to benchmark virtual functions against regular member functions.
Unfortunately, ...
0
votes
1
answer
132
views
Base class pointer calling non virtual base function which is virtual in derived class
I am learning C++ virtual functions. I declared a function as non virtual in base class, and same function as virtual in derived class. If i create base class pointer with derived object and call the ...
0
votes
1
answer
185
views
How does C++ trailing return type help in virtual functions?
There is a statement in C++ Hight Performance book:
The trailing return is necessary in some contexts. For example, if we are writing a virtual function, or the function declaration is put in a ...
1
vote
2
answers
140
views
Is it ever safe to call a virtual method from a constructor?
I am aware that it is unsafe to call a virtual method from within the constructor of the class in which the method is declared virtual (or pure virtual).
However, I am wondering if the following is ...
0
votes
2
answers
348
views
How to calculate totalPrice in sequelize?
I have four tables: Users, Orders, OrderProducts (join table), and Products.
I want to calculate totalprice column in Orders table. OrderProducts has quantity and productId, Products has price column.
...
0
votes
0
answers
61
views
Trying to avoid code duplication - virtual with template solution
I have many objects which contain different properties.
I'm trying to create a properties' editor dialog where I can pass the properties as a template and then, based on the template, construct ...
1
vote
1
answer
93
views
Virtual method table of non-pointer object?
If you have an object of a class that has virtual functions, and this object is not a pointer, will the virtual method table be used?
E.g let's pretend that the class Student has a virtual function ...
0
votes
0
answers
933
views
Ghidra: Override type for C++ vTable for Derived Classes (with Base Class located at 0x0 of Class); c++ polymorphism
I am new to ghidra and reverse engineering, but i have watched this series text and read text. And encountered following problem with ghidra:
When you have a Base class with members and virtual ...
0
votes
0
answers
69
views
Overrides only for certain derived classes
Is there a way to make only certain derived classes of a class, call it Base, override a virtual method, call it foo(), of Base? The reason I need this is because all the derived classes of Base that ...
0
votes
2
answers
173
views
Const is being ignored in virtual function override [duplicate]
Title kind of says it all. I can't figure out why the compiler will allow this:
#include <iostream>
class Base
{
public:
Base() = default;
virtual ~Base() {}
virtual ...
1
vote
1
answer
93
views
explicit specialization of a class that inherits from a virtual class
I have a virtual class BASE, and an inherited class BOX_strwhich implements the virtual functions
class BASE {
public:
virtual ~BASE() {};
virtual std::vector<int> custom_int() const = ...
1
vote
0
answers
188
views
C++ virtual template function best workaround
I have a class ComInterface which has a overloaded function send. This function is overloaded for many different enum class types.
class ComInterface{
public:
virtual void send(MotorCommand cmd);
...
0
votes
2
answers
254
views
Is there any workaround for a virtual function template with a type constraint in this case?
If you have a concept and a class member function template like so:
template<typename T>
concept Vector2 = requires (T t) { t.x; t.y; };
struct Shape
{
bool contains(const Vector2 auto&)...
7
votes
3
answers
174
views
Why does invoking a virtual method in constructor and binding a virtual method then calling it later yield different results?
This is my code snippet:
class Base {
public:
Base() {
foo();
bind();
}
virtual void foo() {
std::cout << "base foo\n";
}
void bind() {
fn = std::bind(&...
6
votes
1
answer
209
views
What is the difference between virtual final and non-virtual?
What is the difference in between declaring a new (non override) member function virtual final versus just non-virtual?
I'm guessing a virtual final member function (that does not override anything) ...