Disambiguating between const and non-const member

Discussion in 'C++' started by Martin Magnusson, Jul 4, 2007.

  1. Suppose I have a class like this one:

    class A
    {
    int* getint()
    {
    return const_cast<int*>(getint());
    }

    const int* getint() const
    {
    //some complicated code
    return i;
    }

    int* i;
    };

    That is, I have a const member returning a const pointer, but I also
    want to be able to access a non-const pointer to the same data, without
    doing a lot of copy-paste coding. So I'd like to call the const function
    from the non-const function. For cleanliness, I'd like the functions to
    have the same name, and disambiguate between the two based on the
    context where they are called. This is generally not a problem, but with
    this implementation, the process gets stuck in an infinite recursive
    loop when the non-const method is called.

    Is there a way to call the const getint() from within the non-const
    getint(), or would I have to copy the body of the const method, or
    rename it const_getint, to get rid of the recursive behaviour?
     
    Martin Magnusson, Jul 4, 2007
    #1
    1. Advertising

  2. Martin Magnusson wrote:

    > Suppose I have a class like this one:
    >
    > class A
    > {
    > int* getint()
    > {
    > return const_cast<int*>(getint());
    > }
    >
    > const int* getint() const
    > {
    > //some complicated code
    > return i;
    > }
    >
    > int* i;
    > };
    >
    > That is, I have a const member returning a const pointer, but I also
    > want to be able to access a non-const pointer to the same data, without
    > doing a lot of copy-paste coding. So I'd like to call the const function
    > from the non-const function. For cleanliness, I'd like the functions to
    > have the same name, and disambiguate between the two based on the
    > context where they are called. This is generally not a problem, but with
    > this implementation, the process gets stuck in an infinite recursive
    > loop when the non-const method is called.


    Yes.

    > Is there a way to call the const getint() from within the non-const
    > getint(), or would I have to copy the body of the const method, or
    > rename it const_getint, to get rid of the recursive behaviour?


    Convert this to a const A*, and call getint() for that pointer:
    const A* p = this;
    return const_cast<int*>(p->getint());

    --
    rbh
     
    Robert Bauck Hamar, Jul 4, 2007
    #2
    1. Advertising

  3. Martin Magnusson

    S S Guest

    On Jul 4, 5:56 pm, Martin Magnusson <> wrote:
    > Suppose I have a class like this one:
    >
    > class A
    > {
    > int* getint()
    > {
    > return const_cast<int*>(getint());


    return const_cast<int*>(static_cast<const A&>(*this)->getint());

    > }
    >
    > const int* getint() const
    > {
    > //some complicated code
    > return i;
    > }
    >
    > int* i;
    >
    > };
    >
    > That is, I have a const member returning a const pointer, but I also
    > want to be able to access a non-const pointer to the same data, without
    > doing a lot of copy-paste coding. So I'd like to call the const function
    > from the non-const function. For cleanliness, I'd like the functions to
    > have the same name, and disambiguate between the two based on the
    > context where they are called. This is generally not a problem, but with
    > this implementation, the process gets stuck in an infinite recursive
    > loop when the non-const method is called.
    >
    > Is there a way to call the const getint() from within the non-const
    > getint(), or would I have to copy the body of the const method, or
    > rename it const_getint, to get rid of the recursive behaviour?
     
    S S, Jul 4, 2007
    #3
    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 Dingley

    Disambiguating separated node sets

    Andy Dingley, Oct 20, 2005, in forum: XML
    Replies:
    2
    Views:
    391
    Dimitre Novatchev
    Oct 20, 2005
  2. Santa
    Replies:
    1
    Views:
    1,121
    Mark A. Odell
    Jul 17, 2003
  3. Terry Hancock

    gettext and "disambiguating comments"

    Terry Hancock, Jul 11, 2005, in forum: Python
    Replies:
    1
    Views:
    498
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Jul 11, 2005
  4. gangesmaster

    proposal: disambiguating type

    gangesmaster, May 21, 2006, in forum: Python
    Replies:
    1
    Views:
    254
    Heiko Wundram
    May 21, 2006
  5. fungus
    Replies:
    13
    Views:
    901
    fungus
    Oct 31, 2008
Loading...

Share This Page