您的位置:首页 > Web前端

Defining a Copy Constructor for a Reference Class Type

2013-04-02 23:37 375 查看
You are unlikely to be doing it very often, but if you do pass objects of a reference class type to a function by value, you must implement a public copy constructor; this situation can arise with the Standard Template
Library implementation for the CLR, which you will learn about in Chapter 10.

The parameter to the copy constructor must be a const reference, so you would defi ne the copy constructor for the Box class like this:

Box(const Box% box) : Length(box.Length), Width(box.Width), Height(box.Height)
{ }

In general, the form of the copy constructor for a ref class T that allows a ref type object of type T to be passed by value to a function is:

T(const T% t)
{
// Code to make the copy...
}

Occasionally, you may also need to implement a copy constructor that takes an argument that is a handle. Here ’ s how you could do that for the Box class:

Box(const Box^ box) : Length(box- > Length), Width(box- > Width), Height(box- > Height)
{}

As you can see, there is little difference between this and the previous version.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐