I have a class called classA, something like this:
class classA {
private:
char* data;
public:
classA(const classA&) = delete;
~classA();
};
~classA()
{
delete[] data;
}
In another class, let's call it classB, I have as a member a shared pointer to classA:
class classB
{
private:
std::shared_ptr<classA> ptrA;
public:
classB(std::shared_ptr<classA>);
};
classB(std::shared_ptr<classA> sp) : ptrA(sp)
{}
This is how I instantiate my classB:
classA ca;
classB cb(std::make_shared<classA>(ca));
This gives me the following error:
attempting to reference a deleted function
Obviously, I am trying to reference the copy constructor, which I defined as deleted (there is a reason for this, objects of this class shouldn't be copied). But I am confused as to why the copy constructor is called since I am passing a shared pointer, and how to avoid this.
You're calling the copy constructor trying to make the shared pointer.
std::make_shared<classA>(ca)
^^ constructs the classA using these parameters
You can call make_shared<classA>() to create a shared pointer to a default constructed classA. Or chose another constructor.
This example can be simplified quite a bit.
#include <memory>
class classA {
public:
classA(const classA&) = delete;
classA() = default;
};
int main() {
classA ca; // note, no ()
auto sp = std::make_shared<classA>(ca); // problem.
auto sp2 = std::make_shared<classA>(); // this does what you want
}
You are passing ca as an argument to std::make_shared, which constructs a classA by calling classA::classA with whatever arguments that you passed to make_shared.
This might be more obvious if you consider how a make_shared might be implemented.
template <typename Cls, typename... Args>
std::shared_ptr<Cls> MakeShared(Args&&... args) {
//this is where the copying happens
return std::shared_ptr<Cls>{new Cls{std::forward<Args>(args)...}};
}
int main() {
classA ca;
auto sp = MakeShared<classA>(ca);
auto sp2 = MakeShared<classA>();
}
You pass ca to MakeShared, which then calls new Cls(...) where the ... is whatever you passed to MakeShared, in this case, another classA object.
If the above is too dense (maybe you aren't used to forward or variadic templates), then consider this simplified version of MakeShared which does the same thing for your problem case
template <typename Cls, typename Arg>
std::shared_ptr<Cls> MakeShared(const Arg& arg) {
// copy here ----+
// v
return std::shared_ptr<Cls>{new Cls{arg}};
}
int main() {
classA ca;
auto sp = MakeShared<classA>(ca);
}
Related
I want to enforce shared_ptr for a few classes. I'm using a static factory function to encapsulating private constructors:
#include <memory>
class MyClass
{
public:
static std::shared_ptr<MyClass> create() {
auto a = std::shared_ptr<MyClass>(new MyClass());
return a;
}
private:
MyClass();
~MyClass();
}
}
This template fails with C2440, (function-style cast) in VS2017, but works fine in VS2015 and I have no idea why. A make_shared-version works fine in both but requires public constructors.
Any idea which option I'm missing?
Looks like VS2017 complains about accessing destructor from std::shared_ptr, so you may want to declare std::shared_ptr as friend of MyClass. For std::make_shared you can use a trick from this answer How do I call ::std::make_shared on a class with only protected or private constructors?
class MyClass
{
public:
static std::shared_ptr<MyClass> create() {
struct make_shared_enabler : MyClass {};
return std::make_shared<make_shared_enabler>();
}
// compiles fine for gcc without friend though
//friend class std::shared_ptr<MyClass>;
private:
MyClass() {}
~MyClass() {}
};
live example
In addition to other answers: if you don't want to declare std::shared_ptr as a friend of your class and you don't want to make your destructor public, you can create a shared_ptr with the custom deleter. For that you will need some method from your MyClass that can access the private destructor and call delete. For example:
class MyClass
{
public:
static std::shared_ptr<MyClass> create() {
auto a = std::shared_ptr<MyClass>(new MyClass(),
[](MyClass* ptr) { destroy(ptr); });
return a;
}
static void destroy(MyClass* ptr) { delete ptr; }
private:
MyClass(){}
~MyClass(){}
};
// later in the source code
auto ptr = MyClass::create();
You can also declare destroy method as non-static and commit a suicide inside (one of a few situations when it actually makes sense).
I want to call a constructor of a member object in the owners constructor, but can not construct the member object in the initializer-list because of dependencies. How can I call the constructer after my initializations? I would really like to not use an init method
No.
You cannot call the constructor of a member class outside the initializer list.
PS: Even if you don't call it yourself in the initializer list, then the compiler will do it implicitly.
If you cannot call it in the initializer list, and you don't want to use an init-like method, then re-think your design/approach.
You have two options: either use dynamic storage, or placement new.
The first one is obvious (as pointed out in comments, you can use unique_ptr). If you want to avoid this, you might try placement new with std::aligned_union as storage:
class SomeClass { ... };
class Owner
{
public:
Owner()
{
m_ptr = new(&m_storage) SomeClass();
}
~Owner()
{
m_ptr->~SomeClass();
}
private:
std::aligned_union<0, SomeClass> m_storage;
SomeClass* m_ptr;
};
Note: in this case, you are responsible to calling destructor of the object, as shown above.
You can wrap m_ptr with a unique_ptr (with a deleted which only calls a destructor) to avoid this:
struct DtorDeleter
{
template<typename T>
void operator ()(T* ptr) { ptr->~T(); }
};
std::unique_ptr<SomeClass, DtorDeleter> m_ptr; // no need to call destructor manually
You can use an union for this (C++11 needed):
#include <new>
class Foo {
public:
Foo(int a) { }
};
class Bar {
public:
Bar() {
new(&m_foo) Foo(42); // call the constructor
// you can use m_foo from this point
}
~Bar() {
m_foo.~Foo(); // call the destructor
}
private:
union { // anonymous union
Foo m_foo;
};
};
Note that you need to explicitly call the destructor of m_foo at ~Bar().
want to call a constructor of a member object in the owners constructor, but can not construct the member object in the initializer-list because of dependencies
You might modify your class to have correct dependency order, and maybe use delegating constructor:
So turning:
struct S
{
S(/*..*/) : /*m1(data), m2(data),*/ data(/*..*/) // Issue, m1, m2 constructed before data
{
ComplexData2 data2 = Query();
m1.init(data, data2.someField1); // Doesn't like init method
m2.init(data, data2.someField2); // Doesn't like init method
}
Member1 m1; // Cannot be const with init method :/
Member2 m2; // Cannot be const with init method :/
ComplexData data;
};
into
struct S
{
S(/*..*/) : S(Query() /*, ..*/) {}
// private: // probably private
S(const ComplexData2& data2 /*, ..*/) :
data(/*..*/),
m1(data, data2.someField1),
m2(data, data2.someField2)
{
}
public:
ComplexData data; // Before m1, m2 for dependencies.
/*const*/ Member1 m1; // Can be const
/*const*/ Member2 m2; // Can be const
};
I'm working on implementing a reflection mechanism in C++.
All objects within my code are a subclass of Object(my own generic type) that contain a static member datum of type Class.
class Class{
public:
Class(const std::string &n, Object *(*c)());
protected:
std::string name; // Name for subclass
Object *(*create)(); // Pointer to creation function for subclass
};
For any subclass of Object with a static Class member datum, I want to be able to initialize 'create' with a pointer to the constructor of that subclass.
You cannot take the address of a constructor (C++98 Standard 12.1/12 Constructors - "12.1-12 Constructors - "The address of a constructor shall not be taken.")
Your best bet is to have a factory function/method that creates the Object and pass the address of the factory:
class Object;
class Class{
public:
Class(const std::string &n, Object *(*c)()) : name(n), create(c) {};
protected:
std::string name; // Name for subclass
Object *(*create)(); // Pointer to creation function for subclass
};
class Object {};
Object* ObjectFactory()
{
return new Object;
}
int main(int argc, char**argv)
{
Class foo( "myFoo", ObjectFactory);
return 0;
}
I encountered this same problem. My solution was a template function which called the constructor.
template<class T> MyClass* create()
{
return new T;
}
To use this as a function pointer is simple:
MyClass* (*createMyClass)(void) = create<MyClass>;
And to get an instance of MyClass:
MyClass* myClass = createMyClass();
Lambda style:
[](){return new YourClass();}
Using variadic templates you can create a wrapper which will turn the constructor into a functor.
#include <utility>
template <typename T>
struct BindConstructor{
template<typename... Args>
T operator()(Args&&...args)const{
return T(std::forward<Args>(args)...);
}
};
struct Foo {
Foo(int a, int b):a(a),b(b){}
int a;
int b;
};
template <typename Fn>
auto Bar(Fn f){
return f(10,20);
}
int main(){
Foo foo = Bar(BindConstructor<Foo>());
}
https://godbolt.org/z/5W383McTc
Hmm, odd. create is a member variable i.e. only available in class instances but the intent of it seems to be creating an instance in the first place.
You cannot take the address of a constructor, but you can create static factory methods of your own and take the address of that.
You can't use regular function pointers on methods, you have to use method pointers, which have bizarre syntax:
void (MyClass::*method_ptr)(int x, int y);
method_ptr = &MyClass::MyMethod;
This gives you a method pointer to MyClass's method - MyMethod. However this isn't a true pointer in that it's not an absolute memory address, it's basically an offset (more complicated than that due to virtual inheritance, but that stuff is implementation specific) into a class. So to use the method pointer, you have to supply it with a class, like this:
MyClass myclass;
myclass.*method_ptr(x, y);
or
MyClass *myclass = new MyClass;
myclass->*method_ptr(x, y);
Of course it should be obvious at this point that you can't use a method pointer to point to an objects constructor. In order to use a method pointer you need to have an instance of the class so it's constructor has already been called! So in your case Michael's Object Factory suggestion is probably the best way of doing it.
Using Qt, you can call a constructor with Qt reflection mechanisms (QMetaObject) if you declare the constructor as Q_INVOKABLE (nothing more to do than that).
class MyClass : public QObject {
Q_OBJECT
public:
Q_INVOKABLE MyClass(int foo);
MyClass *cloningMySelf() {
return metaObject()->newInstance(Q_ARG(int, 42));
}
};
I'm not sure you will want to embed Qt just for that feature ;-) but maybe you would like to have a look on the way it does that.
http://doc.qt.io/qt-5/metaobjects.html#meta-object-system
If you want an actual function pointer representing the constructor, not a functor or closure, you can create a universal factory template:
#include <iostream>
namespace {
template <typename Type, typename... Args>
Type create(Args... args)
{
return Type{std::forward<Args>(args)...};
}
}
template <typename Type, typename... Args>
static constexpr auto constructor = &create<Type, Args...>;
struct Foo {
Foo(double v){ std::cout << "Foo ctor " << v << std::endl; }
};
int main()
{
auto z = constructor<Foo, double>(0.33);
return 0;
}
Foo ctor 0.33
https://godbolt.org/z/qKGGGzjTs
This is similar to Michael Burr's answer, except that you don't have a static factory method per object and it allows any kind of constructor (not just default)
This is similar to bradgonesurfing's answer, except that you create one function pointer per constructor and not one functor per class representing all constructors for that class.
Even though this question is old and already has several very helpful answers, I am adding this additional suggestion because having one function pointer for every constructor of any class is useful when implementing a runtime type reflection system.
I have to register an object in a container upon its creation.
Without smart pointers I'd use something like this:
a_class::a_class()
{
register_somewhere(this);
}
With smart pointers I should use shared_from_this but I can't use that in the constructor.
Is there a clean way to solve this problem? What would you do in a similar situation?
I'm thinking about introducing an init method to call just after creation and put everything in a factory function like this:
boost::shared_ptr<a_class> create_a()
{
boost::shared_ptr<a_class> ptr(new a_class);
ptr->init();
return ptr;
}
Is it fine or there is a standard procedure to follow in such cases?
EDIT: Actually my case is more complex. I have 2 object which shall maintain pointers each other. So the truth is I'm not "registering" but creating another object (let's say b_class) which requires this as a parameter. b_class receives this as a weak pointer and stores it.
I'm adding this because since you are giving me design advices (which are very appreciated) at least you can know what I'm doing:
a_class::a_class()
{
b = new b_class(this);
}
In my program a_class is an entity and b_class is one of the concrete classes representing the state (in the constructor it's just the starting state). a_class needs a pointer to the current state and b_class needs to manipulate the entity.
a_class is responsible for creating and destroying b_class instances and thus maintains a shared_ptr to them but b_class need to manipulate a_class and thus maintains a weak pointer. a_class instance "survives" b_class instances.
Do you suggest to avoid using smart pointers in this case?
a_class is responsible for creating and destroying b_class instances
...
a_class instance "survives" b_class instances.
Given these two facts, there should be no danger that a b_class instance can attempt to access an a_class instance after the a_class instance has been destroyed as the a_class instance is responsible for destroying the b_class instances.
b_class can just hold a pointer to it's associated a_class instance. A raw pointer doesn't express any ownership which is appropriate for this case.
In this example it doesn't matter how the a_class is created, dynamically, part of a aggregated object, etc. Whatever creates a_class manages its lifetime just as a_class manages the lifetime of the b_class which it instantiates.
E.g.
class a_class;
class b_class
{
public:
b_class( a_class* a_ ) : a( a_ ) {}
private:
a_class* a;
};
class a_class
{
public:
a_class() : b( new b_class(this) ) {}
private:
boost::shared_ptr<b_class> b;
};
Note, in this toy example there is no need for a shared_ptr, an object member would work just as well (assuming that you don't copy your entity class).
class a_class
{
public:
a_class() : b( this ) {}
private:
b_class b;
};
If you absolutely need a shared_ptr during construction, it's best to have an 'init' function. In fact, this is the only decent approach I can think of. You should probably have a special function that creates objects of this type, to ensure init() is called, if you choose this path.
However, depending on what you're registering for, it may be a better idea to give whatever object you're registering with a plain pointer to the object in the constructor, rather than a shared_ptr. Then in the destructor, you can just unregister the object from the manager.
Why don't you use http://www.boost.org/doc/libs/1_43_0/libs/smart_ptr/enable_shared_from_this.html
struct a_class : enable_shared_from_this<a_class> {
a_class() {
shared_ptr<a_class> ptr(this);
register_somewhere(ptr);
}
};
Update: here is a complete working example:
#include <stdio.h>
#include <boost/smart_ptr/enable_shared_from_this.hpp>
struct a_class;
boost::shared_ptr<a_class> pa;
void register_somewhere(boost::shared_ptr<a_class> p)
{
pa = p;
};
struct a_class : boost::enable_shared_from_this<a_class> {
private:
a_class() {
printf("%s\n", __PRETTY_FUNCTION__);
boost::shared_ptr<a_class> ptr(this);
register_somewhere(ptr);
}
public:
~a_class() {
printf("%s\n", __PRETTY_FUNCTION__);
}
static boost::shared_ptr<a_class> create()
{
return (new a_class)->shared_from_this();
}
};
int main()
{
boost::shared_ptr<a_class> p(a_class::create());
}
Note the factory function a_class::create(). Its job is to make sure that only one reference counter gets created. Because
boost::shared_ptr<a_class> p(new a_class);
Results in creation of two reference counters and double deletion of the object.
There is no need for shared_ptr in your code (as you show it and explain it). A shared_ptr is only appropriate for shared ownership.
Your b_class doesn't own it's a_class, in fact is even outlived by it, so it should merely keep an observing pointer.
If b_class is polymorphic and the manipulations of a_class involve altering its b_class pointer, you should use a unique_ptr<b_class>:
class a_class;
class b_class
{
friend class a_class;
a_class* mya;
b_class(a_class*p)
: mya(p) {}
public:
virtual~b_class() {} // required for unique_ptr<b_class> to work
virtual void fiddle(); // do something to mya
};
class a_class
{
std::unique_ptr<b_class> myb;
public:
a_class()
: myb(new b_class(this)) {}
template<typename B>
void change_myb()
{
myb.reset(new B(this));
}
};
I came up with a helper class for that problem:
template <class Impl>
class ImmediatelySharedFromThis : public std::enable_shared_from_this<Impl> {
typedef std::unique_ptr<void, std::function<void(void*)>> MallocGuard;
typedef std::shared_ptr<Impl> SharedPtr;
// disallow `new MyClass(...)`
static void *operator new(size_t) = delete;
static void *operator new[](size_t) = delete;
static void operator delete[](void*) = delete;
protected:
typedef std::pair<MallocGuard&, SharedPtr&> SharingCookie;
ImmediatelySharedFromThis(SharingCookie cookie) {
MallocGuard &ptr = cookie.first;
SharedPtr &shared = cookie.second;
// This single line contains the actual logic:
shared.reset(reinterpret_cast<Impl*>(ptr.release()));
}
public:
// Create new instance and return a shared pointer to it.
template <class ...Args>
static SharedPtr create(Args &&...args) {
// Make sure that the memory is free'd if ImmediatelySharedFromThis
// is not the first base class, and the initialization
// of another base class throws an exception.
MallocGuard ptr(aligned_alloc(alignof(Impl), sizeof(Impl)), free);
if (!ptr) {
throw std::runtime_error("OOM");
}
SharedPtr result;
::new (ptr.get()) Impl(SharingCookie(ptr, result),
std::forward<Args>(args)...);
return result;
}
static void operator delete(void *ptr) {
free(ptr);
}
};
class MyClass : public ImmediatelySharedFromThis<MyClass> {
friend class ImmediatelySharedFromThis<MyClass>;
MyClass(SharingCookie cookie, int some, int arguments) :
ImmediatelySharedFromThis(cookie)
// You can pass shared_from_this() to other base classes
{
// and you can use shared_from_this() in here, too.
}
public:
....
};
...
std::shared_ptr<MyClass> obj = MyClass::create(47, 11);
A bit ugly, but it works.
For this purpose, I wrote my own drop-in replacement for shared_ptr, weak_ptr and enable_shared_from_this. You can check it out at Sourceforge.
It allows call of shared_from_this() in constructor and destructor without helper functions without space overhead compared to the standard enable_shared_from_this.
Note: creating shared_ptr's in dtors is only allowed as long as they are created only temporarily. That is, they are being destroyed before the dtor returns.
Here's my solution:
class MyClass: enable_shared_from_this<MyClass>
{
public:
//If you use this, you will die.
MyClass(bool areYouBeingAnIdiot = true)
{
if (areYouBeingAnIdiot)
{
throw exception("Don't call this constructor! Use the Create function!");
}
//Can't/Don't use this or shared_from_this() here.
}
static shared_ptr<MyClass> Create()
{
shared_ptr<MyClass> myClass = make_shared<MyClass>(false);
//Use myClass or myClass.get() here, now that it is created.
return myClass;
}
}
//Somewhere.
shared_ptr<MyClass> myClass = MyClass::Create();
(The constructor has to be public to be callable from a static member function, even an internal one...)
I'm working on implementing a reflection mechanism in C++.
All objects within my code are a subclass of Object(my own generic type) that contain a static member datum of type Class.
class Class{
public:
Class(const std::string &n, Object *(*c)());
protected:
std::string name; // Name for subclass
Object *(*create)(); // Pointer to creation function for subclass
};
For any subclass of Object with a static Class member datum, I want to be able to initialize 'create' with a pointer to the constructor of that subclass.
You cannot take the address of a constructor (C++98 Standard 12.1/12 Constructors - "12.1-12 Constructors - "The address of a constructor shall not be taken.")
Your best bet is to have a factory function/method that creates the Object and pass the address of the factory:
class Object;
class Class{
public:
Class(const std::string &n, Object *(*c)()) : name(n), create(c) {};
protected:
std::string name; // Name for subclass
Object *(*create)(); // Pointer to creation function for subclass
};
class Object {};
Object* ObjectFactory()
{
return new Object;
}
int main(int argc, char**argv)
{
Class foo( "myFoo", ObjectFactory);
return 0;
}
I encountered this same problem. My solution was a template function which called the constructor.
template<class T> MyClass* create()
{
return new T;
}
To use this as a function pointer is simple:
MyClass* (*createMyClass)(void) = create<MyClass>;
And to get an instance of MyClass:
MyClass* myClass = createMyClass();
Lambda style:
[](){return new YourClass();}
Using variadic templates you can create a wrapper which will turn the constructor into a functor.
#include <utility>
template <typename T>
struct BindConstructor{
template<typename... Args>
T operator()(Args&&...args)const{
return T(std::forward<Args>(args)...);
}
};
struct Foo {
Foo(int a, int b):a(a),b(b){}
int a;
int b;
};
template <typename Fn>
auto Bar(Fn f){
return f(10,20);
}
int main(){
Foo foo = Bar(BindConstructor<Foo>());
}
https://godbolt.org/z/5W383McTc
Hmm, odd. create is a member variable i.e. only available in class instances but the intent of it seems to be creating an instance in the first place.
You cannot take the address of a constructor, but you can create static factory methods of your own and take the address of that.
You can't use regular function pointers on methods, you have to use method pointers, which have bizarre syntax:
void (MyClass::*method_ptr)(int x, int y);
method_ptr = &MyClass::MyMethod;
This gives you a method pointer to MyClass's method - MyMethod. However this isn't a true pointer in that it's not an absolute memory address, it's basically an offset (more complicated than that due to virtual inheritance, but that stuff is implementation specific) into a class. So to use the method pointer, you have to supply it with a class, like this:
MyClass myclass;
myclass.*method_ptr(x, y);
or
MyClass *myclass = new MyClass;
myclass->*method_ptr(x, y);
Of course it should be obvious at this point that you can't use a method pointer to point to an objects constructor. In order to use a method pointer you need to have an instance of the class so it's constructor has already been called! So in your case Michael's Object Factory suggestion is probably the best way of doing it.
Using Qt, you can call a constructor with Qt reflection mechanisms (QMetaObject) if you declare the constructor as Q_INVOKABLE (nothing more to do than that).
class MyClass : public QObject {
Q_OBJECT
public:
Q_INVOKABLE MyClass(int foo);
MyClass *cloningMySelf() {
return metaObject()->newInstance(Q_ARG(int, 42));
}
};
I'm not sure you will want to embed Qt just for that feature ;-) but maybe you would like to have a look on the way it does that.
http://doc.qt.io/qt-5/metaobjects.html#meta-object-system
If you want an actual function pointer representing the constructor, not a functor or closure, you can create a universal factory template:
#include <iostream>
namespace {
template <typename Type, typename... Args>
Type create(Args... args)
{
return Type{std::forward<Args>(args)...};
}
}
template <typename Type, typename... Args>
static constexpr auto constructor = &create<Type, Args...>;
struct Foo {
Foo(double v){ std::cout << "Foo ctor " << v << std::endl; }
};
int main()
{
auto z = constructor<Foo, double>(0.33);
return 0;
}
Foo ctor 0.33
https://godbolt.org/z/qKGGGzjTs
This is similar to Michael Burr's answer, except that you don't have a static factory method per object and it allows any kind of constructor (not just default)
This is similar to bradgonesurfing's answer, except that you create one function pointer per constructor and not one functor per class representing all constructors for that class.
Even though this question is old and already has several very helpful answers, I am adding this additional suggestion because having one function pointer for every constructor of any class is useful when implementing a runtime type reflection system.