Function lookup problem

Discussion in 'C++' started by Brian Tyler, Apr 8, 2008.

  1. Brian Tyler

    Brian Tyler Guest

    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
    Brian Tyler, Apr 8, 2008
    #1
    1. Advertising

  2. Brian Tyler

    red floyd Guest

    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().
    red floyd, Apr 8, 2008
    #2
    1. Advertising

  3. Brian Tyler

    Brian Tyler Guest

    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
    Brian Tyler, Apr 8, 2008
    #3
  4. Brian Tyler

    Brian Tyler Guest

    On Tue, 08 Apr 2008 12:48:08 -0700, Chris Thomasson wrote:

    > "Brian Tyler" <> wrote in message
    > news:AaPKj.44228$...
    >> 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
    Brian Tyler, Apr 8, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy G

    Lookup like function

    Andy G, Feb 8, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    321
    fd123456
    Feb 12, 2005
  2. John Collyer

    Function lookup tables?

    John Collyer, Oct 4, 2003, in forum: C++
    Replies:
    12
    Views:
    1,276
    Karl Heinz Buchegger
    Oct 6, 2003
  3. Lucas Lemmens

    Silly function call lookup stuff?

    Lucas Lemmens, Sep 27, 2005, in forum: Python
    Replies:
    8
    Views:
    303
    Tom Anderson
    Sep 28, 2005
  4. Prasad
    Replies:
    4
    Views:
    2,022
    Prasad
    Jun 19, 2006
  5. StephQ
    Replies:
    6
    Views:
    311
Loading...

Share This Page