Currently I have an asset class and an asset manager. When I run the constructor I basically want to force where the object is created. I know I can't just "override" what the new operator returns, and I don't want to have to create a static function by hand for each different type of asset. I'm thinking some sort of preprocessor macro that can do that for me.. the variable amount of arguments and such.
#Object Ownership
176 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.
I come from C# and had really no concept of object ownership.
I think the best option I have is to create a static function that return a unique pointer
Thing is: I dont know if its possible to automate this whole process with macros.
why do u specifically think that preprocessor would be the solution? this sounds like a xy problem to me. why not just use what the language wanta u to use naturally
i want to be able to get into the nitty gritty optimizations
for example if u want to own sth and want it to be on heap, create a unique_ptr
really i just want to be able to return a shared pointer in the constructor
why the need to externalize that
why not just create a shared ptr?
why did u write x there though
u want perfect forwarding
std::make_shared<Person>("john", 20)
i know it feels off coming from c# but its the proper cpp way
why do you think you want a shared_ptr?
in modern cpp u rarely call a constructor directly
when i use raw pointers all my experienced c++ pals are cringing at the sight
if my vector of assets changes length
something gets deleted i have a dangling polinter
bc u shouldn't use raw ptrs
yeah but then use unique_ptr or smth
also, there's not necessarily anything wrong with raw pointers
the question is why shared ptr specifically. do u want to share ownership?
depends on the context
well coming from c# i have the garbage collector
when its not referenced it gets deleted
ig im aiming for that
I understand, but like I said I want to get into the nitty gritty
the c# behavior is shared ptr in cpp. but u should go beyond that and increase ur horizon
C++ has deterministic object lifetime, which you can use to automate resource management
and what exactly do you mean by "get into the nitty gritty"?
all the aspects of memory management basically
having control of what happens and when
and you think the way to do that is to try as hard as you can to not do anything different from how you did things in C#? 😛
no : (
as u figured, in cpp u can give more control over lifetime and ownership. more than in c#.
if u want that sth is only owned by one guy, make it unique_ptr
is there a way to check with unique ptr if its invalid
if u want it to be shared, shared_ptr.
if u want it to not be owning but claim ownership in the future, weak_ptr
sure
the same way as with the rest
if (ptr)
if you're coming from C# and are trying to pick up C++, i recommend that you read a good C++ beginner's book. C++ is a fundamentally different language from C#. you're going to need to get a lot of fundamentals straight in order to use C++ effectively.
yeah, ive been told that by all my friends
i get pointers and stuff, the hurdles are just fundamental program design differences
but as a general rule of thumb: if you're reaching for shared_ptr, you're probably doing smth wrong.
a lot of things translate well. it's mostly about the ownership topic thats just fundamentally different
ok: )
cause c# just doesnt bother with that
well, you're probably familiar with using blocks in C#
yeah i was a big pointer guy in c#
so learn ur options and then use what is appropriate to express ur intentions
though obv i was really limited w/ what i could do
a key thing to understand is constructors and destructors and when and how they're called.
everything to do with lifetime builds on that
yeah. but the smart pointers do that for u in their destructors. smart pointers are good stuff
for most of ur basic needs, they replace raw pointers
how exactly would i implement this
well, the thing is, ideally, you'd just not use any of that and allocate stuff on the stack and stuff
dynamic allocation is for those rare circumstances where you actually need it
cause you cant copy a unique ptr
you can move it
if u want ro transfer ownership of a unique_ptr, u just move it
which, again, is smth that builds on those fundamentals we were talking about
coming from C#, you're not going to have a good time just jumping right into the middle of C++
point is, it's still just one guy owning it. but u can freely transfer that around
you need to start at the beginning i'm afraid
otherwise there will be just confusion and more confusion
😭
yeah u need to understand what ur expressing and what implications that has
The more I think about this the more I'm confused honestly
for example when sth has to be copied
or when it can be moved
or obsolete due to rvalue
I have a std::vector of assets, not as a pointer or anything
you're confused because you didn't start at the beginning, you jumped into the middle expecting that things are gonna be the same as C#. and you're slowly realizing that they very much are not like C#.
raw data in my vector
then the vector owns those
i cant have a raw pointer to the object because if the vector changes the pointers become invalid
the pointers basically have to track where the data moves
std::vector<Person> persons means the vector owns the persons and they should be created inside it
thats how im seeing it
yes
u can give people a reference if they just want to use it
there are basically two ways to go from here. you can keep swimming around in the ocean, trying not to drown, and painfully clear up confusion after confusing after confusion until eventually, some coherent picture starts to arise, possibly with lots of holes that are going to take you years to discover. or you can just start at the beginning and learn the fundamentals properly.
¯_(ツ)_/¯
o shit i totally forgot that existed
cpp has so many ways to express ownership, lifetime and all that. its really at the core of everything. each second line of code u write, u have to think about these things
better to learn all ur options before going to deep
https://www.murach.com/shop/murach-s-c-programming-382-detail
or
https://stroustrup.com/programming.html
would be my recommendation
value, reference, raw ptr
unique_ptr, shared_ptr, weak_ptr
const
all combinations of above
move, copy, perfect forwarding,...
all that stuff u really have to understand before writting more than mini programs
also bleeds heavily into the design of classes, including existing classes
for example vectors push_back vs emplace_back
constructing objects outside of the container, which then have to be moved or copied into it for exclusive ownership, versus constructing them in place
tl;dr: C++ is a very complex language and you're really doing yourself no favor by skipping the fundamentals.
don't worry though. we all had to go through this, it wasnt pleasant until it clicked
ull do it to
👍
i wouldnt say i exactly skipped fundamentals
yea, not saying that it's hard. it's just smth you gotta do.
it just hasnt settled
k^^
took me 3 months of active practice until i finally understood what all the textbook definitions actually mean in practice
right now i havent really even totally settled on a project
ive written a few engines in c#, so opengl just makes sense to me
ive been just playting around
unless ur making big projects, i wouldnt worry too much of making mistakes though. it's part of the journey
opengl is very tricky since it's very hard to deal with in a classic oop way, dunno i'd recommend that.
but i mean, if you're familiar with the API, you can try to write your own RAII wrappers and stuff for it
that'd be a place to practise using move semantics yourself
which might help a lot with your understanding of it
im really familiar with the api
ive kinda been screwing around with wrappers
never heard of the term RAII though 🤔
um ok
yet you just told me you didn't skip fundamentals 😛
anyways
RAII is basically all the stuff we've been talking about
it's the idea of using constructors and destructors to automate resource management
for example, you'd have a class that represents a buffer object. you'd have a constructor that creates the buffer object. and the destructor would free the buffer object.
then you can just go an create instances of that class, which will automatically create a buffer object for you. and they would automatically free the buffer object when the instance is destroyed
the smart pointers are essentially the implementation of RAII to improve raw ptr usage
ive just never heard the term before
well, that's good then
im doing exactly that
👍
then keep doing it
now here's where it gets tricky
what happens when you copy one of your buffer objects?
you shouldnt be able to
are you able to atm?
👍
the problem with that is that you're now basically stuck with the buffer object in the scope you created it in
you can't create a buffer object somewhere and then pass it to someone else to keep
yeah thats where i was starting to dabble with the asset managers
and this is where move semantics come in
i got past the abstractions stage
while you can't copy a buffer object, you can move it
i.e., have one buffer object relinquish its ownership of the underlying resource to another one
i know std::move exists other than that this is completely new
yeah
you might wanna give this a read: http://thbecker.net/articles/rvalue_references/section_01.html
it's the idea of transfering something from A to B. thats how u can give a unique_ptr to someone else
afterwards, A is dead garbage and B contains the moved object
that makes it possible for u to give someone who expects a value (=ownership) ur object without a copy. as long as ur willing to give it away forever
mhm
let's say u created a method that takes by value:
void foo(Person person)
and u have some person floating around that u want to use
Person person("john");
...
foo(person);
now that requires a copy. so it will call the copy constructor
since u want to own ur person but the method wants to own its parameter as well
but if ur willing to sacrifice ur ownership, u can transfer it:
foo(std::move(person))
now it doesn't have to create a copy
also, when ur giving it a rvalue it also doesnt have to copy it, since ur not holding ownership of rvalues:
foo(Person("john"))
and so on
there's a lot to learn about all the combinations of value/reference/const and so on