Подтвердить что ты не робот

Сколько методов по умолчанию имеет класс?

Извините, это может показаться простым, но кто-то спросил меня об этом, и я не знаю наверняка.

Пустой класс С++ содержит какие функции?

Конструктор, Копировать конструктор, Назначение, Деструктор?

Это так? Или есть еще?

4b9b3361

Ответ 1

В С++ 03 есть 4:

  • Конструктор по умолчанию: объявлен только в том случае, если не определяемый пользователем конструктор объявлен. Определено при использовании

  • Конструктор копирования - объявлен только в том случае, если пользователь не объявил его. Определено, если используется

  • Оператор копирования, как указано выше

  • Деструктор, как указано выше

В С++ 11 есть еще два:

  • Переместить конструктор
  • Оператор перемещения-назначения

Также возможно, что компилятор не сможет сгенерировать некоторые из них. Например, если класс содержит, например, ссылку (или что-то еще, что не может быть назначено копией), тогда компилятор не сможет создать для вас оператор присваивания копий. Для получения дополнительной информации прочтите это

Ответ 2

Если я определяю следующий класс

class X
{};

Компилятор определит следующие методы:

X::X()  {}                    // Default constructor. It takes zero arguments (hence default).
X::~X() {}                    // Destructor
X::X(X const& rhs) {};        // Copy constructor
X& operator=(X const& rhs)
{return *this;}              // Assignment operator.

Примечание:
Конструктор по умолчанию не создается, если определен конструктор ЛЮБОЙ.
Другие методы не создаются, если пользователь определяет альтернативу.

Что более интересно, так это реализация по умолчанию, когда у нас есть члены и база:

class Y: public X
{
    int    a;      // POD data
    int*   b;      // POD (that also happens to be a pointer)
    Z      z;      // A class
};

// Note: There are two variants of the default constructor.
//       Both are used depending on context when the compiler defined version
//       of the default constructor is used.
//
//       One does `default initialization`
//       One does `zero initialization`

// Objects are zero initialized when
//   They are 'static storage duration'
//   **OR** You use the braces when using the default constructor
Y::Y()      // Zero initializer
    : X()   // Zero initializer
    , a(0)
    , b(0)
    , z()   // Zero initializer of Z called.
{}

// Objects are default initialized when
//    They are 'automatic storage duration'
//    **AND** don't use the braces when using the default constructor
Y::Y()
    :X    // Not legal syntax trying to portray default initialization of X (base class)
    //,a  // POD: uninitialized.
    //,b  // POD: uninitialized.
    ,z    // Not legal syntax trying to portray default initialization of z (member)
{}
//
// Note: It is actually hard to correctly zero initialize a 'automatic storage duration'
//       variable (because of the parsing problems it tends to end up a a function
//       declaration). Thus in a function context member variables can have indeterminate
//       values because of default initialization. Thus it is always good practice to 
//       to initialize all members of your class during construction (preferably in the
//       initialization list).
//
// Note: This was defined this way so that the C++ is backward compatible with C.
//       And obeys the rule of don't do more than you need too (because we want the C++
//       code to be as fast and efficient as possible.


Y::Y(Y const& rhs)
    :X(rhs)              // Copy construct the base
    ,a(rhs.a)            // Copy construct each member using the copy constructor.
    ,b(rhs.b)            // NOTE: The order is explicitly defined
    ,z(rhs.z)            //       as the order of declaration in the class.
{}

Y& operator=(Y const& rhs)
{
    X::operator=(rhs);   // Use base assignment operator
    a  = rhs.a;          // Use the assignment operator on each member.
    b  = rhs.b;          // NOTE: The order is explicitly defined
    z  = rhs.z;          //       as the order of declaration in the class.
    return(*this);
}

Y::~Y()
{
    Your Code first
}
// Not legal code. Trying to show what happens.
  : ~z()
  , ~b() // Does nothing for pointers.
  , ~a() // Does nothing for POD types
  , ~X() ; // Base class destructed last.

Ответ 3

Просто чтобы продолжить на Армен Цирунян ответ вот подписи к методам:

// C++03
MyClass();                                     // Default constructor
MyClass(const MyClass& other);                 // Copy constructor
MyClass& operator=(const MyClass& other);      // Copy assignment operator
~MyClass();                                    // Destructor

// C++11 adds two more
MyClass(MyClass&& other) noexcept;             // Move constructor
MyClass& operator=(MyClass&& other) noexcept;  // Move assignment operator

Ответ 4

Это что?

Да, это так.

Компилятор генерирует по умолчанию

  • Конструктор по умолчанию
  • Конструктор копирования
  • Оператор присваивания копий
  • Деструктор

для класса

Вы можете увидеть конструктор по умолчанию, конструктор копирования и оператор присваивания, сгенерированный по умолчанию, когда вы используете опцию -ast-dump Clang

[email protected] ~ $ cat empty.cpp && clang++ -cc1 -ast-dump empty.cpp
class empty
{};

int main()
{
   empty e;
   empty e2 = e;
   {
     empty e3;
     e3 = e;
   }

}
typedef char *__builtin_va_list;
class empty {
    class empty;
    inline empty() throw(); //default c-tor
    //copy c-tor
    inline empty(empty const &) throw() (CompoundStmt 0xa0b1050 <empty.cpp:1:7>)

    //assignment operator   
    inline empty &operator=(empty const &) throw() (CompoundStmt 0xa0b1590 <empty.cpp:1:7>
  (ReturnStmt 0xa0b1578 <col:7>
    (UnaryOperator 0xa0b1558 <col:7> 'class empty' prefix '*'
      (CXXThisExpr 0xa0b1538 <col:7> 'class empty *' this))))


};