Function lookup problem

B

Brian Tyler

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
 
R

red floyd

Brian said:
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().
 
B

Brian Tyler

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
 
B

Brian Tyler

Brian Tyler said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top