Rust's equivalent of C 's this
is self
. In Rust you can do this:
struct C;
impl C {
fn some_fn(self: &Rc<Self>) {}
}
Does such feature exist in C ? The following is syntactically invalid:
class c_t {
void some_fn(std::shared_ptr<c_t> this) {
std::cout << "calling from shared_ptr";
}
};
So that I can do:
auto c = std::make_shared<c_t>();
c->some_fn(); // "calling from shared_ptr"
And yes, c->some_fn()
would work (C dereferences), but I want to know the pointer that is passed to some_fn()
though — that is, I want to know what pointer is this
.
CodePudding user response:
You cannot have
shared_ptr<C> c = std::make_shared<C>();
c->foo(); // OK
c.get()->foo(); // Should not be OK.
The nearest you can do would be something like:
class C {
public:
friend void some_fn(std::shared_ptr<C> self) {
// ...
}
};
and so
shared_ptr<C> c = std::make_shared<C>();
some_fn(c); // OK
some_fn(c.get()); // KO.
CodePudding user response:
So that I can do:
auto c = std::shared_ptr<c_t>(c_t()); c->some_fn(); // "calling from shared_ptr"
You can make this work this simply by declaring a member function (also define it somewhere):
struct c_t {
void some_fn();
};
want to know the shared_ptr pointer passed to some_fn
No, you cannot have that with the syntax that you want. Once you indirect through the shared pointer using operator->
, the information about the shared pointer has been lost. The non-static member function knows only the identity of the object (through the implicit this
), and nothing about the shared pointer.
You could instead pass the shared pointer into a non-(non-static-member-function) i.e. a static member function or a free function:
void some_fn(const std::shared_ptr<c_t>& ptr);
// ...
some_fn(c);
A non-static member function can acquire a shared pointer to itself if the class inherits std::enable_shared_from_this
, but it's quite difficult to use correctly and in such case calling the function on an object that isn't owned by shared pointer will result in undefined behaviour. Essentially, it's only safe to use when the creation of instances is encapsulated such that only shared instances can be created.
If none of those are what you want, then I suppose that the answer to the question "Does such feature exist in C ?" is: No.
CodePudding user response:
First of all, C 's this
pointer is not a shared_ptr
. It is possible to get a shared_ptr
this
by using std::enable_shared_from_this
but normally you should not need to do this (please do not do this unless you're absolutely sure why!).
Any non-static member function of c_t
will have access to what amounts to c_t* this
if the member function is non-const, and const c_t* this
if the function is const.
You should not take this
as an argument to any member function in the class. The this
pointer is implicitly declared in each non-static member function already (as per the above). Likewise, the only way to call some_fun
without a this
pointer is if you make the function static (because then there is no specific instance to begin with).
You cannot know in some_fun
if you're calling it from a shared_ptr
, unique_ptr
, c_t*
or any other type of pointer. The only distinction that can be made is whether the pointed to object is const
or not. So a construct like void some_fn(std::shared_ptr<c_t> this)
is nonsensical to begin with.
(One caveat, you might be able to figure out what type of pointer you're using by inspecting the program's stack, but I would really really advise against an approach like this unless you have a very specific case you're dealing with.)
CodePudding user response:
I think you want static methods:
class c_t
{
public:
static void some_fn (std::shared_ptr <c_t> selfPtr)
{
std::cout << "calling from shared_ptr";
}
};
and use case:
c_t::some_fn (std::make_shared<c_t> ());