Let's say I have a class that allocates some arbitrary element data. There are two common ways I've seen (I know there are others):
class A { public: A(); ~A(); //Accessors... private: B *mB; } A::A() { mB = new B(); } A::~A() { delete B; }
Versus ...
class A { public: //Accessors... private: B mB; }
Assume that A itself is allocated on the heap by the consumer code.
In general, which method is preferred? I understand that specific situations really encourage this or that, but in the absence of these requirements is one way? What are the tradeoffs?
. new/delete, , , , . ++ , IMHO
.
, B , B, B. , B A (f'rinstance, A swap B), .
. , A.hh , B (.. A.h #include "B.hh"), , A.hh, B.hh.
A.hh
A.h
#include "B.hh"
B.hh
- , , , , . ; , .
, ( ). , , .
, PIMPL, ( ). (, boost::shared_ptr) .
boost::shared_ptr
, , , .
: . .
( , , ..).
:
, ( , pimpl ..)
Source: https://habr.com/ru/post/1762679/More articles:Исключить ошибки FxCop DoNotPassLiteralsAsLocalizedParameters от создания экземпляра исключений или локализовать сообщения об исключениях - c#Priority queue that can be saved to disk? - cachingJSON vs. XML parsing speed in Flash AS2 - jsonHow do you mark a Ruby binding as trusted? - multithreadingBest SQLite Administrator - sqliteReorder date with javascript - javascriptCan an iPhone application write files? - iphonemysql создает триггер в базе данных, прослушивая изменения таблицы в другой базе данных - sqlCSS problem with box shadow and floating box - htmlBreak Project WCF RIA Services - .netAll Articles