A
Andrey Vul
Given
// Interface Q
// Implementations R, S
// Caller P
#include <iostream>
#include <utility>
class Q { public: virtual void s() = 0; };
class R : public Q { public: virtual void s()
{ std::cout<<"x"<<std::endl; } };
class S : public Q { public : virtual void s()
{ std::cout<<"y"<<std::endl; } };
class P { public: Q& q; P(Q& q) : q(q) {}; void R() { q.s();} };
int main() {
R r;
S s;
std:air<int, Q> t = std::make_pair(5, r); // error
std:air<int, Q*> u = std::make_pair(5, &r); // works
P u(t.second), w(*(u.second));
u.R(); v.R();
}
, an std:air<,> containing an interface causes an error because
std:air requires concrete types. Is there a workaround (apart from
using pointer-to-interface everywhere, which feels like using the C
sledgehammer) so that ABCs can be used in std:air<,> ? My code uses
interface/ABC methods quite a lot and I'm organizing the data
structures as part of cleaning up the code. Are other STL containers
also unable to work directly with ABCs?
// Interface Q
// Implementations R, S
// Caller P
#include <iostream>
#include <utility>
class Q { public: virtual void s() = 0; };
class R : public Q { public: virtual void s()
{ std::cout<<"x"<<std::endl; } };
class S : public Q { public : virtual void s()
{ std::cout<<"y"<<std::endl; } };
class P { public: Q& q; P(Q& q) : q(q) {}; void R() { q.s();} };
int main() {
R r;
S s;
std:air<int, Q> t = std::make_pair(5, r); // error
std:air<int, Q*> u = std::make_pair(5, &r); // works
P u(t.second), w(*(u.second));
u.R(); v.R();
}
, an std:air<,> containing an interface causes an error because
std:air requires concrete types. Is there a workaround (apart from
using pointer-to-interface everywhere, which feels like using the C
sledgehammer) so that ABCs can be used in std:air<,> ? My code uses
interface/ABC methods quite a lot and I'm organizing the data
structures as part of cleaning up the code. Are other STL containers
also unable to work directly with ABCs?