简体   繁体   English

C ++ Copy构造语法

[英]C++ Copy Constructor Syntax

I apologize in advance, my C++ is rusty... 我提前道歉,我的C ++生锈了......

What does 是什么

: m_nSize(sizeof(t1))

mean in the following section? 在下一节意味着什么?

class CTypeSize
{
   public:
      template<class T>
      CTypeSize(const T &t1) :
      m_nSize(sizeof(t1))
      {
      }
      ~CTypeSize(void){ };
      int getSize(void) const{ return m_nSize; }
   private:
      const int m_nSize;
};

I understand copy constructors, but I remember the syntax as Class::Class(const Class& p). 我理解复制构造函数,但我记得语法为Class :: Class(const Class&p)。 Am I thinking of something else, or is that an alternative syntax? 我在想别的东西,还是那种替代语法?

Thanks! 谢谢!

It has nothing todo with copy ctor. 它与复制ctor没什么关系。 You are initializing the variable m_nSize using initializer list with the sizeof the template argument t1. 您正在使用初始化列表初始化变量m_nSize,其大小为模板参数t1。

There is no way to initialize a member variable directly in the class defination. 无法直接在类定义中初始化成员变量。 This is called initialization list. 这称为初始化列表。 You can imagine it to be something like: 你可以想象它是这样的:

const int m_nSize = sizeof(t1);

C++0x allow the above form by the way. 顺便说一下,C ++ 0x允许上面的形式。

CTypeSize(const T &t1) is the constructor of the class. CTypeSize(const T&t1)是类的构造函数。 Members of the class can be initialised in the constructor. 可以在构造函数中初始化类的成员。

class testclass { // constructor: a, b, c are set to // 0, 1, 2 testclass() : a(0), b(1), c(2) { } class testclass {//构造函数:a,b,c设置为// 0,1,2 testclass():a(0),b(1),c(2){}

int a, b, c; int a,b,c; // members }; //成员};

In your example, ": m_nSize(sizeof(t1))" means that m_nSize is initialised with the value of sizeof(t1). 在您的示例中,“:m_nSize(sizeof(t1))”表示使用sizeof(t1)的值初始化m_nSize。

Your question is double: 你的问题是双重的:

The : member( value ) syntax initializes a new object's member to value . : member( value )语法将新对象的成员初始化为value

But template< typename T> Class( const T& ) is not the copy constructor. 但是template< typename T> Class( const T& ) 不是复制构造函数。 That one is Class( const Class& ) . 那个是Class( const Class& )

So 所以

#include <iostream>
struct C {
   template< typename T >
   C( const T& t ) { std::cout << "T"; }

  // C( const C& c ) { std::cout << "C"; }
};

int main() { C c1(1); C c2(c1); }

Will result in the template constructor to be called, followed by the 'synthesized' copy constructor (will output just "T".) 将导致调用模板构造函数,然后是“合成”复制构造函数(将仅输出“T”。)

When you insert the copy constructor explicitly, that one will be called (output will be "TC"). 当您显式插入复制构造函数时,将调用该复制构造函数(输出将为“TC”)。

There's one more important thing regarding the declaration of member-variable CTypeSize::m_nSize . 关于成员变量CTypeSize::m_nSize的声明,还有一个重要的事情。 Do you notice a const modifier in that declaration? 你注意到那个声明中有一个const修饰符吗?

class member-var declared as "const" can be initialized only in initialization list. class member-var declared as "const"只能在初始化列表中初始化。

As AraK mentioned, in C++11 const member-var may also be initialized with the const expression. 正如AraK所提到的,在C ++ 11中,const member-var也可以用const表达式初始化。 This is compile-time case, while initialization list allows const member-var to be initialized at run-time. 这是编译时的情况,而初始化列表允许const member-var在运行时初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM