Let's say i have following classes
class MyClass
{
private:
std::string data_;
public:
MyClass(const std::string& data) : data_(data) {}
void func()
{
std::cout << data_ << std::endl;
}
};
class MyClassWrapper
{
private:
std::unique_ptr<MyClass> obj_;
public:
MyClassWrapper(const MyClass& obj) : obj_(std::make_unique<MyClass>(obj)) {}
void func()
{
obj_->func();
}
};
and following code.
MyClassWrapper my_class(MyClass("some message"));
MyClassWrapper* my_class_ptr = &my_class;
my_class_ptr->func();
My question is, when i call func() on line my_class_ptr->func();, is the memory location of the object my_class actually accessed when i dereference my_class_ptr? And then, when i call obj_->func(); in the implementation of the function, do I physically access the obj_ field of MyClassWrapper (i mean the pointer, not what it points to)?
The reason why I'm asking this is that if i have objects of type MyClassWrapper in some container, then if the memory of the object is accessed, i would have to block other threads from adding elements to the container, and i want to know if that's necesseary.
obj->func()callsfunc()withobjasthisargument. Iffunc()access members,objis dereferenced/accessed.vector::reservecan provide flexibility and increased performance.ptris a pointer to MyClass it actually only points to the member data, and calling a member functionf()on an instance is actually more similar to calling a (freestanding) functionf(MyClass* this)std::unique_ptrin the containers (sometimesstd::shared_ptr) -- the pointer can be moved around freely and efficiently, while the actual data isn't affected at all.