Re: How to Duplicate CString's Default Data Type
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Sat, 11 Mar 2006 23:39:29 -0500
I think this may have already been answered, but here's my two cents...
The parameter is copied by value because the parameter is a pointer and the pointer is
copied by value. If you create a complex structure, the entire structure will be copied
by value. This is why a CString is really just a wrapper for a pointer to a heap object.
The operations on CString basically manipulate that heap object. This is why you can pass
it to printf, because the pointer is not only a pointer to a heap object but looks like a
pointer to the actual object type expected as an (LPCTSTR). And that's the whole trick.
joe
On Sat, 11 Mar 2006 15:46:36 -0700, "Jonathan Wood" <jwood@xxxxxxxxxxxxxxxx> wrote:
Jolest,Joseph M. Newcomer [MVP]
I'm not saying you are wrong,
Well, could we say that it appears that you have serious doubts...? :)
Actually, I had no reason to think you are wrong and, in fact, have since
studied the CString source and have a better understanding of what it is
doing and was able to confirm what you said about only one data member.
How does the compiler know that it should not make a copy of the CString
object in Test2?
I think you're looking at the wrong side of the question. The compiler
only does what it's told to do. Therfore, the question is not "Why
didn't the compiler copy the CString in Test2?". (The answer to that
one is simple, it didn't copy it because it was never told to copy
it...)
Actually, that question didn't come out of thin air. I think I need to give
a little more background so you'll understand where I'm coming from. I wrote
a buffer template class that I was using with a non-Microsoft compiler. When
typed to TCHAR, the template included methods so that the object behaved
like a string. However, we found that when passed untyped as a second
argument to printf, the compiler made a copy of the entire object, passed it
to printf, and then called delete after printf returned.
So I think I have a pretty good reason to ask why the same doesn't happen
with CString, regardless of the number of data members it contains.
In Test2(..), the printf(..) function-template that the compiler saw
(probably in the MSVC header file AFX.H) before compiling your code
determined what kind of calling standard was going to be used in your
code when calling printf. The calling standard that manages pushing
"var-arg" (variable number of arguments) arguments for a function like
printf is an old standard (it pre-dates C++) that doesn't really have
anything to do with "object copying".
Please understand I am not new to programming--it may save you some typing.
I've been programming various languages (including C) for about 20 years. I
understand the hows and whys of the declaration for printf, and I know what
I've seen a compiler do when I tried passing my own object as one of the
untyped arguments.
If you change the definition for Helper(..) to "void Helper(CString&
s)", you'll see all that extra object-duplication go away.
Okay, we'd *really* save some typing if you took it that I had a bit more
knowledge about what we're talking about.
I understand how objects are passed by value. What I've been saying is
that's exactly what a compiler tried to do with my buffer class when passing
it untyped to printf. Now you're saying it doesn't pass the object, just the
data in the object. I guess I could run some more tests with the VC compiler
to see if it tries copy the entire object to printf like the other compiler
did. Because unless the VC compiler has a completely different behavior in
this regard, I still don't understand why it doesn't create a copy of the
CString object, regardless of the number and type of data members it
contains.
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- References:
- Re: How to Duplicate CString's Default Data Type
- From: Tom Serface
- Re: How to Duplicate CString's Default Data Type
- From: Jonathan Wood
- Re: How to Duplicate CString's Default Data Type
- From: Tom Serface
- Re: How to Duplicate CString's Default Data Type
- From: Jonathan Wood
- Re: How to Duplicate CString's Default Data Type
- From: jolest
- Re: How to Duplicate CString's Default Data Type
- From: Jonathan Wood
- Re: How to Duplicate CString's Default Data Type
- From: jolest
- Re: How to Duplicate CString's Default Data Type
- From: Jonathan Wood
- Re: How to Duplicate CString's Default Data Type
- From: jolest
- Re: How to Duplicate CString's Default Data Type
- From: Jonathan Wood
- Re: How to Duplicate CString's Default Data Type
- Prev by Date: Re: Painter for CMyDocView does not inherit from CView - How to?
- Next by Date: Re: Serialization
- Previous by thread: Re: How to Duplicate CString's Default Data Type
- Next by thread: Re: How to Duplicate CString's Default Data Type
- Index(es):
Relevant Pages
|