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

M

muchan

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
 
M

muchan

Vadym said:
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...)

Thanks for the suggestion.

muchan
 
M

muchan

Vadym said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top