471,326 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,326 software developers and data experts.

Collection.Add() as Copy of the data, not Reference to the the data


I'm a C++ programmer now poking into C#.
I wanted to write a snippet of code, equivalent of

//--- C++ code ---------------
#include <string>
#include <vector>

class obj { // a POD class
public:
std::string s;
//... other data omitted
};

std::vector<obj> v;
obj o; // a buffer on the stack
o.s = "aaa";
v.push_back();
o.s = "bbb";
v.push_back();
//----------------------------

Using System.Collenction.Generic I could write a quite similar code,

//--- C# code that failed -----
class obj {
public String s;
}

Collection<obj> col;
obj o = new obj();
o.s = "aaa";
col.Add(o);
o.s = "bbb"; // <--- here the col[0].s is also changes as "aaa"
col.Add(o);
//------------------------------

I see that the Add is adding a reference of o, so the values of o are
not copied to the collection.

Adding a Clone() function to the POD object like:

//---- C# clode that worked ---
class obj {
public String s;

public Obj Clone() // return a clone of POD object
{
Obj o = new Obj();
o.s = this.s;
return o;
}
}

Collection<obj> col;
obj o = new obj();
o.s = "aaa";
col.Add(o.Clone());
o.s = "bbb";
col.Add(o.Clone());
//-------------------------------

This seems to work, and does the same thing like C++'s code.
But writing Clone function for each data member isn't very attractive,
esp. when the structure becoming more complecated.

Isn't there a simpler syntax to do the same copy/clone things
at Collection.Add()?

muchan

Feb 9 '06 #1
3 2341
Vadym Stetsyak wrote:

AFAIR in C++ type has to have copy constructor, right? Isn't it equivalent to Clone?

m> Collection<obj> col;
m> obj o = new obj();
m> o.s = "aaa";
m> col.Add(o.Clone());
m> o.s = "bbb";
m> col.Add(o.Clone());
m> //-------------------------------

You can instantiate different objects, IMO it will be more clear that these objects are different
e.g.
obj o = new obj();
o.s = "aaa";
col.Add(o);
o = new obj();
o.s.= "bbb";
col.Add(o);

I thought this version...
but I encountered this problem while reading the data streams in a loop,
and filling the object inside the loop and at some point adding to the
Collection. if I used

obj o = new obj(); // a first instance
while (...) {
....
o.s = "something_from_the_source_stream;
continue;

....
if (some_condition_met) {
obj clone = new obj();
cloned = o; // <-- this is again the reference! ;(
col.Add(clone); // <-- so this is not good...
continue;
}
}

so before filling the data, the new should be called, like

if (some_condition_met) {
col.Add(o); // <-- here add the reference of preciously filled obj
o = new obj(); // <-- and prepare the new buffer to fill the data
continue;
}

This might work, but as a C++ programmer, I feel very very bad to loosing
the previous o at asigning the new obj()... My head is not GCed yet.
(and when I'll feel this normal, I wonder if I won't do it on C++! horror!)
Another way is that you can clone type in the Add method, however to do it, you have to
write your own collection class.

writing my own collection class doesn't sound good idea, tho...

Isn't there a simple way to creat a temporary buffer object on the stack?
(Otherwise C# is not so similar to the C/C++, indeed...)
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com


Thanks for the suggestion.

muchan
Feb 9 '06 #2
Hello, muchan!

have a look at stackalloc keyword, but beware this is unsafe mode.

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Feb 9 '06 #3
Vadym Stetsyak wrote:
Hello, muchan!

have a look at stackalloc keyword, but beware this is unsafe mode.


It's interesting, that it's possible but declared as "unsafe"...
In this wording, all my C++ programing was in unsafe mode. :)

Everybody now a days seems to see pointer as an evil, but in C++,
often declaring a alias (reference) of the content of pointer,
it can be used just like normal variable.

If I could do something like this:

unsafe void func()
{
Obj *op = stackalloc Obj;
Obj &o = *op; // o as alias to the pointee of op

// ... using o as the stack allocated object ...

} // at the end of scope it disappears from stack

This is equivalent to my C++ way of writing auto variable of the
function scope... In C++, using stack allocated variables feels
safer than memory managiment with calling "new".
But GC way looks like "just calling new, and don't worry about it".

Thank you very much.
muchan
Feb 10 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Emilia | last post: by
5 posts views Thread by Kurt Bauer | last post: by
3 posts views Thread by PauloFor | last post: by
3 posts views Thread by Sakharam Phapale | last post: by
5 posts views Thread by Jeff Stewart | last post: by
8 posts views Thread by JAL | last post: by
18 posts views Thread by Larry Herbinaux | last post: by
5 posts views Thread by David Longnecker | last post: by
reply views Thread by rosydwin | last post: by

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.