I have a class with an object as a member which doesn\'t have a default constructor. I\'d like to initialize this member in the constructor, but it seems that in C++ I can\'
If it's to initialize a variable during construction in a class's constructor the right way is:
template<class T>
udp_sock<T>::udp_sock(std::string host = "localhost", unsigned short port = 50000)
:res(_io_service)
,query(udp::v4(), host, "spec")
,ep(*res.resolve(query))
,_sock(_io_service, ep)
{
}
Edit: Forgot to mention that 'res', 'query' and 'ep' should be part of the class. Another crude method (without having _sock as a pointer) is as given below:
template<class T>
udp_sock<T>::udp_sock(std::string host = "localhost", unsigned short port = 50000)
:_sock(_io_service, udp::resolver(_io_service).resolve(udp::resolver::query(udp::v4(),host,"spec"))
{
}
This is a really ancient thread, but there is another possibility using an anonymous union. Let me first declare a helper class without a standard constructor, which will inform us about the moment of constructor and destructor calling:
#include <iostream>
#include <string>
struct Embedded
{
Embedded(const char* init_name)
: name(init_name)
{
std::cout << "Constructor of Embedded: " << name << std::endl;
}
~Embedded()
{
std::cout << "Destructor of Embedded: " << name << std::endl;
}
std::string name;
};
Now let us embed three different objects of this class in another class. One is placed inside a union (which won't be automatically initialized). For this object, constructor and destructor must be manually called (using placement new). This is possible at any position in the code.
struct Demo
{
Embedded object_1 {"object_1"};
Embedded object_2;
union // members won't be automatically constructed
{
Embedded object_3; // don't use standard initializer (as for object_1)
};
Demo()
: object_2("object_2")
{
std::cout << "Constructor of Demo" << std::endl;
new (&object_3) Embedded ("object_3");
}
~Demo()
{
object_3.~Embedded();
std::cout << "Destructor of Demo" << std::endl;
}
};
Finally, use the Demo class in its own scope:
int main()
{
std::cout << "main() start" << std::endl;
{
Demo demo;
std::cout << "demo created" << std::endl;
}
std::cout << "main() end" << std::endl;
return 0;
}
As you can see from the output, the constructor is deferred.
main() start
Constructor of Embedded: object_1
Constructor of Embedded: object_2
Constructor of Demo
Constructor of Embedded: object_3
demo created
Destructor of Embedded: object_3
Destructor of Demo
Destructor of Embedded: object_2
Destructor of Embedded: object_1
main() end
So you can exactly define the moment of the deferred constructor call. The downside is, that you also have to manually call the destructor or really bad things can happen.