I am writing a method that gets called with a pointer to a CDWordArray, I
want to call the member function Copy (), which takes a const. I know I can
copy each element by looping through, but there must be a better way. I feel
like this should be simple and I'm just missing it.

The following compiles, but fails at runtime in a call to memcpy.

void CMyClass::MethodA (CDWordArray *pArray)
{
CDWordArray OrigArray;

OrigArray.Copy (*pArray); //the memcpy is done within Copy ()

...
}

TIA

Re: I have a pointer, need to call method that takes a const by Ben

Ben
Mon Jan 07 18:05:20 CST 2008


"bpelot@hotmail.com" <bpelot@hotmail.com@discussions.microsoft.com> wrote in
message news:F374D2BA-3E16-43D2-A0F4-22C277A70C0E@microsoft.com...
>
> I am writing a method that gets called with a pointer to a CDWordArray, I
> want to call the member function Copy (), which takes a const. I know I
> can
> copy each element by looping through, but there must be a better way. I
> feel
> like this should be simple and I'm just missing it.

What's the prototype for Copy()? Is it a reference parameter? You are
dereferencing the pArray pointer in the call and I wouldn't have expected
that was necessary.

>
> The following compiles, but fails at runtime in a call to memcpy.
>
> void CMyClass::MethodA (CDWordArray *pArray)
> {
> CDWordArray OrigArray;
>
> OrigArray.Copy (*pArray); //the memcpy is done within Copy ()
>
> ...
> }
>
> TIA
>



Re: I have a pointer, need to call method that takes a const by bpelothotmailcom

bpelothotmailcom
Mon Jan 07 18:27:00 CST 2008


> What's the prototype for Copy()? Is it a reference parameter? You are
> dereferencing the pArray pointer in the call and I wouldn't have expected
> that was necessary.
>

That was my thought too (makes me feel a little better). The prototype is:
void Copy( const CDWordArray& src );

If I try it as suggested:
OrigArray.Copy (pArray);
I get a compiler error:
error C2664: 'Copy' : cannot convert parameter 1 from 'class CDWordArray *'
to 'const class CDWordArray &'



Re: I have a pointer, need to call method that takes a const by David

David
Mon Jan 07 22:06:34 CST 2008

bpelot@hotmail.com wrote:
> I am writing a method that gets called with a pointer to a CDWordArray, I
> want to call the member function Copy (), which takes a const. I know I can
> copy each element by looping through, but there must be a better way. I feel
> like this should be simple and I'm just missing it.
>
> The following compiles, but fails at runtime in a call to memcpy.
>
> void CMyClass::MethodA (CDWordArray *pArray)
> {
> CDWordArray OrigArray;
>
> OrigArray.Copy (*pArray); //the memcpy is done within Copy ()
>
> ...
> }

bpelot:

I don't see what is wrong, but

1. You might find your problem would go away if you used
std::vector<DWORD> rather than CDWordArray. The memory management of the
STL is much better (and cleaner) than that of the MFC collection classes.

2. Why not write your method to take a const reference:

void CMyClass::MethodA (const CDWordArray& array)
{
}

--
David Wilkinson
Visual C++ MVP

Re: I have a pointer, need to call method that takes a const by Ulrich

Ulrich
Tue Jan 08 02:39:20 CST 2008

bpelot@hotmail.com wrote:
[ Note here: if you quote me, please pay me some respect by doing it
properly. Here you are quoting Ben Voigt, and I think he deserves the same
treatment. ]
>> What's the prototype for Copy()? Is it a reference parameter? You are
>> dereferencing the pArray pointer in the call and I wouldn't have expected
>> that was necessary.
>>
>
> That was my thought too (makes me feel a little better).

I don't understand why you are passing around pointers at all when the
pointer can't be NULL anyway. That is what C++ uses references for.

> The prototype
> is: void Copy( const CDWordArray& src );
>
> If I try it as suggested:
> OrigArray.Copy (pArray);
> I get a compiler error:
> error C2664: 'Copy' : cannot convert parameter 1 from
> 'class CDWordArray *' to
> 'const class CDWordArray &'


I hope this should be clear now, using the formatting above. You actually
have to dereference a pointer to get a reference. The additional 'const' is
then added implicitly.

Uli


Re: I have a pointer, need to call method that takes a const by Alexander

Alexander
Wed Jan 09 12:07:02 CST 2008

Well, that's exactly what the original code does, right? OP's
problem was at runtime, not comiple time.

To OP: What do you mean when you say it fails? Does it
crash? If so, what's the call stack?

Note this is MFC so you may want to also ask in the MFC group:
microsoft.public.vc.mfc (though I suspect all the MFC folks visit
here as well)

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@mvps.org
MVP VC FAQ: http://vcfaq.mvps.org
=====================================

"Ulrich Eckhardt" <eckhardt@satorlaser.com> wrote in message
news:vepb55-odm.ln1@satorlaser.homedns.org...
> bpelot@hotmail.com wrote:
> [ Note here: if you quote me, please pay me some respect by doing it
> properly. Here you are quoting Ben Voigt, and I think he deserves the same
> treatment. ]
>>> What's the prototype for Copy()? Is it a reference parameter? You are
>>> dereferencing the pArray pointer in the call and I wouldn't have
>>> expected
>>> that was necessary.
>>>
>>
>> That was my thought too (makes me feel a little better).
>
> I don't understand why you are passing around pointers at all when the
> pointer can't be NULL anyway. That is what C++ uses references for.
>
>> The prototype
>> is: void Copy( const CDWordArray& src );
>>
>> If I try it as suggested:
>> OrigArray.Copy (pArray);
>> I get a compiler error:
>> error C2664: 'Copy' : cannot convert parameter 1 from
>> 'class CDWordArray *' to
>> 'const class CDWordArray &'
>
>
> I hope this should be clear now, using the formatting above. You actually
> have to dereference a pointer to get a reference. The additional 'const'
> is
> then added implicitly.
>
> Uli
>



Re: I have a pointer, need to call method that takes a const by bpelothotmailcom

bpelothotmailcom
Wed Jan 09 15:16:11 CST 2008

If I have offended Ben Voigt, I would like to offer my sincere apology. Each
person here has taken time to offer help and insight, and I am grateful. But
I am clearly in the dark regarding how I improperly handled Ben Voigt's
response. If you could tell me, so that I do not commit the offense in the
future, I would appreciate it.

Brian

"Ulrich Eckhardt" wrote:

> bpelot@hotmail.com wrote:
> [ Note here: if you quote me, please pay me some respect by doing it
> properly. Here you are quoting Ben Voigt, and I think he deserves the same
> treatment. ]
> >> What's the prototype for Copy()? Is it a reference parameter? You are
> >> dereferencing the pArray pointer in the call and I wouldn't have expected
> >> that was necessary.
> >>
> >
> > That was my thought too (makes me feel a little better).
>
> I don't understand why you are passing around pointers at all when the
> pointer can't be NULL anyway. That is what C++ uses references for.
>
> > The prototype
> > is: void Copy( const CDWordArray& src );
> >
> > If I try it as suggested:
> > OrigArray.Copy (pArray);
> > I get a compiler error:
> > error C2664: 'Copy' : cannot convert parameter 1 from
> > 'class CDWordArray *' to
> > 'const class CDWordArray &'
>
>
> I hope this should be clear now, using the formatting above. You actually
> have to dereference a pointer to get a reference. The additional 'const' is
> then added implicitly.
>
> Uli
>
>

Re: I have a pointer, need to call method that takes a const by Ben

Ben
Wed Jan 09 15:37:35 CST 2008


"bpelot@hotmail.com" <bpelothotmailcom@discussions.microsoft.com> wrote in
message news:D5550794-0EEE-4B67-8546-BE0DE1658DDF@microsoft.com...
> If I have offended Ben Voigt, I would like to offer my sincere apology.
> Each
> person here has taken time to offer help and insight, and I am grateful.
> But
> I am clearly in the dark regarding how I improperly handled Ben Voigt's
> response. If you could tell me, so that I do not commit the offense in
> the
> future, I would appreciate it.

Not at all. In fact, I'm probably quite guilty of exactly the same thing,
which is snipping out the attribution along if the top section of the quoted
material wasn't relevant.

Note that Ulrich specifically kept the "bpelot@hotmail.com wrote" when he
replied. I'm pretty sure that's what he's talking about.

>
> Brian
>
> "Ulrich Eckhardt" wrote:
>
>> bpelot@hotmail.com wrote:
>> [ Note here: if you quote me, please pay me some respect by doing it
>> properly. Here you are quoting Ben Voigt, and I think he deserves the
>> same
>> treatment. ]
>> >> What's the prototype for Copy()? Is it a reference parameter? You
>> >> are
>> >> dereferencing the pArray pointer in the call and I wouldn't have
>> >> expected
>> >> that was necessary.
>> >>
>> >
>> > That was my thought too (makes me feel a little better).
>>
>> I don't understand why you are passing around pointers at all when the
>> pointer can't be NULL anyway. That is what C++ uses references for.
>>
>> > The prototype
>> > is: void Copy( const CDWordArray& src );
>> >
>> > If I try it as suggested:
>> > OrigArray.Copy (pArray);
>> > I get a compiler error:
>> > error C2664: 'Copy' : cannot convert parameter 1 from
>> > 'class CDWordArray *' to
>> > 'const class CDWordArray &'
>>
>>
>> I hope this should be clear now, using the formatting above. You actually
>> have to dereference a pointer to get a reference. The additional 'const'
>> is
>> then added implicitly.
>>
>> Uli
>>
>>



Re: I have a pointer, need to call method that takes a const by Ulrich

Ulrich
Thu Jan 10 02:39:05 CST 2008

bpelot@hotmail.com wrote:
> If I have offended Ben Voigt, I would like to offer my sincere apology.
> Each person here has taken time to offer help and insight, and I am
> grateful. But I am clearly in the dark regarding how I improperly
> handled Ben Voigt's response.

It's just this "attribution line", i.e. that you mark who wrote something
when quoting them, that's all. This is not a strong rule or law, but I
consider it a way to respectfully treat each other.

cheers

Uli