C++.Coding.Standards.1918.Rules.Guidelines [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

C++.Coding.Standards.1918.Rules.Guidelines [Electronic resources] - نسخه متنی

Herb Sutter, Andrei Alexandrescu

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید


Examples


Example: Using a factory function to insert a post-constructor call. Consider:

class B {

// hierarchy root
protected:
B() {/* ... */ }
virtual void PostInitialize() {/* ... */ }

// called right after construction
public:
template<class T>
static shared_ptr<T> Create() {

// interface for creating objects
shared_ptr<T> p( new T );

p->PostInitialize();
return p;
}
};
class D : public B {/* ... */ };

// some derived class
shared_ptr<D> p = D::Create<D>();

// creating a D object

This rather fragile design sports the following tradeoffs:Items 45 and 46).

  • D must define a constructor with the same parameters that

    B selected. Defining several overloads of

    Create can assuage this problem, however; and the overloads can even be templated on the argument types.

  • If the requirements above are met, the design guarantees that

    PostInitialize has been called for any fully constructed

    B -derived object.

    PostInitialize doesn't need to be virtual; it can, however, invoke virtual functions freely.



  • / 521