What you tried is not possible in C++. You can insert a layer of abstraction (Call a function that calls the operator), but someone's beaten me to that option. So let's look at Alpha
knowing how to construct itself based on Beta
instead.
struct Alpha
{
...
// construct an Alpha from a Beta
Alpha(const Beta& source) :
foo(source.foo), bar(source.bar)
{
}
};
Now you can use std::vector::insert
to construct Alpha
s from Beta
s.
alphas.insert(alphas.begin(), betas.begin(), betas.end());
Full code with test:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Beta
{
int foo;
int bar;
Beta(int aFoo, int aBar) :
foo(aFoo), bar(aBar)
{
}
};
struct Alpha
{
float foo;
float bar;
Alpha(float aFoo, float aBar) :
foo(aFoo), bar(aBar)
{
}
Alpha(const Beta& source) :
foo(source.foo), bar(source.bar)
{
}
};
int main()
{
vector<Beta> betas
{
{ 1, 1 },
{ 2, 2 },
{ 3, 3 }
};
vector<Alpha> alphas;
alphas.insert(alphas.begin(), betas.begin(), betas.end());
for (const Alpha & a : alphas)
{
std::cout << a.foo << ',' << a.bar << std::endl;
}
}
Note that while simpler, this might not be a viable option. Alpha may not know of the existence of Beta.