Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Function lookup problem

Reply
Thread Tools

Function lookup problem

 
 
Brian Tyler
Guest
Posts: n/a
 
      04-08-2008
Could someone tell me why this function lookup doesn't work (gcc 4.1.2 on
64bit linux)?

struct test
{
const int& func( void ) const {
static const int i = 0;
return i;
}

private:
int& func( void ) {
return const_cast<int&>( static_cast<const test&>(*this).func() );
}
};

int main() {
test t;
int i = t.func(); // error: 'int& test::func()' is private


return i;
}

I'm sure that there is a good reason for it not being able to see the
public const version, but the thing is that I want to give read access to
some boost tuples publicly an write access privately and I would prefer
to keep the "tuple getting" code in one place. I know I could change the
read function's name, or static cast t to <const test&>, but seems a bit
awkward.

What do you think a good solution is?

Cheers,
Brian
 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      04-08-2008
Brian Tyler wrote:
> Could someone tell me why this function lookup doesn't work (gcc 4.1.2 on
> 64bit linux)?
>
> struct test
> {
> const int& func( void ) const {
> static const int i = 0;
> return i;
> }
>
> private:
> int& func( void ) {
> return const_cast<int&>( static_cast<const test&>(*this).func() );
> }
> };
>
> int main() {
> test t;
> int i = t.func(); // error: 'int& test::func()' is private
>
>
> return i;
> }
>
> I'm sure that there is a good reason for it not being able to see the
> public const version, but the thing is that I want to give read access to
> some boost tuples publicly an write access privately and I would prefer
> to keep the "tuple getting" code in one place. I know I could change the
> read function's name, or static cast t to <const test&>, but seems a bit
> awkward.
>
> What do you think a good solution is?
>


Because overload resolution and visibility are orthogonal. It finds the
non-const version of func(), and complains because it's private.

Your solution? Rename the non-const version of func().

 
Reply With Quote
 
 
 
 
Brian Tyler
Guest
Posts: n/a
 
      04-08-2008
I had guessed as much, but thought that some sort of automatic conversion
might take place... reminds me of my introduction operators and
namespaces.

I will rename the read function.

Thanks for the help.

Brian
 
Reply With Quote
 
Brian Tyler
Guest
Posts: n/a
 
      04-08-2008
On Tue, 08 Apr 2008 12:48:08 -0700, Chris Thomasson wrote:

> "Brian Tyler" <(E-Mail Removed)> wrote in message
> news:AaPKj.44228$(E-Mail Removed)...
>> Could someone tell me why this function lookup doesn't work (gcc 4.1.2
>> on 64bit linux)?

> [...]
>>
>>

> Your calling func on a non-const test object. Try this to force it work:
> __________________________________________________ ____ struct test
> {
> const int& func( void ) const {
> static const int i = 0;
> return i;
> }
>
> private:
> int& func( void ) {
> return const_cast<int&>( static_cast<const test&>(*this).func() );
> }
> };
>
> int main() {
> test t;
> test const& tx = t;
> int i = tx.func();
>
> return i;
> }
> __________________________________________________ ____
>
>
>
>
>> What do you think a good solution is?

>
> Rename your private function. I personally like to add either a 'sys_'
> or a 'prv_' prefix to my private function names. In other words use
> 'test::sys/prv_func()' instead...


Thanks for the tip Chris. I like the idea of identifying the private
functions with a prefix.

Brian
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with function lookup and inheritance StephQ C++ 6 12-09-2007 08:12 AM
Problem with CMapStringToPtr Lookup function Prasad C++ 4 06-19-2006 05:12 PM
Lookup like function Andy G ASP .Net 3 02-12-2005 11:59 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Function lookup tables? John Collyer C++ 12 10-06-2003 12:37 PM



Advertisments