By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,407 Members | 920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,407 IT Pros & Developers. It's quick & easy.

What to do when you want a Shallow Copy?

P: n/a
Hi all you C++ gurus out there,
I've got what may be an unusual question. What's the best way to
get a shallow copy of a class that you've programmed to prevent
accidental shallow copying? I've got an operator= that does deep copy,
and a copy constructor that does deep copy... is there any way for me
to get a shallow copy without writing a function that maually copies
each member datum into an out parameter? I'd love to be able to just
return *this, but that'd invoke my deep copy constructor, which I don't
want. Any suggestions?

Jun 23 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
coredumperror wrote:
I've got what may be an unusual question. What's the best way to
get a shallow copy of a class that you've programmed to prevent
accidental shallow copying? I've got an operator= that does deep copy,
and a copy constructor that does deep copy... is there any way for me
to get a shallow copy without writing a function that maually copies
each member datum into an out parameter?


Whats wrong with Object b = a.shallowCopy();?

What does shallow copy mean to you?

Why would writing such a function be hard?

The ultimate answer to your question is "C++ Reflection", which is an
absurdly hairy topic, and much harder than just writing

Or do you have too many classes that need shallow copies? So then why are
there too many classes?

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jun 23 '06 #2

P: n/a
co***********@gmail.com wrote:
I've got what may be an unusual question. What's the best way to
get a shallow copy of a class that you've programmed to prevent
accidental shallow copying? I've got an operator= that does deep
copy, and a copy constructor that does deep copy... is there any way
for me to get a shallow copy without writing a function that maually
copies each member datum into an out parameter?
Most likely, no. Copying through construction and copying through
assignment are two copying that exist. You can always memcpy, but
it usually is a BAD IDEA(tm) if your class has virtual functions, for
example. Besides, the language give no guarantees if your class is
not a POD.
I'd love to be able
to just return *this, but that'd invoke my deep copy constructor,
which I don't want. Any suggestions?


How would you use it, BTW?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 23 '06 #3

P: n/a
> Most likely, no. Copying through construction and copying through
assignment are two copying that exist. You can always memcpy, but
it usually is a BAD IDEA(tm) if your class has virtual functions, for
example. Besides, the language give no guarantees if your class is
not a POD.
What's a POD?
I'd love to be able
to just return *this, but that'd invoke my deep copy constructor,
which I don't want. Any suggestions?


How would you use it, BTW?


I've only got one class so far, which is a really simple RawImage
class. It contains the pointer to an array of bytes for each pixel,
and ints for the width, the height, the color channels, and the total
size (width*height*chans). I'd like to be able to copy the pointer
value and the ints for width, etc in a more concise way than manually
assigning them into an out parameter. Maybe I'm just trying to be too
clever for my own good?

-Robert

Jun 23 '06 #4

P: n/a
co***********@gmail.com wrote:
Most likely, no. Copying through construction and copying through
assignment are two copying that exist. You can always memcpy, but
it usually is a BAD IDEA(tm) if your class has virtual functions, for
example. Besides, the language give no guarantees if your class is
not a POD.


What's a POD?


Plain Old Data. Basically a built-in type or a class/struct that has only
public members, no virtual functions, no base classes, no user-defined
constructors or destructor and only member variables that are themselves
POD.
> I'd love to be able
> to just return *this, but that'd invoke my deep copy constructor,
> which I don't want. Any suggestions?


How would you use it, BTW?


I've only got one class so far, which is a really simple RawImage
class. It contains the pointer to an array of bytes for each pixel,
and ints for the width, the height, the color channels, and the total
size (width*height*chans). I'd like to be able to copy the pointer
value and the ints for width, etc in a more concise way than manually
assigning them into an out parameter. Maybe I'm just trying to be too
clever for my own good?


I'd do it the other way round. Let the copy constructor and assignment
operator make a shallow copy and write a copy() member function that makes
a deep copy. But that means that the data has to be reference counted.

Jun 23 '06 #5

P: n/a
co***********@gmail.com wrote:
What's a POD?


http://www.parashift.com/c++-faq-lit....html#faq-26.7

Also, you should read the rest of the FAQ while you're there. There's
lots of good stuff in it.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 23 '06 #6

P: n/a

Rolf Magnus wrote:
co***********@gmail.com wrote:
What's a POD?
Plain Old Data. Basically a built-in type or a class/struct that has only
public members, no virtual functions, no base classes, no user-defined
constructors or destructor and only member variables that are themselves
POD.


Ah, I didn't know that acronym, but I understood the concept. :)
> I'd love to be able
> to just return *this, but that'd invoke my deep copy constructor,
> which I don't want. Any suggestions?

How would you use it, BTW?
I've only got one class so far, which is a really simple RawImage
class. It contains the pointer to an array of bytes for each pixel,
and ints for the width, the height, the color channels, and the total
size (width*height*chans). I'd like to be able to copy the pointer
value and the ints for width, etc in a more concise way than manually
assigning them into an out parameter. Maybe I'm just trying to be too
clever for my own good?


I'd do it the other way round. Let the copy constructor and assignment
operator make a shallow copy and write a copy() member function that makes
a deep copy. But that means that the data has to be reference counted.


I really don't like the idea of making shallow destructors and copy
constructors...my C++ professor at college drilled that into my head
pretty hard. I guess I'll just stick with my
RawImage.ShallowCopy(RawImage *out) function.
http://www.parashift.com/c++-faq-lit....html#faq-26.7

Also, you should read the rest of the FAQ while you're there. There's
lots of good stuff in it.


I've used that FAQ for years, and I love it! I asked my question here
only after looking for the answer there :).

Jun 24 '06 #7

P: n/a
coredumperror wrote:
I really don't like the idea of making shallow destructors and copy
constructors...my C++ professor at college drilled that into my head
pretty hard.
Did they tell you that some objects should be big and stationary, and that
"value" objects should be small and should copy around? The ultimate
examples here would be simCity, to simulate a city, and std::string, whose
object identity is less important.
I guess I'll just stick with my
RawImage.ShallowCopy(RawImage *out) function.


Why not a return value?

Otherwise, why not a reference?

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jun 24 '06 #8

P: n/a
I've only got one class so far, which is a really simple RawImage
class. It contains the pointer to an array of bytes for each pixel,
and ints for the width, the height, the color channels, and the total
size (width*height*chans). I'd like to be able to copy the pointer
value and the ints for width, etc in a more concise way than manually
assigning them into an out parameter. Maybe I'm just trying to be too
clever for my own good?
Why not just return a const reference? Like:

const RawImage& f()
{
static RawImage ri;

// blah blah blah

return ri;
}

int main()
{
const RawImage& r = f();
void* p = r.pixels_ptr();
int i = r.width();
// and so on...
}
Ben

-Robert

Jun 24 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.