inheritance and const

This is a discussion on inheritance and const within the C++ Programming forums, part of the General Programming Boards category; ok, i have a class that has a const in it and uses it within it's member functions...i want to ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    719

    inheritance and const

    ok, i have a class that has a const in it and uses it within it's member functions...i want to make it non constant but i cannot change the class. the ONLY thing i want different about the class is changing that to a non-constant. i was wondering if i could hack around this using inheritance?
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    113
    you mean make one private member non-const for some reason?
    well i really dont understand your question, but you can use "const_cast", to "convert temporaly" that value or object to non-const

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Given that it is possible to change the constant member variable, is it accessible? Where are you accessing that variable?

    Kuphryn

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Code:
    class foo
    {
         public:
         static const int x = 8;
    };
    int foo::dosomething(){ while(i++ < x); return i; } 
    
    int bar::blah(){
           foo a; 
           foo::x = 6; 
          return a.dosomething(); 
    }
    does that clearify what i want?

    doing return a.dosomething() - 2; will not suffice
    Last edited by misplaced; 10-21-2004 at 12:31 PM.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    This is a bad idea. The const is there for a reason. If you can't change the code in the class, you shouldn't be changing the constant.
    Code:
    class foo
    {
         public:
         static const int x = 8;
    };
    int foo::dosomething(){ while(i++ < x); return i; } 
    
    int bar::blah(){
        foo a; 
        const_cast<int&>(foo::x) = 6; 
        return a.dosomething(); 
    }

  6. #6
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Let me preface by saying that it might depend on your compiler because depending on which you're using, a static const may be treated as a constant (calculated at compile time) and as such, it can not be modified. It can be likened to wanting to do this:
    Code:
    class foo
    {
         public:
      // x compiled into 8
    };
    int foo::dosomething(){ while(i++ < 8); return i; } 
    
    int bar::blah(){
           foo a; 
           8 = 6; 
          return a.dosomething(); 
    }
    But generally speaking, you won't be able to do what you're asking.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    i know it's a bad idea. i hate to give away any clues to my ultra top secret algorthm which will anihilate everyone in the connect four contest, but i've found an ingenius way to use the checkWin() function if only i could get toWin's value to change. so i was wondering if somehow i could inherit Board and use polymorphism or something to be able to change the value of toWin.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  8. #8
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    I think you're going to have to do more work than that.

    Why don't you try duplicating the checkWin code but without the const on the toWin part?

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    there's size constraints isn't there?

    i have more code in mind than just that, but it would be very helpful
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  10. #10
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    No, I don't think there is a size constraint for the code in that contest (there is a size constraint for Quzah's sigmaze contest).

    Besides, I don't think Sang-drax would like you cheating the interface like that.

    I'd suggest doing it the right way. The code for checking for a win is right there, just copy it and modify it to suit your needs.

    In fact, the only private or protected data used in that function is lastX and lastY, which you should know since you made the move.

    If you must, ask Sang-drax if the Board code can be changed to allow a variable toWin value.
    Last edited by jlou; 10-21-2004 at 01:07 PM.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    i was just trying to whip something up because i just found the contest thread yesterday and there's only 10 days left..

    i figured i might as well, cheat...ermm, 'use the function creatively' because i'm going to lose anyway
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  12. #12
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Understood. However, I always thought one of the reasons to do those contests was to test your abilities and try to improve your skills. The connect four contest is nice in that it doesn't restrict code size in any way, so you can try to make the best possible code. If you don't expect to win, then writing better code than usual or trying to learn something are admirable goals.

  13. #13
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by misplaced
    i've found an ingenius way to use the checkWin() function if only i could get toWin's value to change.
    Creative, but it will get you disqualified
    Not because of cheating, but because your code yields undefined behaviour.
    Quote Originally Posted by jlou
    No, I don't think there is a size constraint for the code in that contest (there is a size constraint for Quzah's sigmaze contest).

    Besides, I don't think Sang-drax would like you cheating the interface like that.
    There is no code size constraint, only a somewhat vague execution time constraint.
    Last edited by Sang-drax; 10-21-2004 at 05:40 PM. Reason: Apparently jlou != misplaced
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  14. #14
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote Originally Posted by Sang-drax
    Quote Originally Posted by jlou
    i've found an ingenius way to use the checkWin() function if only i could get toWin's value to change.
    Creative, but it will get you disqualified
    Not because of cheating, but because your code yields undefined behaviour.
    Hey now!!! That wasn't my quote!

    Don't sully my reputation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 04:03 PM
  2. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  3. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  4. Need Help Please
    By YouShallNotPass in forum C++ Programming
    Replies: 3
    Last Post: 08-22-2006, 12:22 PM
  5. Need Help with Inheritance assignment
    By mejv3 in forum C++ Programming
    Replies: 6
    Last Post: 11-08-2005, 12:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21