# VC2005 b2: gcnew /w copy constructor?

This topic is 4908 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Admittedly, I'm very new to .net, CLR, CLI, managed extentions, etc. but for the most part I've figured it out. However, this has come up and I have no clue what's going on with it:
#using <system.drawing.dll>

using namespace System::Drawing;

int main()
{
Point Temp;
Point Works(Temp);
Point^ C2558 = gcnew Point(Temp);
}

As you might be able to surmise from the variable names, allocating a System::Drawing::Point on the stack and copy-constructing it works perfectly fine. However, if I try to make one on the garbage collection heap and copy construct it, I get a C2558: no copy constructor available or copy constructor is declared 'explicit'. Now, correct me if I'm wrong, but explicitness (for lack of a better word) shouldn't have any effect here since I am explicitly calling the copy constructor. The other option, copy constructor doesn't exist/isn't public, is proven wrong by the prior line. Google was rather empty on the subject, and so I ask: Is there a reason that I can't do this?

##### Share on other sites
System::Drawing::Point is a value type, CLI user defined value types cannot have special member functions so no default constructors, copy constructors, destructors, finalizers etc, etc however value types are copy constructable/assignable (otherwise that would contradict value semantics i.e. "do as the ints do") but it is more in the sense of structs in C as in a bitwise copy as opposed to a memberwise copy (i think that is correct). Value types are generally meant to be allocated on the stack (but as you know you can allocate them on un/managed heap if you really want to) hence you can *copy construct* value types on the stack but there is no real copy constructor be invoked as value types can't have special member functions, its just a bitwise copy and this is the reason why you can not copy construct on un/managed heap because they dont and can't have special member functions like copy constructors but this is legal for any value type:

using System::Drawing::Point;	Point p(30, 40);Point^ g = gcnew Point();*g = p;

Although its not the samething, assignment != initialization.

Despite all that reference types can have copy constructors which takes the form of:

ref struct foo {   foo(const foo%); // % is simillar to &};

Oh by the way an explicit constructor means a constructor declared with the standard C++ keyword explicit:

struct foo { explicit foo(***); };

Although in this context it doesn't have much to do with the problem.

[Edited by - snk_kid on August 10, 2005 6:15:56 PM]

##### Share on other sites
Okay, I understand that it doesn't actually have a copy constructor. But why can't it do a bitwise initialization on the managed heap? Why does it matter where the memory is that's to be bitwise initialized?

##### Share on other sites
Quote:
 Original post by ErzengeldeslichtesOkay, I understand that it doesn't actually have a copy constructor. But why can't it do a bitwise initialization on the managed heap? Why does it matter where the memory is that's to be bitwise initialized?

I don't think the problem is weather or not bitwise copying can be done on managed heap i think (as in i'm making an assumption here) the problem is when use operator new/gcnew normally you state the type and which constructor you want invoke i.e foo^ bar = gcnew foo(***); but as user-defined value types can't have special member functions like copy constructors as far as i'm aware there is no way to state that you want to copy construct on un/managed heap using operator new/gcnew because user-defined value types don't have real copy constructor functions. Oddest thing is built-in value types like ints although also don't have real copy constructors you can use that syntax (you can copy construct on un/managed heap like you normally do) but not for CLI user-defined value types and i have no clue why.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 13
• 9
• 9
• 15
• ### Forum Statistics

• Total Topics
634078
• Total Posts
3015362
×