Calling non-const member functions from const member functions ...


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Calling non-const member functions from const member functions ...

Hybrid View

  1. #1
    Join Date
    Jul 2005
    Posts
    22

    Calling non-const member functions from const member functions ...

    I guess this is kind of a 'design issue' question. Let's say you have two functions in a class:
    Code:
    class A{
    
    public:
    const void f() const;
    int& g();
    
    private:
    // stuff ...
    
    };
    where g can only modifies A objects through the return type, i.e.,
    Code:
    int i = a.g();
    does not modify the object a. I'd like to call this function from A::f() to get the information and not in a way that modifies the object, i.e.,
    Code:
    a.g()++;
    This is possible but I'm getting warnings about removing the const-ness of the function A::f() ... which I'd rather not have (or is this fine to just not worry about in this case?). So I guess there is something wrong w/ the design of these functions within the class? Is there a better way to design this than just dropping the const-ness from the function A::f()?

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    Generally speaking, ignoreing warnings is never a good idea. Remember that what your compiler considers a warning is an error for another compiler. In this particualr case, the warning is really serious.

    I'm not sure I understand what you're trying do: a function that returns a non const reference to an internal member of its class is a dangerous one. If you want to make it secure, either return a copy of the interger i.e.,

    int g();

    or return a const int& and declare that member as const:

    cont int & g() const;

    If your const member has to call a non-const member, and you know that the non-const member is safe (i.e., it doesn't modifty its object), either delcare it as const (which is what it should be in teh first place) or overload it, so your class has two versions of the same function, const and non-const:

    int & g();
    const int & g() const;

    BTW, which compiler are you using? I'm surprised that it accepts a function that has a return value 'const void'.
    Danny Kalev

  3. #3
    Join Date
    Jul 2005
    Posts
    22

    Thumbs up kudos again Danny ...

    Generally speaking, ignoreing warnings is never a good idea. Remember that what your compiler considers a warning is an error for another compiler.
    That's what I figured and the warning is there because I'm doing something not quite on the level ...

    a function that returns a non const reference to an internal member of its class is a dangerous one.
    So then you'd say this is a bad design feature of the class, period? I realize it breaks encapsilation but it also allows for some 'slickness' in the actual code; I guess you're saying that most degrees of 'slickness' and easy of end use are outweighted by good OO design?

    so your class has two versions of the same function, const and non-const:
    Code:
     int& g();
    const int& g() const;
    So if the function calling g() is declared const then the compiler will be able to pick out the correct g() w/o complaining, right? This type of polymorphism is legal because the const post-fix is part of the function's signature because I know you can't overload on just the return type of a function, right?

    BTW, which compiler are you using? I'm surprised that it accepts a function that has a return value 'const void'.
    Sorry. That was a side-effect of my Franken-code for the post from the project I'm actually working on; should have just been void return type. However, I think it's g++ 3.3 ...

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    Quote Originally Posted by bpw1621
    That's what I figured and the warning is there because I'm doing something not quite on the level ...

    So then you'd say this is a bad design feature of the class, period? I realize it breaks encapsilation but it also allows for some 'slickness' in the actual code; I guess you're saying that most degrees of 'slickness' and easy of end use are outweighted by good OO design?
    Good OOD doesn't allow external clients to poke into the objetc's private data members, but there are exceptions to this rule.
    When you return a reference to non-const, the client might alter the object's member behind your back, or it could attempt to use that reference when the object has already been destroyed. I'm not saying that it's always a bad design choice to do so -- the overloaded operator [] in STL containers does exactly the same thing. However, in this case, it's a plausible design choice since vector imitates a built-in array. So in some cases, you would return a reference to a data member, allowing clients to alter that dara member directly, whereas in other cases you'd use member functions to alter the data member, e.g., setValue().

    Quote Originally Posted by bpw1621
    So if the function calling g() is declared const then the compiler will be able to pick out the correct g() w/o complaining, right? This type of polymorphism is legal because the const post-fix is part of the function's signature because I know you can't overload on just the return type of a function, right?
    Yes, a const member function and a similar member function that isn't const qualified have different signatures:

    Code:
    struct A{
     int func()const
     int func(); //has a different signature from previous func
    };
    The selection is rather intuitive: the compiler will pick the const function when called for a const object, and the non-const one otherwise. In fact, every container that has an overloaded [] defines two versions thereof: a const and non-const one.

    Quote Originally Posted by bpw1621
    Sorry. That was a side-effect of my Franken-code for the post from the project I'm actually working on; should have just been void return type. However, I think it's g++ 3.3 ...
    So you mean that g++ 3.3. accepts const void ?
    Last edited by Danny; 08-14-2005 at 07:56 AM.
    Danny Kalev

  5. #5
    Join Date
    Jul 2005
    Posts
    22
    Sorry ... I meant I was using g++ 3.3 but the code snipet I put in my post was the result of bad copying/simplifying from my actual project ...

    Thanks for the post ... I think I already new that I was sacrificing easy of (my) use to good OOD ...

    Been looking over a bunch of your "10 minute solutions" ... some really good/helpful stuff ... since they're 10 minute solutions though some of them are a little short by design ... they've been great though and don't be surprised if I bring some questions up on one of them soon ...

    Being the tangential thinker I am, I also recently came across the TopCoder problem sets that are on the DevX.com main site ... now besides being good practice, is there any point to doing the problems? I didn't quite get whether you submitted them and/or were judged against anyone else? If not, are there any problem set sites like that? Thanks ...

  6. #6
    Join Date
    Nov 2003
    Posts
    4,118
    Quote Originally Posted by bpw1621
    Being the tangential thinker I am, I also recently came across the TopCoder problem sets that are on the DevX.com main site ... now besides being good practice, is there any point to doing the problems? I didn't quite get whether you submitted them and/or were judged against anyone else? If not, are there any problem set sites like that? Thanks ...
    Solution to the Top Coder challenges can be posted on this forum. There is no pecuniary bounty for solving them but my generous editors are willing to send C++ books (very good books, btw!) as a reward. It's not exactly a competition either. Think of it as a healthy C++ exercising. So, if you have a solution to a TopCoder challenge and feel like posting it here, you're most welcome!
    Danny Kalev

Similar Threads

  1. Screen Capture Bug
    By Ranger in forum VB Classic
    Replies: 3
    Last Post: 05-29-2006, 10:43 PM
  2. Screen Capture Bug
    By Ranger in forum VB Classic
    Replies: 0
    Last Post: 12-09-2000, 01:21 PM
  3. How do I detect an FTP timeout?
    By Julian Milano in forum VB Classic
    Replies: 2
    Last Post: 08-11-2000, 01:11 PM
  4. How do I detect an FTP timeout?
    By Julian Milano in forum VB Classic
    Replies: 0
    Last Post: 08-10-2000, 10:16 PM
  5. Trying to print a PDF File from VB
    By Kunal Sharma in forum VB Classic
    Replies: 2
    Last Post: 04-25-2000, 04:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center