Calling non const function from a const function?

Discussion in 'C++' started by none, May 19, 2010.

  1. none

    none Guest

    I have extended a library class 'LibraryBase' which I cannot modify. 'LibraryBase' contains a const
    function 'ConstFunction'. In my subclass 'LibrarySub' I am overriding 'ConstFunction'. But in this
    function I now need to call a non const function.

    But that gives an error and as I understand also violates the const principle.

    But is there no way to call a non const function from a const function? Here is a basic example:

    class Test {
    public:
    void testConst() const {
    // this is a no go!
    testNonConst();
    }
    void testNonConst() {
    }

    private:
    };

    int main(){
    Test t;
    t.testConst();
    return 0;
    }
    none, May 19, 2010
    #1
    1. Advertising

  2. On 5/19/2010 2:41 PM, none wrote:
    > I have extended a library class 'LibraryBase' which I cannot modify.
    > 'LibraryBase' contains a const function 'ConstFunction'. In my subclass
    > 'LibrarySub' I am overriding 'ConstFunction'. But in this function I now
    > need to call a non const function.
    >
    > But that gives an error and as I understand also violates the const
    > principle.
    >
    > But is there no way to call a non const function from a const function?
    > Here is a basic example:
    >
    > class Test {
    > public:
    > void testConst() const {
    > // this is a no go!
    > testNonConst();
    > }
    > void testNonConst() {
    > }
    >
    > private:
    > };
    >
    > int main(){
    > Test t;
    > t.testConst();
    > return 0;
    > }


    The only legal way is to do a const_cast on 'this'. And only do it if
    you know that your object is non-const, which you probably can't know
    since the system is going to be calling your 'ConstFunction'...

    Why do you need to call the non-const function? The whole point in
    having the 'const' in a function is to promise that the state of the
    object is not going to change during the call. You're violating that
    promise. Could it be you're overriding a wrong function?

    Another possible solution is declaring your data that you need to change
    as 'mutable' (RTFM). Then you can change the value of such data in a
    member function declared 'const'. Not that I advocate that.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, May 19, 2010
    #2
    1. Advertising

  3. none

    Öö Tiib Guest

    On May 19, 9:41 pm, none <""mort\"@(none)"> wrote:
    > I have extended a library class 'LibraryBase' which I cannot modify. 'LibraryBase' contains a const
    > function 'ConstFunction'. In my subclass 'LibrarySub' I am overriding 'ConstFunction'. But in this
    > function I now need to call a non const function.


    Like you have already told you are developing/maintaining something on
    base of something that should not compile. It may do anything. Nothing
    is granted. It is *lot* worse situation than to write directly in
    assembler. I would ask for budget to rewrite it and if refused, i
    would turn it down. World is full of lot more hopeful job not done.

    > But that gives an error and as I understand also violates the const principle.
    >
    > But is there no way to call a non const function from a const function? Here is a basic example:
    >
    > class Test {
    > public:
    >    void testConst() const {
    >      // this is a no go!
    >      testNonConst();


    You may try silly things like:

    const_cast<Test*>(this)->testNonConst();

    But i would prefer to run fast. There is good life elsewhere waiting
    for you.

    >    }
    >    void testNonConst() {
    >    }
    >
    > private:
    >
    > };
    >
    > int main(){
    >    Test t;
    >    t.testConst();
    >    return 0;
    > }
    Öö Tiib, May 19, 2010
    #3
  4. none

    James Kanze Guest

    On May 19, 7:52 pm, Victor Bazarov <> wrote:
    > On 5/19/2010 2:41 PM, none wrote:
    > > I have extended a library class 'LibraryBase' which I cannot
    > > modify. 'LibraryBase' contains a const function
    > > 'ConstFunction'. In my subclass 'LibrarySub' I am overriding
    > > 'ConstFunction'. But in this function I now need to call
    > > a non const function.


    > > But that gives an error and as I understand also violates
    > > the const principle.


    > > But is there no way to call a non const function from a const function?
    > > Here is a basic example:


    > > class Test {
    > > public:
    > > void testConst() const {
    > > // this is a no go!
    > > testNonConst();
    > > }
    > > void testNonConst() {
    > > }


    > > private:
    > > };


    > > int main(){
    > > Test t;
    > > t.testConst();
    > > return 0;
    > > }


    > The only legal way is to do a const_cast on 'this'. And only
    > do it if you know that your object is non-const, which you
    > probably can't know since the system is going to be calling
    > your 'ConstFunction'...


    There's no problem calling a non-const function on a const
    object, as long as the function doesn't actually modify the
    object in any way.

    > Why do you need to call the non-const function?


    Probably because the class in question isn't const-correct. He
    says he can't modify it, which means that he probably got it
    from somewhere else, where they didn't worry about const.

    > The whole point in having the 'const' in a function is to
    > promise that the state of the object is not going to change
    > during the call. You're violating that promise. Could it be
    > you're overriding a wrong function?


    He's only violating the promess if the base class function
    actually does modify something. The original poster, you and
    I know enough to make this promess explicit with the const
    keyword, but there are doubtlessly still a lot of people around
    who don't know that---including, perhaps, the author of the
    original class.

    --
    James Kanze
    James Kanze, May 20, 2010
    #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. Yuming Ma
    Replies:
    5
    Views:
    874
    wogston
    Dec 8, 2003
  2. David Scarlett
    Replies:
    3
    Views:
    817
    mlimber
    Feb 7, 2006
  3. Fabian Wein
    Replies:
    8
    Views:
    434
    terminator
    Jul 26, 2007
  4. Javier
    Replies:
    2
    Views:
    542
    James Kanze
    Sep 4, 2007
  5. fungus
    Replies:
    13
    Views:
    870
    fungus
    Oct 31, 2008
Loading...

Share This Page