Global class comparison with inheritance - c++

I am writing global class comparison functions with operator== for a large framework where classes tend to inherit several classes or have deep inheritance (class A inherits from B, B inherits from C, etc.). In order to make things manageable I figured I would have a comparison functions for base classes and then classes that inherit from a base would use that function in addition to checking their own members
Googling around, I found example code for comparing classes but no examples that involved inheritance. Below I made up a simple example for base class Foo which Bar inherits from:
#include <iostream>
class Foo
{
public:
int m_a;
Foo(int i) : m_a(i) {}
};
inline static bool operator==(const Foo& l, const Foo& r)
{
return l.m_a == r.m_a;
}
static void coutResult(const Foo& l, const Foo&r)
{
std::cout << "l.m_a == " << l.m_a << ", "
<< "r.m_a == " << r.m_a << ", "
<< (l == r ? "true" : "false") << std::endl;
}
class Bar :
public Foo
{
public:
int m_b;
Bar(int i, int j) : Foo(i), m_b(j) {}
};
inline static bool operator==(const Bar& l, const Bar& r)
{
return ((Foo)l) == ((Foo)r) &&
l.m_b == r.m_b;
}
static void coutResult(const Bar& l, const Bar& r)
{
std::cout << "l.m_a == " << l.m_a << ", "
<< "l.m_b == " << l.m_b << ", "
<< "r.m_a == " << r.m_a << ", "
<< "r.m_b == " << r.m_b << ", "
<< (l == r ? "true" : "false") << std::endl;
}
int main(int argc, char** argv) {
Foo a(1);
Foo b(1);
Foo c(2);
coutResult(a, b);
coutResult(a, c);
coutResult(a, c);
Bar d(1, 2);
Bar e(1, 2);
Bar f(1, 3);
Bar g(2, 2);
coutResult(d, e);
coutResult(d, f);
coutResult(d, g);
coutResult(e, f);
coutResult(f, g);
coutResult(f, g);
return 0;
}
It seems to work just fine but I was wondering if there was a "standard" way to go about this or a better solution. There are two problems I see with this solution:
Every time a developer adds a member to some class they will have to know to update the corresponding comparison function but I can't see how this could be avoided
No members can be made private and considering that the framework is large this is a problem. The only solution I know of is to make a getter for every private member

Your design has the potential to produce unexpected results.
If your main is:
int main(int argc, char** argv)
{
Foo a(1);
Bar d(1, 2);
coutResult(a, d);
return 0;
}
you'll end up comparing a Foo object with a Bar object and output will be:
l.m_a == 1, r.m_a == 1, true
If you are happy with that outcome, you can stick your current design. However, I think that is an inappropriate outcome.
My suggestions:
Make Foo a pure virtual class to avoid situations like that.
Make operator=() a pure virtual member function the Foo. Provide an implementation in Foo the derived class implementations can take advantage of.
Implement the function the derived classes. Use dynamic_cast to make sure that you are comparing a Bar with another Bar, not Bar with another sub-type of Foo.
Here's a program that demonstrates those ideas.
#include <iostream>
class Foo
{
public:
int m_a;
Foo(int i) : m_a(i) {}
virtual bool operator==(const Foo& r) const = 0;
};
bool Foo::operator==(const Foo& r) const
{
return (this->m_a == r.m_a);
}
static void coutResult(const Foo& l, const Foo&r)
{
std::cout << std::boolalpha << (l == r) << std::endl;
}
class Bar : public Foo
{
public:
int m_b;
Bar(int i, int j) : Foo(i), m_b(j) {}
virtual bool operator==(const Foo& r) const
{
Bar const* barPtr = dynamic_cast<Bar const*>(&r);
if ( barPtr == nullptr )
{
return false;
}
if ( !Foo::operator==(r) )
{
return false;
}
return (this->m_b == barPtr->m_b);
}
};
class Baz : public Foo
{
public:
double m_c;
Baz(int i, double c) : Foo(i), m_c(c) {}
virtual bool operator==(const Foo& r) const
{
Baz const* bazPtr = dynamic_cast<Baz const*>(&r);
if ( bazPtr == nullptr )
{
return false;
}
if ( !Foo::operator==(r) )
{
return false;
}
return (this->m_c == bazPtr->m_c);
}
};
int main(int argc, char** argv)
{
Bar bar1(1, 2);
Bar bar2(1, 2);
Bar bar3(2, 2);
Baz baz1(1, 10.8);
Baz baz2(1, 10.8);
coutResult(bar1, bar2);
coutResult(bar1, bar3);
coutResult(bar1, baz1);
coutResult(baz1, baz2);
return 0;
}
Output:
true
false
false
true

Related

Polymorphism does not work with pointers, operator<< overload, inheritance, C++

I have a problem with my code. I have two classes, A and B, and B inherits A. I also have operators << overloaded in both classes.
Everything works, I have no compiler errors, but it seems something is wrong. As far as I understand polymorphism, when I use pointers to base class while creating child class with new, calling a method should match the child class, not the base class.
For the code below,
#include <iostream>
using namespace std;
class A
{
protected:
int a;
public:
A(int aa) : a(aa) {};
virtual void show(ostream& o) const
{
o << "a = " << a << "\n";
}
};
ostream& operator << (ostream& os, const A &o)
{
o.show(os);
return os;
}
class B : public A
{
private:
int b;
public:
B(int bb, int aa) : A(aa), b(bb){}
int getb() const {return b;}
};
ostream & operator << ( ostream & os, const B & o)
{
os << static_cast <const A &>(o);
os << "\n";
os << "b = " << o.getb() << "\n";
return os;
}
int main()
{
A *o1 = new B(2,3);
cout << *o1;
cout << "---------------------\n";
B *o2 = new B(2,3);
cout << *o2;
return 0;
}
In main:
A *o1 = new B(2,3);
cout << *o1;
Shows a = 3, instead of showing a = 3 b = 2 (the call should match the child class, not the base class). The thing is, I need to implement the << and >> operators in every child class, but I think they do not behave as they should.
The output of the program:
Even the modified code with re-implmented show method shows wrong results, it does not show a at all this time:
#include <iostream>
using namespace std;
class A
{
protected:
int a;
public:
A(int aa) : a(aa) {};
virtual void show(ostream& o) const
{
o << "a = " << a << "\n";
}
};
ostream& operator << (ostream& os, const A &o)
{
o.show(os);
return os;
}
class B : public A
{
private:
int b;
public:
B(int bb, int aa) : A(aa), b(bb) {}
int getb() const
{
return b;
}
void show(ostream& o) const
{
o << "b = " << b << "\n";
}
};
ostream & operator << ( ostream & os, const B & o)
{
os << static_cast <const A &>(o);
o.show(os);
return os;
}
int main()
{
A *o1 = new B(2,3);
cout << *o1;
cout << "---------------------\n";
B *o2 = new B(2,3);
cout << *o2;
return 0;
}
enter image description here
you have to implement the virtual function show in derived class B:
class B: public A
{
public:
// some code here
virtual void show(ostream& o) const
{
o << "b = " << b << "\n";
}
};
when I use pointers to base class while creating child class with new,
calling a method should match the child class, not the base class
It does when you call a member function ("method" in some other languages), but operator<< is not a member function – it's an overloaded free function.
When choosing an overload, only the types known at compile-time are used.
Since o1 is an A*, *o1 is an A&, and the overload for A& is chosen.
You're doing this a bit "backwards"; you only need one operator<<, for the base class, which calls the virtual show, and then you override show in the derived classes.
Like this:
class A
{
// ...
virtual void show(ostream& o) const
{
o << "a = " << a << "\n";
}
};
ostream& operator << (ostream& os, const A &o)
{
o.show(os);
return os;
}
class B : public A
{
// ...
void show(ostream& o) const override
{
A::show(o); // Do the "A part".
o << "b = " << b << "\n";
}
};
Follow the same pattern for operator>>.

Virtual overloading of the comparison operator

Suppose we have the following snippet:
class A
{
public:
virtual bool operator< (const A &rhs) const;
};
class B: public A;
class C: public A;
I want the comparison to depend on the real types of both the left and right hand side, for example:
x < y == true if type(x) == B and type(y) == C
x < y == false if type(x) == C and type(y) == B
The situation could be more complex, with much more derived classes than two. Of course, operator< has to be a virtual function. Is there an elegant way to write this?
///
/// goal: provide partial ordering of objects derived from A on the basis
/// only of class type.
/// constraint: ordering specified by us
///
#include <vector>
#include <typeindex>
#include <algorithm>
#include <iostream>
class A
{
public:
virtual bool operator< (const A &rhs) const = 0;
static const std::vector<std::type_index>& ordering();
};
template<class T> struct impl_A : public A
{
bool operator< (const A &rhs) const override
{
auto& o = ordering();
auto first = std::begin(o);
auto last = std::end(o);
auto il = std::find(first, last, typeid(T));
auto ir = std::find(first, last, typeid(rhs));
return il < ir;
}
};
class B: public impl_A<B> {};
class C: public impl_A<C> {};
const std::vector<std::type_index>& A::ordering()
{
// specify fording of types explicitly
static const std::vector<std::type_index> _ordering { typeid(B), typeid(C) };
return _ordering;
}
void test(const A& l, const A& r)
{
if (l < r) {
std::cout << typeid(l).name() << " is less than " << typeid(r).name() << std::endl;
}
else {
std::cout << typeid(l).name() << " is not less than " << typeid(r).name() << std::endl;
}
}
int main()
{
test(B(), C());
test(B(), B());
test(C(), B());
test(C(), C());
}
example output (clang):
1B is less than 1C
1B is not less than 1B
1C is not less than 1B
1C is not less than 1C
Fine! But (I was not precise enough in my question), when x and y share the same type (for example B), the result of x < y is given by a specific function const operator< (B &rhs) const in class ̀B. It is not necessarily false`.
OK, so we are revising requirements. This is a normal dialogue between users (who rarely realise the level of detail required in specifications) and the developers (who do!)
So this time we will say that any two dissimilar derived classes will have a consistent partial ordering (i.e. they will never compare equal and one will always compare less than the other) but we'll let the standard library decide which one comes first.
However, when the two classes being compared are of the same type, we would like to actually compare their values to determine ordering (and equivalence).
It would go something like this:
#include <vector>
#include <typeinfo>
#include <algorithm>
#include <iostream>
#include <tuple>
#include <iomanip>
class A
{
public:
virtual bool operator< (const A &rhs) const = 0;
std::ostream& print(std::ostream& os) const {
handle_print(os);
return os;
}
private:
virtual void handle_print(std::ostream&) const = 0;
};
std::ostream& operator<<(std::ostream& os, const A& a) {
return a.print(os);
}
template<class T> struct impl_A : public A
{
bool operator< (const A &rhs) const override
{
auto& rhs_info = typeid(rhs);
auto& lhs_info = typeid(T);
if (rhs_info == lhs_info) {
// same type, so do comparison
return static_cast<const T&>(*this).ordering_tuple() < static_cast<const T&>(rhs).ordering_tuple();
}
else {
return lhs_info.before(rhs_info);
}
}
};
class B: public impl_A<B> {
public:
B(int v) : _value(v) {}
auto ordering_tuple() const {
return std::tie(_value);
}
private:
void handle_print(std::ostream& os) const override {
os << _value;
}
int _value;
};
class C: public impl_A<C> {
public:
C(std::string v) : _value(std::move(v)) {}
auto ordering_tuple() const {
return std::tie(_value);
}
private:
void handle_print(std::ostream& os) const override {
os << std::quoted(_value);
}
std::string _value;
};
// now we need to write some compare functions
void test(const A& l, const A& r)
{
if (l < r) {
std::cout << l << " is less than " << r << std::endl;
}
else {
std::cout << l << " is not less than " << r << std::endl;
}
}
int main()
{
test(B(1), C("hello"));
test(B(0), B(1));
test(B(1), B(0));
test(B(0), B(0));
test(C("hello"), B(1));
test(C("goodbye"), C("hello"));
test(C("goodbye"), C("goodbye"));
test(C("hello"), C("goodbye"));
}
example results:
1 is less than "hello"
0 is less than 1
1 is not less than 0
0 is not less than 0
"hello" is not less than 1
"goodbye" is less than "hello"
"goodbye" is not less than "goodbye"
"hello" is not less than "goodbye"
The only solution I see is to not have the operator< function as a virtual member function, but as a set of overloaded non-member functions: One "default" function which takes two references to A as arguments, and then one overload each for the special cases.

Call function using subtype overload

Consider the following program
class A {};
class B : public A {};
void fun(A v) { std::cout << "A" << std::endl; }
void fun(B v) { std::cout << "B" << std::endl; }
void call(A v) { fun(v); }
int main(int argc, char *argv[]) {
A a;
B b;
call(a);
call(b);
fun(a);
fun(b);
}
It will print
A
A
A
B
Is there a way for the program to notice that the variable is actually a B in the second case, and hence call the overloaded fun(B), so that the output would become the following instead?
A
B
A
B
Option 1
You could use a template for your call function.
#include <iostream>
class A {};
class B : public A {};
void fun(A v) { std::cout << "A" << std::endl; }
void fun(B v) { std::cout << "B" << std::endl; }
template <typename T>
void call(T v) { fun(v); }
int main(int argc, char *argv[]) {
A a;
B b;
call(a);
call(b);
fun(a);
fun(b);
}
This will only compile if there is an overload of fun that takes a parameter of type T, which in your case is A or B.
Working example
Option 2
Alternatively, you could make these free functions into virtual class methods and actually use polymorphism.
#include <iostream>
class A
{
public:
virtual void fun() { std::cout << "A" << std::endl; }
void call() { fun(); }
};
class B : public A
{
public:
virtual void fun() override { std::cout << "B" << std::endl; }
};
int main(int argc, char *argv[]) {
A a;
B b;
a.call();
b.call();
a.fun();
b.fun();
}
Working example
Is there a reason you don't want fun to be part of the classes? If fun was a virtual method on the class, and call took an A and simply did v.fun() it would find the proper implementation to execute

C++ operator overloading in abstract class

Let's say we have the following scenario:
We have a base abstract class A. Then we have classes B and C which derived from A. We also have class D which is a custom implementation of a std::vector<T> - it contains a private property list of type std::vector<T> and some custom methods to work with it.
Now my problem is as follows: I would like to overload the operator + in class A to be able to do this:
B* b = new B();
C* c = new C();
D mList = b+c; //the property *list* of mList would contain b an c
I have tried everything and can't seem to be able to get it to work and am out of ideas. Is it even possible to override an operator in a base abstract class so that it will apply to derived classes?
EDIT:
Here is what I have tried so far:
File A.h:
#pragma once
#include <string>
#include <iostream>
using namespace std;
class A
{
protected:
double price;
string name;
public:
A() :name(""){};
A(string n, double p){
price = p;
name = n;
};
~A(){};
virtual void calculate(double value) = 0;
virtual void print() const = 0;
};
File B.h:
#pragma once
#include "A.h"
class B : public A
{
private:
public:
B() :A(){};
B(string n, double p) :A(n,p){};
~B();
void calculate(double value)
{
price = price + value;
}
void print() const
{
cout << name << " says: " << " " << price;
}
};
File C.h:
#include "A.h"
class C : public A
{
private:
public:
C() :A(){};
C(string n, double p) : A(n,p){};
~C();
void calculate(double value)
{
price = price * value;
}
void print() const
{
cout << name << " says: " << " " << price;
}
};
File D.H:
#include <vector>
class D
{
private:
vector<A*> list;
public:
D(){}
~D()
{
int len = list.size();
for (int i = 0; i < len; i++)
{
delete list[i];
}
};
void push(A* item)
{
list.push_back(item);
}
A* pop()
{
A* last = list.back();
list.pop_back();
return last;
}
//I have tried overriding it here and in A.h
friend D D::operator+(A* first, A* second)
{
D temp;
temp.push(first);
temp.push(second);
return temp;
}
};
It looks like you're are adding two pointers, so A::operator+() won't even be called. But to answer your question, yes, operator overloading is inheritable. Even from an abstract base class.
class A
{
public:
virtual void test() = 0;
int operator+(const A &a) {return 42;}
};
class B : public A
{
void test() {};
};
class C : public A
{
void test() {};
};
int main()
{
B* b = new B();
C* c = new C();
cout << "result: " << *b + *c << endl;
return 0;
}
Output:
result: 42
When c in C* and d is a D* if you write c+d you're just adding pointers, whatever overloads you defined.
Maybe you could redefine pointer addition for A* with a global operator(A*, A*) (not sure it's possible) but it would be quite dangerous for users since it overrides standard behavior.
The better solution is to define operators on references (const) and not pointers, which in your case is a little less convenient since you'd have to write: list = *c + *d;
Also, since you're using containers of pointers for polymorphism, I strongly recommend using shared_ptr.
Working code below (simplified, but with the ability to add more than 2 elements):
#include <list>
using std::list;
struct A {
list<const A*> operator+(const A& right) { // A + A
list<const A*> r;
r.push_back(this);
r.push_back(&right);
return r;
}
list<const A*> operator+(const list<const A*> & right) { // A + list
list<const A*> r = right;
r.push_front(this);
return r;
}
virtual void print() const = 0;
};
list<const A*> operator+(const list<const A*> & left, const A & right) { // list + A
list<const A*> r = left;
r.push_back(&right);
return r;
}
#include <iostream>
struct B : A {
void print() const { std::cout << "B" << std::endl; }
};
struct C : A {
void print() const { std::cout << "C" << std::endl; }
};
int main() {
B b;
C c;
B* pb = new B;
list<const A*> lst = b + c + *pb;
for(list<const A*>::iterator i = lst.begin(); i != lst.end(); ++i) {
(*i)->print();
}
return 0;
}
Take a look at this code-example:
#include <iostream>
class B;
class A;
class A
{
public:
virtual void overrideProp() = 0;
friend int operator+(const B& b, const A& a);
friend std::ostream& operator<<(std::ostream& os, const A& a)
{
return os << a.prop;
}
protected:
int prop;
};
class B : public A
{
public:
B(){overrideProp();}
void overrideProp(){prop=1;}
};
class C : public A
{
public:
C(){overrideProp();}
void overrideProp(){prop=3;}
};
int operator+(const B& b, const A& a)
{
return b.prop + a.prop;
}
class D
{
public:
void operator=(const int& i){d = i;}
friend std::ostream& operator<<(std::ostream& os, const D& a)
{
return os << a.d;
}
private:
int d;
};
int main()
{
B b;
C c;
D d; d = b+c;
std::cout << "B contains: " << b << " C contains: " << c << " D contains: " << d;
}
The output is B contains: 1 C contains: 3 D contains: 4
Here's an compilable and runnable example (http://codepad.org/cQU2VHMp) I wrote before you clarified the question, maybe it helps. The idea is that the addition overload can either be unary (and D defined as a friend), as here, or defined as a non-member binary operator using public methods. Note that I have to dereference the pointers b and c to make this work, as adding pointers often don't make sense.
#include <iostream>
#include <string>
class D {
public:
void Foo() {
std::cout << "D: " << member_ << std::endl;
}
friend class A;
private:
std::string member_;
};
class A {
public:
virtual void Foo() = 0;
A(const std::string &member) : member_(member) {}
D operator+(const A &rhs) {
D out;
out.member_ = member_ + " " + rhs.member_;
return out; // Uses the default copy constructor of D
}
protected:
std::string member_;
};
class B : public A {
public:
B(const std::string &member) : A(member) {}
void Foo() {
std::cout << "B: " << member_ << std::endl;
}
};
class C : public A {
public:
C(const std::string &member) : A(member) {}
void Foo() {
std::cout << "C: " << member_ << std::endl;
}
};
int main() {
B *b = new B("hello");
C *c = new C("world");
b->Foo();
c->Foo();
D d = (*b) + (*c);
d.Foo();
delete b;
delete c;
return 0;
}
The output of this program is:
B: hello
C: world
D: hello world

Can I implement Factory-pattern construction without using new()?

At the moment I'm dealing with a delightful legacy code class implementing polymorphism by switch-case:
class LegacyClass {
public:
enum InitType {TYPE_A, TYPE_B};
void init(InitType type) {m_type=type;}
int foo() {
if (m_type==TYPE_A)
{
/* ...A-specific work... */
return 1;
}
// else, TYPE_B:
/* ...B-specific work... */
return 2;
}
/** Lots more functions like this **/
private:
InitType m_type;
};
I'd like to refactor this to proper polymorphism, e.g.:
class RefactoredClass {
public:
virtual ~RefactoredClass(){}
virtual int foo()=0;
};
class Class_ImplA : public RefactoredClass {
public:
virtual ~Class_ImplA(){}
int foo() {
/* ...A-specific work... */
return 1;
}
};
class Class_ImplB : public RefactoredClass {
public:
virtual ~Class_ImplB(){}
int foo() {
/* ...B-specific work... */
return 2;
}
};
Unfortunately, I have one crucial problem: due to optimization and architectural considerations, within a primary use of LegacyClass, I cannot use dynamic allocation; the instance is a member of a different class by composition:
class BigImportantClass{
/* ... */
private:
LegacyClass m_legacy;
}
(In this example, BigImportantClass may be dynamically allocated, but the allocation needs to be in one continuous virtual segment, and a single new() call; I can't make further calls to new() in the BigImportantClass ctor or in subsequent initialization methods.)
Is there a good way to initialize a concrete implementation, polymorphically, without using new()?
My own progress so far: What I can do is provide a char[] buffer as a member of BigImportantClass, and somehow initialize a concrete member of RefactoredClass in that memory. The buffer would be large enough to accommodate all implementations of RefactoredClass. However, I do not know how to do this safely. I know the placement-new syntax, but I'm new to dealing with alignment (hence, warned off by the C++-FAQ...), and aligning generically for all concrete implementations of the RefactoredClass interface sounds daunting. Is this the way to go? Or do I have any other options?
Here's some code... just doing the obvious things. I don't use C++11's new union features, which might actually be a more structured way to ensure appropriate alignment and size and clean up the code.
#include <iostream>
template <size_t A, size_t B>
struct max
{
static const size_t value = A > B ? A : B;
};
class X
{
public:
X(char x) { construct(x); }
X(const X& rhs)
{ rhs.interface().copy_construct_at_address(this); }
~X() { interface().~Interface(); }
X& operator=(const X& rhs)
{
// warning - not exception safe
interface().~Interface();
rhs.interface().copy_construct_at_address(this);
return *this;
}
struct Interface
{
virtual ~Interface() { }
virtual void f(int) = 0;
virtual void copy_construct_at_address(void*) const = 0;
};
Interface& interface()
{ return reinterpret_cast<Interface&>(data_); }
const Interface& interface() const
{ return reinterpret_cast<const Interface&>(data_); }
// for convenience use of virtual members...
void f(int x) { interface().f(x); }
private:
void construct(char x)
{
if (x == 'A') new (data_) Impl_A();
else if (x == 'B') new (data_) Impl_B();
}
struct Impl_A : Interface
{
Impl_A() : n_(10) { std::cout << "Impl_A(this " << this << ")\n"; }
~Impl_A() { std::cout << "~Impl_A(this " << this << ")\n"; }
void f(int x)
{ std::cout << "Impl_A::f(x " << x << ") n_ " << n_;
n_ += x / 3;
std::cout << " -> " << n_ << '\n'; }
void copy_construct_at_address(void* p) const { new (p) Impl_A(*this); }
int n_;
};
struct Impl_B : Interface
{
Impl_B() : n_(20) { std::cout << "Impl_B(this " << this << ")\n"; }
~Impl_B() { std::cout << "~Impl_B(this " << this << ")\n"; }
void f(int x)
{ std::cout << "Impl_B::f(x " << x << ") n_ " << n_;
n_ += x / 3.0;
std::cout << " -> " << n_ << '\n'; }
void copy_construct_at_address(void* p) const { new (p) Impl_B(*this); }
double n_;
};
union
{
double align_;
char data_[max<sizeof Impl_A, sizeof Impl_B>::value];
};
};
int main()
{
{
X a('A');
a.f(5);
X b('B');
b.f(5);
X x2(b);
x2.f(6);
x2 = a;
x2.f(7);
}
}
Output (with my comments):
Impl_A(this 0018FF24)
Impl_A::f(x 5) n_ 10 -> 11
Impl_B(this 0018FF04)
Impl_B::f(x 5) n_ 20 -> 21.6667
Impl_B::f(x 6) n_ 21.6667 -> 23.6667
~Impl_B(this 0018FF14) // x2 = a morphs type
Impl_A::f(x 7) n_ 11 -> 13 // x2 value 11 copied per a's above
~Impl_A(this 0018FF14)
~Impl_B(this 0018FF04)
~Impl_A(this 0018FF24)
I implemented this using C++11 unions. This code seems to work under g++ 4.8.2, but it requires the -std=gnu++11 or -std=c++11 flags.
#include <iostream>
class RefactoredClass {
public:
virtual ~RefactoredClass() { }; // Linking error if this is pure. Why?
virtual int foo() = 0;
};
class RefactorA : RefactoredClass {
double data1, data2, data3, data4;
public:
int foo() { return 1; }
~RefactorA() { std::cout << "Destroying RefactorA" << std::endl; }
};
class RefactorB : RefactoredClass {
int data;
public:
int foo () { return 2; }
~RefactorB() { std::cout << "Destroying RefactorB" << std::endl; }
};
// You may need to manually create copy, move, &ct operators for this.
// Requires C++11
union LegacyClass {
RefactorA refA;
RefactorB refB;
LegacyClass(char type) {
switch (type) {
case 'A':
new(this) RefactorA;
break;
case 'B':
new(this) RefactorB;
break;
default:
// Rut-row
break;
}
}
RefactoredClass * AsRefactoredClass() { return (RefactoredClass *)this; }
int foo() { return AsRefactoredClass()->foo(); }
~LegacyClass() { AsRefactoredClass()->~RefactoredClass(); }
};
int main (void) {
LegacyClass A('A');
LegacyClass B('B');
std::cout << A.foo() << std::endl;
std::cout << B.foo() << std::endl;
return 0;
}
Somebody should have made an answer by now...so here's mine.
I'd recommend using a union of char array and one of the biggest integer types:
union {
char refactored_class_buffer[ sizeof RefactoredClass ];
long long refactored_class_buffer_aligner;
};
I also strongly recommend putting an assert or even an if(check) throw; into your factory so that you never, ever, exceed the size of your buffer.
If the data is the same for each case, and you're only changing behaviuor, you don't need to allocate in your core - this is basically a strategy pattern using singleton strategies. You end up using polymorphism in your logic, but not in your data.
class FooStrategy() {
virtual int foo(RefactoredClass& v)=0;
}
class RefactoredClass {
int foo() {
return this.fooStrategy(*this);
}
FooStrategy * fooStrategy;
};
class FooStrategyA : public FooStrategy {
//Use whichever singleton pattern you're happy with.
static FooStrategyA* instance() {
static FooStrategyA fooStrategy;
return &fooStrategy;
}
int foo(RefactoredClass& v) {
//Do something with v's data
}
}
//Same for FooStrategyB
Then when you create a RefactoredClass you set its fooStrategy to FooStrategyA::instance().