473,799 Members | 3,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1943
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***********@g mail.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*c hans). 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***********@g mail.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*c hans). 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***********@g mail.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***********@g mail.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*c hans). 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.Shallo wCopy(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.Shallo wCopy(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*c hans). 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
2034
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 define a class which doesn't have a pointer type variable as member and this class could be derived, and a new member of pointer type could be added in the future? Does this mean that all classes that will be inherited in the future should be...
9
1631
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. However, when the copy of the object inside the function is destroyed, its destructor is called. By default, when a copy of an object is made, a bitwise copy occurs.
2
12354
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
1913
by: | last post by:
any body please help me ,Please explain what is a shallow copy and what is a deep copy?
1
2696
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 DataTable LoadStuff() { DataTable dt = new DataTable(); .... dt.Columns.Add(...) ....
10
4171
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 help. Not very helpful. So I look to the newsgroup. Not mentioned. So now I ask. What does the IsMirrored property tell me?
669
26251
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 paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990. http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf
26
15816
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
2100
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 MyObject(); X = Y; //what does this '=' mean?
0
9688
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9546
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10491
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7571
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5593
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.