复制构造函数说明 [英] copy constructor clarification

查看:80
本文介绍了复制构造函数说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始编写托管C ++(VS 2005 Beta 2)而且我真的没有

阅读了很多有关托管和非托管C ++之间差异的内容。


使用类向导,我可以创建一个定义如下的托管类:

ref class MyClass {

public

MyClass (无效);

~MyClas(无效);

};


我想知道我是否应该添加复制和赋值构造函数。

标准语法

MyClass(const MyClass&);

MyClass& operator =(const MyClass&);

不起作用。 (编译器错误C3699)。我会更换&与^?我认为

有时默认复制构造函数不足。

部分困惑源于我无法理解差异
new和gcnew之间的
。我什么时候想要使用一个而不是另一个?


感谢任何指针(没有双关语)。

I''ve just started writing managed C++ (VS 2005 Beta 2) and I really have not
read a whole lot about the differences between managed and unmanaged C++.

Using the class wizard, I can create a managed class defined like:
ref class MyClass {
public
MyClass(void);
~MyClas(void);
};

I''d like to know whether I should add copy and assignment constructors. The
standard syntax of
MyClass(const MyClass&);
MyClass& operator=(const MyClass&);
doesn''t work. (compiler error C3699). Do I replace & with ^ ? I would think
that there''s times when the default copy constructor will be insufficient.
Part of my confusion stems from my inability to understand the difference
between new and gcnew. When would I want to use one as opposed to the other?

Thanks for any pointers (no pun intended).

推荐答案



-

Kapil Khosla,Visual C ++团队

此帖子按原样提供,不提供任何保证,并且授予没有权利

" Rob"写道:

--
Kapil Khosla, Visual C++ Team
This posting is provided AS IS with no warranties, and confers no rights
"Rob" wrote:
我刚开始编写托管C ++(VS 2005 Beta 2),我真的没有读过很多关于托管和非托管C ++之间差异的内容。

使用类向导,我可以创建一个托管类,定义如下:
ref class MyClass {
public
MyClass(void);
〜 MyClas(无效);
};

我想知道是否应该添加复制和赋值构造函数。
MyClass的标准语法(const MyClass&);
MyClass& operator =(const MyClass&);
不起作用。 (编译器错误C3699)。我会更换&与^?


等价的代码是


ref class MyClass {

public:

MyClass(void);

~MyClass(void);

MyClass(MyClass%);

MyClass%operator =(const MyClass% );

};


想想这样的定义的运算符等价:

& =%

* = ^


取消引用^仍然用*完成。


我会认为有时候默认的复制构造函数不够用。
部分困惑源于我无法理解new和gcnew之间的区别。
我什么时候想要使用一个而不是另一个?


您将使用new为本机堆上的本机对象分配内存

和gcnew为托管堆上的manged对象分配内存。你

可以在gcnew上阅读更多内容
http://msdn2.microsoft.com/library/t...us,vs.80).aspx


谢谢,

Kapil

感谢任何指针(没有双关语)。
I''ve just started writing managed C++ (VS 2005 Beta 2) and I really have not
read a whole lot about the differences between managed and unmanaged C++.

Using the class wizard, I can create a managed class defined like:
ref class MyClass {
public
MyClass(void);
~MyClas(void);
};

I''d like to know whether I should add copy and assignment constructors. The
standard syntax of
MyClass(const MyClass&);
MyClass& operator=(const MyClass&);
doesn''t work. (compiler error C3699). Do I replace & with ^ ?
The equivalent code would be

ref class MyClass {
public:
MyClass(void);
~MyClass(void);
MyClass(MyClass%);
MyClass% operator=(const MyClass%);
};

Think of the operator equivalence for definitions like this:
& = %
* = ^

Dereferencing a ^ is still done with *, though.

I would think that there''s times when the default copy constructor will be insufficient.
Part of my confusion stems from my inability to understand the difference
between new and gcnew.
When would I want to use one as opposed to the other?
You would use new to allocate memory for a native object on the native heap
and gcnew to allocate memory for a manged object on the managed heap. You
can read more on gcnew on
http://msdn2.microsoft.com/library/t...us,vs.80).aspx

Thanks,
Kapil
Thanks for any pointers (no pun intended).



Rob写道:
Rob wrote:
我刚刚开始编写托管C ++(VS 2005 Beta 2)


这不是托管扩展。但是C ++ / CLI。


我真的没有读过很多关于托管和非托管C ++之间差异的内容。

使用这个类向导,我可以创建一个托管类,定义如下:
ref class MyClass {
public
MyClass(void);
~MyClas(void);
};

我想知道是否应该添加复制和赋值构造函数。
MyClass的标准语法(const MyClass&);
MyClass& operator =(const MyClass&);
不起作用。 (编译器错误C3699)。我会更换&与^?



跟踪引用用%表示,适用于托管类型和本机类型。你也可以这样定义赋值运算符,但是这只适用于C ++,如果你使用

键入另一种语言,你就会遇到问题。

托管赋值运算符(仅适用于托管类型)在C ++ / CLI中定义为具有两个参数的
a静态成员函数。

您可以下载最新的C ++ / CLI草案(目前1.12)从这里开始:

http://www.plumhall.com/ecma/index.html

我认为
有时候默认的复制构造函数会是不足。
我的一些困惑源于我无法理解new和gcnew之间的区别。我什么时候想要使用一个而不是另一个?

感谢任何指针(没有双关语)。
I''ve just started writing managed C++ (VS 2005 Beta 2)
It is not "managed extensions" but C++/CLI.

and I really have not
read a whole lot about the differences between managed and unmanaged C++.

Using the class wizard, I can create a managed class defined like:
ref class MyClass {
public
MyClass(void);
~MyClas(void);
};

I''d like to know whether I should add copy and assignment constructors. The
standard syntax of
MyClass(const MyClass&);
MyClass& operator=(const MyClass&);
doesn''t work. (compiler error C3699). Do I replace & with ^ ?

Tracking references are denoted by % and work for both managed and native types. Also you
can define assignment operator like this, but this will work only for C++, and if you use
that type in another language you will have problems.
The managed assignment operator (that is for managed types only) is defined in C++/CLI as
a static member function with two arguments.
You may download the latest C++/CLI draft (currently 1.12) from here:

http://www.plumhall.com/ecma/index.html
I would think
that there''s times when the default copy constructor will be insufficient.
Part of my confusion stems from my inability to understand the difference
between new and gcnew. When would I want to use one as opposed to the other?

Thanks for any pointers (no pun intended).



您可以查看:

http://www23.brinkster.com/noicys/ cppcli.htm


下面的问题......


" Ioannis Vranos"写道:
Question below...

"Ioannis Vranos" wrote:
Rob写道:
Rob wrote:
我想知道是否应该添加复制和赋值构造函数。
MyClass的标准语法(const MyClass&);
MyClass& operator =(const MyClass&);
不起作用。 (编译器错误C3699)。我会更换&与^?

跟踪引用用%表示,适用于托管和本机类型。你也可以像这样定义赋值运算符,但是这只适用于C ++,如果你用另一种语言键入
你就会遇到问题。

托管赋值运算符(仅适用于托管类型)在C ++ / CLI中定义为具有两个参数的静态成员函数。
I''d like to know whether I should add copy and assignment constructors. The
standard syntax of
MyClass(const MyClass&);
MyClass& operator=(const MyClass&);
doesn''t work. (compiler error C3699). Do I replace & with ^ ?

Tracking references are denoted by % and work for both managed and native types. Also you
can define assignment operator like this, but this will work only for C++, and if you use
that type in another language you will have problems.
The managed assignment operator (that is for managed types only) is defined in C++/CLI as
a static member function with two arguments.




所以你是说我不应该将它定义为

MyClass%operator =(const MyClass%);

因为语言互操作性问题而是什么?这是吗?

void operator =(MyClass%,const MyClass%); ?


我将看一下300页的C ++ / CLI规范,但我觉得微软有关C ++托管代码开发的文件可能会更清楚一点。


谢谢。

您可以从这里下载最新的C ++ / CLI草案(目前为1.12):

http://www.plumhall.com/ecma/index.html



这篇关于复制构造函数说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆