In-depth learning c ++ smart pointers related

C ++ =============================== -------------
alignas bytes alignment, to align the minimum unit,   
struct alignas (. 1) {S};
the typeid (a) .name () to find variable type
constexpr function defined previously, can find out directly if a value, will find out directly during the compilation
int FACT constexpr (n-int) n-{return <. 1. 1:? (n-FACT * (n--. 1));}
struct Double X {A}
the decltype (A-> X) Y; and the like auto
decltype ((a -> x)) y; double & brackets with and without parentheses difference is that the reference

template<typename T,typename U>
auto add (T a,U b) -> decltype(a + b){
  return a + b;
}

add (1,2) Integer
add (1.0,2) double precision, double precision by default 1.0

enum class NewColor {Red,Green = 20,Blue};
NewClor r == NewColor::Blue;

int a = static_cast <int> (r); // explicitly tell the compiler, so subconversion

static_assert when compiling the checked only by
checking assert runtime

static_cast tells the compiler that you need to like this

thread_local use multithreading

using a = map <int, cstring >; <int, cstring> a equivalent Map typedef;
volatile and do not optimize Statement

class RuleOfThree {
   public:
   char * szBuf;
   private:
   RuleOfThree 
}

Rvalue reference
int A = 0
int & E = A; operation e, equivalent to A *
int && E =. 5; but not like this change int & E =. 5;
 Auto D = STD :: Move (B); the left value turn into the right value
 
base * P = the derived new new ();
the derived * PD1 = static_cast <the derived *> (P);
the derived of dynamic_cast * PD2 = <the derived *> (P); // switch base class derived class is no problem, but the following is a group class to the derived class, will be a problem, so the reasons for use of dynamic_cast, when the conversion fails, it returns NULL

* = P new new Base Base;
the Derived PD3 = static_cast * <* the Derived> (P);
the Derived * = PD4 of dynamic_cast <the Derived *> (P);
 
 destructor not unusual, because otherwise there will dump compiler defaults is not abnormal
 
 
 struct base {
   Virtual void F () {:: COUT << STD "base \ n-";}
   Virtual base ~ () {}
 };
 struct the Derived: base {
    void F () {the override 
       - yl rewritable keywords class f function the override
     }
   };

   
 std :: shared_ptr Chino guideline
 Auto_ptr
 Shared_ptr

============================================================= weak_ptr ================================================
 class Parent;
typedef std::shared_ptr<Parent> ParentPtr;
typedef std::weak_ptr<Parent>  WeakParentPtr;

chlid class: public STD :: enable_shared_from_this, <chlid>
    // define template parameters ambiguity, the principle is then constructed by Weakptr.lock the this
{
public:
    WeakParentPtr Father;
    chlid ();
    ~ chlid ();
    void checkRelation ();
} ;

typedef std::shared_ptr<chlid> ChildPtr;
typedef std::weak_ptr<chlid> WeakChildPtr;

class Parent : public std::enable_shared_from_this<Parent>
{
public:
    WeakChildPtr son;
    Parent();
    ~Parent();
    void checkRelation();
};
chlid::chlid() { std::cout << "In child\n"; }
Parent::Parent() { std::cout << "In fahter\n"; }
chlid::~chlid() { std::cout << "bye child\n"; }
Parent::~Parent() { std::cout << "bye fahter\n"; }

void handleChildandParent(const ParentPtr & p, const ChildPtr &c)
{
    auto cp = c->father.lock();
    auto pc = p->son.lock();
    if (cp == p && pc == c)
    {
        std::cout << "right relation \n";

    }
    else
    {
        std::cout << "oop!!!!!!\n";

    }
}

void chlid::checkRelation() {
    auto ps = father.lock();
    if (ps)
    {
        handleChildandParent(ps, shared_from_this());

    }
}
void Parent::checkRelation() {
    auto ps = son.lock();
    if (ps)
    {
        handleChildandParent(shared_from_this(), ps);

    }
}
 
 
 
 
=========================================  unique_ptr ================================================

void trasnsfer(uniquePtr obj)
{
  std::cout << obj>->id() << std::endl;
}

Characteristics: the only references;
 class the Parent;
typedef a unique_ptr STD :: <Object> uniquePtr;

uniquePtr Obj(new Object(1));
auto p = Obj.get()
if (p) { std::cout << p->id() std::endl; }

p = Obj.release();
delete p;

Obj.reset (); // release the pointer?
Obj.reset (new new Object (2));

trasnsfer (std :: move (Obj)); // if references to others, then his value should be nullptr;
 

Released four original articles · won praise 0 · Views 32

Guess you like

Origin blog.csdn.net/u010665493/article/details/104387556