Reputation: 1
I'm trying to write a layer for making some components of a project more modular. How can I make a class Foo exactly equal to a class Bar, where Foo would be able to be used as the variable 'a' in a function int testFunc(Bar a)
?
Would the only solution be to have Foo have a definition like this?
class Foo {
public:
Foo(int a) : barReturn(a) {};
operator Bar() const { return barReturn; }
private:
Bar barReturn;
};
Upvotes: 0
Views: 397
Reputation: 462
May be you can use the polymorphism of c++.There are some diffrent components derived an interface Foo.
class Foo {
public:
virtual Foo(int a);
virtual Update(int a);
};
class Bar1 : public Foo{
public:
virtual Bar1(int a): m_a(a);
virtual Update(){
// TODO:
}
private:
int m_a;
};
class Bar2 : public Foo{
public:
virtual Bar1(int a): m_b(a);
virtual Update(){
// TODO:
}
private:
int m_b;
};
// There a module to do Foo
void ProcessModule(){
std::vector<Foo*> v;
v.push_back(new Bar1(1));
v.push_back(new Bar2(2));
for (std::vector<Foo*>::itreator it = v.begin(); v != v.end(); ++v){
it->UPdate()
}
}
Upvotes: 0
Reputation: 218333
If you want a true alias, you might do an alias:
since C++11:
using Foo = Bar;
or the old way (still valid):
typedef Bar Foo;
Upvotes: 1