VC2005 b2: gcnew /w copy constructor?

Started by
2 comments, last by snk_kid 18 years, 8 months ago
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?
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?
Advertisement
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]
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?
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?
Quote:Original post by Erzengeldeslichtes
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?



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.

This topic is closed to new replies.

Advertisement