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.
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.
相关文章推荐
- AFNetworking报错"_UTTypeCopyPreferredTagWithClass", referenced from: _AFContentTypeForPathExtens
- No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.
- No suitable constructor found for type [simple type, class com.bonc.vbap.dataservice.core.metadata.i
- AFNetworking报错"_UTTypeCopyPreferredTagWithClass", referenced from: _AFContentTypeForPathExtens
- No suitable constructor found for type [simple type, class com.beiyanght.dxhbm.vehiclemanagement.business.Student.dto.StudentCondition]: can not insta
- AFNetworking报错"_UTTypeCopyPreferredTagWithClass", referenced from: _AFContentTypeForPathExtens
- Json转对象失败:"No suitable constructor found for type [simple type, class com.test.faster.domain.respons
- How do you define a copy constructor or assignment operator for a class that contains a pointer to a (abstract) base class?
- C++ 编译出现undefined reference to `vtable for Class 和 undefined reference to typeinfo for Class
- Qt经典出错信息之undefined reference to `vtable for classname
- java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.Has
- Class to disable copy and assign constructor
- The type java.lang.Object cannot be resolved.It is indirectly referenced from required .class file
- JsonMappingException: No suitable constructor found for type
- The type java.util.Optional cannot be resolved. It is indirectly referenced from required .class....
- Redefining already defined constructor for class
- The type com.sun.star.lang.XEventListener cannot be resolved. It is indirectly referenced from required .class files
- Cannot determine embedded database driver class for database type NONE
- The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files
- The type java.lang.String cannot be resolved. It is indirectly referenced from required .class files