473,804 Members | 3,138 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

copy constructor clarification

Rob
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).
Nov 17 '05 #1
19 1413

--
Kapil Khosla, Visual C++ Team
This posting is provided AS IS with no warranties, and confers no rights
"Rob" wrote:
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).

Nov 17 '05 #2
Rob wrote:
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).

You may check this:

http://www23.brinkster.com/noicys/cppcli.htm
Nov 17 '05 #3
Rob
Question below...

"Ioannis Vranos" wrote:
Rob wrote:
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.


So are you saying that I shouldn't define it as
MyClass% operator=(const MyClass%);
because of language interoperabilit y problems but rather as what? Is this it?
void operator=(MyCla ss%, const MyClass%); ?

I will take a look at the 300 page C++/CLI spec, but I feel that Microsoft's
documentation on managed code development in C++ could be a little clearer.

Thanks.

You may download the latest C++/CLI draft (currently 1.12) from here:

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

Nov 17 '05 #4
Rob wrote:
So are you saying that I shouldn't define it as
MyClass% operator=(const MyClass%);
because of language interoperabilit y problems but rather as what? Is this it?
void operator=(MyCla ss%, const MyClass%); ?

Well yes, if you intend to share your code with other languages (e.g. making a managed
dll). Otherwise it is OK.
I will take a look at the 300 page C++/CLI spec, but I feel that Microsoft's
documentation on managed code development in C++ could be a little clearer.

Well, the Microsoft documentation is MSDN, and they have the VS 2005 Beta on line. Check
the following on this subject:
http://msdn2.microsoft.com/library/d...us,vs.80).aspx
and what is new in general:

http://msdn2.microsoft.com/library/x...us,vs.80).aspx
Nov 17 '05 #5
Rob wrote:

So are you saying that I shouldn't define it as
MyClass% operator=(const MyClass%);
because of language interoperabilit y problems but rather as what? Is this it?
void operator=(MyCla ss%, const MyClass%); ?

ref class MyClass
{
// ...

public:
static MyClass %operator=(cons t MyClass %x, const MyClass %y)
{
// ...
}

// ...
};
is an example. They are similar to global function ISO C++ operator overloading. You can
also define them as global functions and member functions as usual, but these will not be
accessible from the other managed languages.
Nov 17 '05 #6
Rob
Thanks. I was well aware of msdn2.microsoft .com/library, but didn't have the
specific links you provided. I had searched that site for "copy constructor"
but did not find anything. I hadn't yet tried searching for operator=,
although that page wouldn't have shown up for that either. :-)

"Ioannis Vranos" wrote:
Rob wrote:
So are you saying that I shouldn't define it as
MyClass% operator=(const MyClass%);
because of language interoperabilit y problems but rather as what? Is this it?
void operator=(MyCla ss%, const MyClass%); ?

Well yes, if you intend to share your code with other languages (e.g. making a managed
dll). Otherwise it is OK.
I will take a look at the 300 page C++/CLI spec, but I feel that Microsoft's
documentation on managed code development in C++ could be a little clearer.

Well, the Microsoft documentation is MSDN, and they have the VS 2005 Beta on line. Check
the following on this subject:
http://msdn2.microsoft.com/library/d...us,vs.80).aspx
and what is new in general:

http://msdn2.microsoft.com/library/x...us,vs.80).aspx

Nov 17 '05 #7
Rob
Actually, operator= cannot be a static member as you show below. You get
compiler error C2581 followed by C2801. In fact, binary operators such as
operator= cannot have two parameters. The first parameter is implied. See
C2804.

So, I will go back to Kapil's suggested syntax.

-Rob

"Ioannis Vranos" wrote:
Rob wrote:

So are you saying that I shouldn't define it as
MyClass% operator=(const MyClass%);
because of language interoperabilit y problems but rather as what? Is this it?
void operator=(MyCla ss%, const MyClass%); ?

ref class MyClass
{
// ...

public:
static MyClass %operator=(cons t MyClass %x, const MyClass %y)
{
// ...
}

// ...
};
is an example. They are similar to global function ISO C++ operator overloading. You can
also define them as global functions and member functions as usual, but these will not be
accessible from the other managed languages.

Nov 17 '05 #8
Rob wrote:
Actually, operator= cannot be a static member as you show below. You get
compiler error C2581 followed by C2801. In fact, binary operators such as
operator= cannot have two parameters. The first parameter is implied. See
C2804.

C++/CLI is currently in draft and has not been finished yet. VC++ 2005 is still under
development and not everything has not been implemented yet. *So far* in the draft
C++/CLI, things are as I have described them.

Beta 1 did not support other things, but now does. And the current Beta also does not
support all current C++/CLI draft features.

Do not rely completely on the current Beta to learn C++/CLI. Let's wait C++/CLI to be
finished itself first. :-)
Nov 17 '05 #9
Rob
That's fine from the perspective of learning C++/CLI, but I'm trying to use
VS 2005 Beta 2 to write code and I would've thought that correct assignment
operator syntax would've been put into the compiler kind of early on.

Otherwise, what's the "workaround "? Only create ref classes that can be
shallow copied? I posted a separate question on the syntax I need since
making the changes as suggested by Kapil leaves a problem with member
property assignment.

-Rob

"Ioannis Vranos" wrote:
Rob wrote:
Actually, operator= cannot be a static member as you show below. You get
compiler error C2581 followed by C2801. In fact, binary operators such as
operator= cannot have two parameters. The first parameter is implied. See
C2804.

C++/CLI is currently in draft and has not been finished yet. VC++ 2005 is still under
development and not everything has not been implemented yet. *So far* in the draft
C++/CLI, things are as I have described them.

Beta 1 did not support other things, but now does. And the current Beta also does not
support all current C++/CLI draft features.

Do not rely completely on the current Beta to learn C++/CLI. Let's wait C++/CLI to be
finished itself first. :-)

Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
5814
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same kind. It sounds simple but evidently .NET has difficulty with this concept for some reason. I do understand that .NET objects are created on the GC heap but that doesn't mean that they couldn't be copied from another object of the same kind when...
15
21204
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including pointers) and for objects types call their default constructor. Any others points i should know?
3
1441
by: Aire | last post by:
Will defining a copy constructor also make the default constructor not available anymore, until a default constructor is explicitly provided? Thanks!
8
2986
by: trying_to_learn | last post by:
Why do we need to explicitly call the copy constructor and the operator = , for base class and member objects in composition? ....book says "You must explicitly call the GameBoard copy-constructor or the default constructor is automatically called instead" Why cant the compiler do this on its own. if we are making an object through copr construction for an inherited class , then why not simply call the corresponding copy constructors for...
45
6370
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using parameters, I get CS1501 (no method with X arguments). Here's a simplified example which mimics the circumstances: namespace InheritError { // Random base class. public class A { protected int i;
4
2663
by: Francisco Amaro | last post by:
Hi all, Have question about inheriting a class that has parameters in the constructor such as : Public MustInherit Class MyParentClass Public mystring As String Public Sub New(ByVal paramstring As String)
1
1995
by: Carl Fenley | last post by:
I've been programming exclusively in C# for the last few years but am now working on a project where I am required to write all code in VB.NET. I'm trying to create a class with multiple overrideable contructor methods. The problem is that I don't know how to even define a contructor in VB.NET, let alone inherit the default contructor. Here is what I have so far: Public Class AppSnippets Private _instances As Integer Private...
8
4301
by: shuisheng | last post by:
Dear All, I am wondering how the default copy constructor of a derived class looks like. Does it look like class B : public A { B(const B& right) : A(right) {}
2
1348
by: Jeroen | last post by:
Hi all, I wrote a little bit of code to see the behaviour of (copy) constructors of base and derived classes. But I have a question about it. Let me explain by the following (incomplete/illustrative) code: class A { // (copy) ctors goe here... };
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9144
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4297
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3816
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.