473,395 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

What to do when you want a Shallow Copy?

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
8 1918
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
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
> 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
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
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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Birt | last post by:
My understanding about defining your own copy and assignment constructors is whenever there is a member of pointer type. Is this true or is there any exception to this"rule"? How about when you...
9
by: ceo | last post by:
Hi there, I'm reffering to a text that says following: "To summarize: When a copy of an object is generated because it passed to a function, the object's constructor function is not called....
2
by: Alex | last post by:
Entering the following in the Python shell yields >>> help(dict.copy) Help on method_descriptor: copy(...) D.copy() -> a shallow copy of D >>>
1
by: | last post by:
any body please help me ,Please explain what is a shallow copy and what is a deep copy?
1
by: bubby | last post by:
Should I be concerned about the classic "Deep/Shallow" copy problem when returning objects, specifically a DataTable or DataView from a method? For example, see the code below: private...
10
by: dgk | last post by:
I'm searching for a way to determine if control values have changed. For some it's easy, such as TextBox.Modified. But looking at the radiobutton I come across IsMirrored. Curious, I look to the...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
26
by: saxenavaibhav17 | last post by:
what is Deep Copy, Shallow copy and Bitwise copy, Memberwise copy? and what is the difference between them? pls help vaibhav
5
by: raylopez99 | last post by:
In C++, you have symbolic overriding of "+", "=", etc, which C# also has. This question is not really about that. Rather, in C#, when you say: MyObject X = new MyObject(); MyObject Y = new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.