🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.comp.lang.learn.c-c++
Article #838

Re: function parameter declared type const short int * &, and call fails if value passed is a short int * (without const).

#838
From: Ulrich Eckhardt
Date: Wed, 02 Jun 2004 18:20
46 lines
1922 bytes
Mitchell Laks wrote:
> I have a function with prototype something like
> 
>         findAndGetUint16Array (const Uint16*&)
> 
> (where Uint is just some 16 bit integer type). Now if I pass it a
> parameter initialized as
> 
>         Uint16 * p = NULL;
> 
> the compiler complains and doesnt compile my code: while, if instead I
> initialized p as 
> 
>         const Uint16 * p = NULL;
> 
> and pass it as a parameter the compilation and code works. 

This all is not specific to g++, follow-up set to acllc.
 
> Why is this?

The point is this:

Assume inside the function you have this code:

// constant integer
int const x = 42;
void function( int const*& p)
{
   // assign address of x to p
   p = &x;
}

void other_function()
{
   int* k;
   function(k);
   /** k now points to x but is not a pointer-to-const-int which means 
   you could now modify x although it was declared constant. */
}

Uli

-- 
Questions ?
see  C++-FAQ Lite: http://parashift.com/c++-faq-lite/  first !
Technical Details
Message-ID: <2i6k33Fjbiu8U1@uni-berlin.de>
Path: arc1-ca.pugleaf.net!controlnews3.google.com!news2.google.com!fu-berlin.de!uni-berlin.de!not-for-mail
References: <1Wnvc.14744$bD4.10504@nwrdny02.gnilink.net>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 114592,609040a7c4796bc6,start
X-Google-Thread: 10ba33,ba81bd455610ba58
X-Google-Attributes: gid114592,gid10ba33,public
Path: arc1-ca.pugleaf.net!controlnews3.google.com!news2.google.com!fu-berlin.de!uni-berlin.de!not-for-mail
From: Ulrich Eckhardt <doomster@knuut.de>
Newsgroups: alt.comp.lang.learn.c-c++,gnu.g++.help
Subject: Re: function parameter declared type const short int * &,  and call fails if value passed is a short int * (without const).
Followup-To: alt.comp.lang.learn.c-c++
Date: Wed, 02 Jun 2004 20:20:17 +0200
Lines: 47
Message-ID: <2i6k33Fjbiu8U1@uni-berlin.de>
References: <1Wnvc.14744$bD4.10504@nwrdny02.gnilink.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7Bit
X-Trace: news.uni-berlin.de 1x/26+qWDzhbtrRTHc1dnAD2jlL/9OoecCFRDnBGDtcgdXCFst
User-Agent: KNode/0.7.6