464 questions
- Bountied 0
- Unanswered
- Frequent
- Score
- Trending
- Week
- Month
- Unanswered (my tags)
2
votes
3
answers
322
views
Is there C++ syntax for "at least one of these" pure virtual methods?
C++ has a lovely feature where the compiler will prevent you from forgetting to implement a pure virtual method in a subclass. But what if I want to require a user of my abstract base class to ...
15
votes
1
answer
385
views
Are pointers to pure virtual member functions of local classes allowed?
Consider the following snippet:
int main() {
struct Local {
virtual void foo() = 0;
};
void (Local::* ptr)() = &Local::foo;
}
When compiling with C++20, GCC 13.3.0 and Clang 18.1.3 ...
0
votes
1
answer
150
views
Why can't my destructor invoke an impure virtual function?
(By an "impure virtual function", I mean pure virtual functions that also have implementations (as described at http://www.gotw.ca/gotw/031.htm).)
Consider the following code:
#include <...
0
votes
1
answer
118
views
c++ Using implementation of parent class
Regarding the following code, I would avoid copy paste so much code, or implement the interface class IHardware:
#include <iostream>
#include <string>
class IHardware
{
public:
virtual ~...
0
votes
0
answers
43
views
C++ interface with only one implementation without performance overhead (for mocking in unit tests)
I am making a game engine in C++ and want to start implementing unit tests.
As far as I'm aware, you can only create mocks of pure virtual functions.
However, because I do not want to add needless ...
-1
votes
1
answer
71
views
Is there a way for a base class to require derived classes to have a comparison function to an object of the derived same class?
I have a homework assignment, and a small part of that assignment asks for a abstract parent "Movie" class with three concrete subclasses: "Comedy," "Drama," and "Classical."
Each subclass will never ...
-1
votes
2
answers
207
views
C++ problem accessing override pure virtual function after destructor and setting new value
#include <iostream>
using namespace std;
class Abstract {
protected:
int* arr;
int size;
public:
Abstract(int s = 10)
{
size = s;
arr = new int[size];
}
...
1
vote
2
answers
118
views
Is it possible to overload the implementation of a pure virtual function in the child class?
I am working on a project in which I have a base class called MSTAlgorithm (Minimum Spanning Tree Algorithm). Two classes called PrimsAlgorithm and KruskalsAlgorithm publically derive from this base ...
-2
votes
1
answer
167
views
Require derived classes to provide a static const value
I have a base class and derived classes. The base class uses pure virtual methods to require the derived classes to implement methods - otherwise it doesn't compile. Classic.
Can I also require from ...
0
votes
1
answer
110
views
How to implement an interface and inherit from another specialized template at the same time
Let's say we have following interfaces
// interface
template <typename T>
class MyInterface
{
public:
virtual void fun1() = 0;
virtual void fun2() = 0;
}
// just specialization, keep ...
-1
votes
1
answer
262
views
What is the correct way to write pure virtual function without a specific return type?
I'm trying to write an abstract struct named Shape, and I need a function in derived classes that will be counting distance from a ray origin to my shape. I use functions from here, they return float, ...
-2
votes
2
answers
376
views
Why does a pure virtual member function have to be virtual?
I have a question regarding the declaration of pure virtual functions in C++. I come from a Java background, and so I see pure virtual functions as a way to define abstract classes and the idea of ...
1
vote
2
answers
282
views
Bind pure virtual method
Suppose we have the following hierarchy:
class Add3Interface {
virtual int add3 (const int&) const = 0;
};
class Add3: public Add3Interface {
virtual int add3 (const int& arg) const ...
4
votes
1
answer
107
views
C++ Interface with convenience methods
Suppose I have the following interface:
struct Person {
std::string name;
unsigned short age;
};
class ContainerInterface {
public:
virtual ~ContainerInterface () = default;
...
1
vote
1
answer
209
views
Unexpected compile error when dealing with vector of unique_ptr<pure_virtual_class> inside std::map
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
class Base {
public:
virtual ~Base() = default;
virtual void doIt() = 0;
public:
int base = 1;
...