From: "Paul Ranson"
This is a multi-part message in MIME format.
------=_NextPart_000_0110_01C4C683.56A90770
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
You can pass objects by value or by reference, the behaviour is = consistent.
class Date ;
bool DoDate ( Date d )
{
// d is passed by value, DoDate has its own copy.
}
bool DoDate ( Date& d )
{
// d is a reference to the caller's Date object. DoDate may change the =
callers object.
}
bool DoDate ( const Date& d )
{
// d is a reference to the caller's Date object. DoDate may NOT change =
the callers object.
}
bool DoDate ( Date * pd )
{
// pd is a pointer to the caller's Date object. DoDate may change the =
caller's object.
}
bool DoDate ( const Date * pd )
{
// pd is a pointer to the caller's Date object. DoDate may NOT change =
the caller's object.
}
It can be inefficient to pass objects by value, in effect a temporary =
copy is made which only exists for the life of the function call. So in =
that case use a const reference.=20
Paul
"Antti Kurenniemi" wrote in
message = news:4190d345{at}w3.nls.net...
> "John Beckett"
wrote in message =
> news:419087c1.2407381{at}216.144.1.254...
>> In case Geo reads this literally, I think it should be clarified.
>> Consider:
>>
>> void func()
>> {
>> int i;
>> Date d;
>> // ... do stuff ...
>> }
>>
>> It's not true that the above creates a pointer to the Date.
>=20
> Thanks for clarifying - I knew I said something very wrong. But (I'm =
just=20
> asking this and not arguing, because it's been ages since I did any =
"real"=20
> C++): when you pass an int as a variable to a function, you are =
actually=20
> passing a variable by value, thus creating a new variable in the =
function=20
> that was called, and then you can change the value in that function =
without=20
> affecting the original one, right (nevermind passing actual pointers =
for=20
> now)? But when you pass that "d" to a function, you're passing a =
"pointer"=20
> (reference? handle? what do you call it now?) to that object and *not* =
> creating another object in the scope of the function that was called, =
right?=20
> Is it even possible to actually pass an object by value to a function? =
This=20
> is basically what I meant by "d" being more of a pointer than a =
variable,=20
> it's just hidden by the compiler / language.
>=20
> Or did I just confuse poor ol' schoolboy Geo more?
>=20
>=20
> Antti Kurenniemi
>=20
>
------=_NextPart_000_0110_01C4C683.56A90770
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
You can pass objects by value or by reference, the =
behaviour is=20
consistent.
class Date =
;
bool DoDate ( =
Date d=20
)
{
// d is passed =
by value,=20
DoDate has its own copy.
}
bool DoDate ( =
Date& d=20
)
{
// d is a =
reference to the=20
caller's Date object. DoDate may change the callers object.
}
bool DoDate ( =
const Date&=20
d )
{
// d is a =
reference to the=20
caller's Date object. DoDate may NOT change the callers=20
object.}
bool DoDate ( =
Date * pd=20
)
{
// pd is a =
pointer to the=20
caller's Date object. DoDate may change the caller's =
object.
}
bool DoDate ( =
const Date * pd=20
)
{
// pd is a =
pointer to the=20
caller's Date object. DoDate may NOT change the caller's =
object.
}
It can be inefficient to pass objects by value, in =
effect a=20
temporary copy is made which only exists for the life of the function=20
call. So in that case use a const reference.
Paul
"Antti Kurenniemi"
<mailto:NOantti{at}SPAManttikPLEASE.com">
size=3D2>NOantti{at}SPAManttikPLEASE.com> = wrote in message=20
news:4190d345{at}w3.nls.net...> "John Beckett" <mailto:FirstnameSurname{at}compuserve.com.omit">
size=3D2>FirstnameSurname{at}compuserve.com.omit> wrote in=20
message > news:419087c1.2407381{at}216.144.1.254...>> In case Geo reads this literally,
I think it = should be=20
clarified.>>
Consider:>>>> void =
func()>>=20
{>> int
i;>> =
Date=20
d;>> // ... do stuff
...>>=20
}>>>> It's not true that
the above creates a pointer =
to the=20
Date.> > Thanks for clarifying - I knew I
said something = very=20
wrong. But (I'm just > asking this and not arguing,
because it's = been=20
ages since I did any "real" > C++): when you
pass an int as a = variable to=20
a function, you are actually > passing a variable by
value, thus = creating=20
a new variable in the function > that was called, and then
you = can change=20
the value in that function without > affecting the
original one, = right=20
(nevermind passing actual pointers for > now)? But when
you pass = that "d"=20
to a function, you're passing a "pointer" >
(reference? handle? = what do=20
you call it now?) to that object and *not* > creating
another = object in=20
the scope of the function that was called, right? > Is it
even = possible=20
to actually pass an object by value to a function? This >
is = basically=20
what I meant by "d" being more of a pointer than a variable,
> = it's just=20
hidden by the compiler / language.> > Or
did I just = confuse poor=20
ol' schoolboy Geo more?> >
> Antti = Kurenniemi>=20
>
------=_NextPart_000_0110_01C4C683.56A90770--
--- BBBS/NT v4.01 Flag-5
* Origin: Barktopia BBS Site http://HarborWebs.com:8081 (1:379/45)
SEEN-BY: 633/267 270 5030/786
@PATH: 379/45 1 396/45 106/2000 633/267
|