一个类有多少个默认方法?

时间:2010-10-28 15:15:14

标签: c++

对不起,这看似简单,但有人问我这个问题,我不确定。

一个空的C ++类带有什么功能?

构造, 复制构造函数, 分配, 析构函数?

是吗?还是有更多?

5 个答案:

答案 0 :(得分:45)

在C ++ 03中有4:

  • 默认构造函数:仅在声明时声明 没有用户定义的构造函数 声明。使用时定义

  • 复制构造函数 - 仅在用户尚未声明时才声明。如果使用则定义

  • 复制分配运算符与上述相同

  • 析构函数与上述相同

在C ++ 11中还有两个:

  • 移动构造函数
  • 移动分配运算符

编译器也可能无法生成其中的一些。例如,如果类包含例如引用(或任何其他无法复制分配的引用),则编译器将无法为您生成复制赋值运算符。 For more information read this

答案 1 :(得分:12)

如果我定义以下类

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.

注意:
如果定义了 ANY 构造函数,则不会构建默认构造函数 如果用户定义了替代方法,则不会构建其他方法。

当我们有成员和基础时,默认实现更有趣:

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.

答案 2 :(得分:7)

只需展开Armen Tsirunyan answer,这里有方法的签名:

.child {
    background: none repeat scroll 0 0 #F6F6F6;
    border: 1px solid #CCCCCC;
    border-bottom: 1px solid #CCCCCC;
    border-bottom-right-radius: 6px;
    border-bottom-left-radius: 6px;
    border-right: 1px solid #CCCCCC;
    border-top: 1px solid #CCCCCC;
    border-top-left-radius: 6px;
    border-top-right-radius: 6px;
    min-height: 749px;
    background-color: white;
    display:table;
    float:right;
    min-width: 900px;
    position:absolute;
    overflow: visible;
    height:100%;
}

.parent {
    overflow: visible;
    min-height: 100%;
    height: 100%;
    padding: 13px 25px 75px 25px;
    min-width: 900px;
}

答案 3 :(得分:0)

  是吗?

是的,就是这样。

编译器默认生成

  • 默认构造函数
  • 复制构造函数
  • 副本分配运算符
  • 析构函数

一个班级

使用Clang的-ast-dump选项时,您可以看到默认生成的默认构造函数,复制构造函数和赋值运算符

prasoon@prasoon-desktop ~ $ 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))))


};

答案 4 :(得分:0)