In brief, my question is about member variables as pointers in unmanaged C++.
In java or c#, we have “advanced pointer”. In fact, we can’t aware the “pointer” in them. We usually initialize the member of a class like this:
member = new Member();
or
member = null;
But in c++, it becomes more confusing. I have seen many styles: using new, or leave the member variable in stack.
In my point of view, using boost::shared_ptr seems friendly, but in boost itself source code there are news everywhere. It’s the matter of efficiency,isn’t it?
Is there a guildline like “try your best to avoid new” or something?
EDIT
I realize it’s not proper to say “leave them in stack”, here’s a more proper way to say: when i need an object to be my member variable, should i prefer a object than a object*?
The Boost source code is not a good example for how you should write your source code. The Boost libraries are designed to wrap up all the tedious, difficult, and error-prone code so that you don’t have to worry about it in your code.
Your best bet is to follow two general rules in your code:
shared_ptrorscoped_ptr)