How can I enable explicit casting from, lets say int, to a user defined class Foo?
I made a conversion constructor from int to Foo, but is that it? I could overload a cast operator from Foo to int, but that is not what I'm looking for.
Is there a way to enable this piece of code?
int i = 5;
Foo foo = (Foo)i;
Something like this:
struct Foo {
explicit Foo(int x) : s(x) { }
int s;
};
int main() {
int i = 5;
Foo foo =(Foo)i;
}
Read about converting constructor.
If you won't set constructor as explicit, it allows you to convert type (which constuctor accepts) to a (newly constructed) class instance.
Here is the example
#include <iostream>
template<typename T>
class Foo
{
private:
T m_t;
public:
Foo(T t) : m_t(t) {}
};
int main()
{
int i = 0;
Foo<int> intFoo = i;
double d = 0.0;
Foo<double> doubleFoo = d;
}
You need a constructor which accecpts an int
class Foo {
public:
Foo (int pInt) {
....
}
Foo (double pDouble) {
....
}
int i = 5;
Foo foo(i); // explicit constructors
Foo foo2(27);
Foo foo3(2.9);
Foo foo4 = i; // implicit constructors
Foo foo5 = 27;
Foo foo6 = 2.1;
Related
Assume that I have a struct foo and a wrapper struct wrapper for foo defined below
struct foo
{
int n;
foo(const int s) {n = s};
};
struct wrapper
{
foo f;
wrapper(const int s);
};
Is there a way to use the constructor of wrapper to initialize the member of type foo in wrapper through calling constructor foo?
This should do what you want, here foo is initialized with the same const int that was given to wrapper constructor.
struct foo
{
int n;
foo(const int s) {n = s;} // <- corrected your typo here
};
struct wrapper
{
foo f;
int u;
wrapper(const int s) : f (s), u(0){ //<- foo initialization here
}
};
I have the following code:
class example {
int x;
inline void operator=(int value) { x = value; }
};
int main() {
example foo { 100 };
int bar = foo;
}
The int bar = foo; obviously doesn't work, because I'm trying to assign a variable of type example to a variable of type int.
Is it possible to retrieve the x variable without using a getter function and without using operator.? If so, is it still possible to do purely by code inside the struct, and keeping the int bar = foo; as is?
Add a conversion function to allow implicit conversion
struct example {
int x;
inline void operator=(int value) { x = value; }
operator int() const
{
return x;
}
};
int main() {
example foo { 100 };
int bar = foo;
}
I've gone through similar questions, but none of them did not explain me where I'm doing a mistake. I've got following code:
main.cpp
int main(int argc, char* argv[]) {
Blah blah1(100);
Blah blah2(1000, 333, Foo(Foo::FOO_VAL2));
}
blah.h
class Blah
{
public:
Blah(int param1, int param2 = 666, Foo foo = Foo());
~Blah();
int param1;
int param2;
Foo foo;
};
blah.cpp
Blah::Blah(int param1, int param2, Foo foo)
{
this->param1 = param1;
this->param2 = param2;
this->foo = foo;
}
Blah::~Blah() {}
Foo.h
class Foo
{
public:
Foo(int param = Foo::FOO_VAL1);
~Foo();
static const int FOO_VAL1 = -1;
static const int FOO_VAL2 = 0;
int param;
};
Foo.cpp
Foo::Foo(int param)
{
this->param = param;
}
Foo::~Foo() {}
And when I ran the application, it cause:
‘Foo& Foo::operator=(const Foo&)’ is implicitly deleted because the default definition would be ill-formed
I know that the mistake is on the assignment this->foo = foo;, but what does it mean? Why it is failing? What should I do different way?
Thank you very much.
Don't use the constructor body to initialize members. Use the constructors initialization list.
I have a functor like this
struct foo
{
int a;
foo(a) : a(a) {}
int operator()(int b) { return a+b; }
};
And a class like this
class bar
{
public:
foo* my_ftor;
bar(foo* my_ftor) : my_ftor(my_ftor) {}
~bar() {}
};
Then suppose a pointer to this class, which contains a pointer to foo.
foo MyFoo(20);
bar MyBar(&MyFoo);
In a function I pass a reference to bar, and I want to run the functor. I got it working the following way:
void AnyFunction(bar* RefToBar)
{
int y;
y = RefToBar->my_ftor->operator()(25);
}
Is there any other "cleaner" way to dereference the functor? Something akin to
y = RefToBar->my_ftor(25);
won't work, sadly...
Any idea? Thank you
Use real references:
class bar {
public:
foo &my_ftor;
bar (foo &f) : my_ftor(f) {}
};
void AnyFunction (bar &reftobar) {
int y = reftobar.my_ftor(25);
}
And call like this
foo myFoo(20);
bar myBar (myFoo);
AnyFunction (myBar);
In the interest of completeness, here is another answer that is more of a modern approach.
class foo {
public:
foo (int i) : a(i) {}
int operator() (int x) const {
return x + a;
}
private:
int a;
};
template <typename F>
void AnyFunction (const F &func) {
int y = func(25);
}
So you can pass in a foo directly:
AnyFunction (foo (20));
Or another kind of function object, like a lambda:
AnyFunction([](int x) -> int {
return x + 20;
});
You could also extend bar to include the following function:
int run_foo (int x) const {
return my_ftor (x);
}
And bind it (#include <functional>):
AnyFunction (std::bind (&bar::run_foo, &myBar, std::placeholders::_1));
Use std::function they are designed to hold functor of any sort.
#include <functional>
#include <iostream>
struct foo
{
int _a;
foo(int a) : _a(a) {}
int operator()(int b) { return _a+b; }
};
class bar
{
public:
std::function<int (int)> _ftor;
bar(std::function<int (int)> my_ftor) : _ftor(my_ftor) {}
~bar() {}
};
void AnyFunction(bar& RefToBar)
{
int y = RefToBar._ftor(25);
std::cout << "Y: " << y << std::endl;
}
int AnotherFunction(int b)
{
return b + 11;
}
int main(int argc, char const *argv[])
{
foo MyFoo(20);
bar MyBar(MyFoo);
bar MyBar_2(AnotherFunction);
bar MyBar_3([](int b) { return b + 56; });
AnyFunction(MyBar);
AnyFunction(MyBar_2);
AnyFunction(MyBar_3);
return 0;
}
http://ideone.com/K3QRRV
y = (*RefToBar->my_ftor)(25);
(better use std::function and don't violate demeter)
I'd like to bind operator new (see example below). If the constructor doesn't have any arguments, it works fine, but if it does have arguments, I apparently have trouble getting the bind syntax correct.
#include <map>
#include <boost\function.hpp>
#include <boost\lambda\lambda.hpp>
#include <boost\lambda\construct.hpp>
#include <boost\lambda\bind.hpp>
enum TypeEnum
{
BarType,
BazType
};
class Foo
{
};
class Bar : public Foo
{
public:
Bar(int x)
{ BarVal = x; }
private:
int barVal;
};
class Baz : public Foo
{
public:
Baz(int x)
{ bazVal = 2 * x; }
private:
int bazVal;
};
class FooFactory
{
public:
FooFactory()
{
// How does this work?
factoryMap[BarType] = boost::lambda::bind(boost::lambda::new_ptr<Bar>(_1));
factoryMap[BazType] = boost::lambda::bind(boost::lambda::new_ptr<Baz>(_1));
}
Foo* getFoo(TypeEnum type, int z)
{
return factoryMap[type](z);
}
private:
std::map<TypeEnum, boost::function<Foo* (int)>> factoryMap;
};
int main()
{
FooFactory fooFactory;
Bar *newBar = static_cast<Bar*> (fooFactory.getFoo(BarType, 10));
return 0;
}
This should do:
factoryMap[BarType] = boost::lambda::bind(boost::lambda::new_ptr<Bar>(), boost::lambda::_1);
factoryMap[BazType] = boost::lambda::bind(boost::lambda::new_ptr<Baz>(), boost::lambda::_1);
Why not just to write the following? I can't see any reason to use bind in your case.
factoryMap[BarType] = boost::lambda::new_ptr<Bar>();
factoryMap[BazType] = boost::lambda::new_ptr<Baz>();