Passing, AND modifying object values (passing const... discards qualifiers error)

This is a discussion on Passing, AND modifying object values (passing const... discards qualifiers error) within the C++ Programming forums, part of the General Programming Boards category; So I am trying to pass an object to a function and access its methods, some of which modify its ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    35

    Passing, AND modifying object values (passing const... discards qualifiers error)

    So I am trying to pass an object to a function and access its methods, some of which modify its values. However, I haven't done this before, and I was following this example

    Use & to get object address : object pointer..«..Class..«..C++ Tutorial

    but since I keep getting

    passing `...' as `this' argument of `...' discards qualifiers

    I realize I am not supposed to be using "const", since that Object is not remaining constant. (vs, the example where they dont actually change the values)

    What should a function be, which is receiving an object in order to be modified

    If any examples could be provided or a link to another tutorial, that would help me out A LOT, thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    What exactly is the code that you tried?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    Code:
    const Target *  const animateTarget( const Target *  const tar){
    
    ...
    tar->setValue(something)
    ....
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    Well, the code example in the tutorial that you linked to compiles for me without even a warning. So, you should post the smallest and simplest example that demonstrates the problem, not a fragment that could be correct, or could be wrong, depending on what you don't show.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quite obviously you shouldn't insist that the object is const if you want to modify it (by calling non-const methods).

    Code:
    const Target *  const animateTarget( Target *  const tar){
        ...
        tar->setValue(something)
        ....
    }
    If the intention was really not to modify it, then you shouldn't be calling setValue() (or that method should be marked "const", which is hard to believe it is).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    @laserlight: I don't believe for one second you didn't immediately know what the problem was.
    Actually, I usually refuse to answer badly formulated questions as well, but I consider this one to be okay. I mean, even from his first post you can easily tell he tried to call a non-const function on a const object.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Pointing out what the problem is as opposed to where the problem is sometimes just isn't helpful.
    Pointing out where and why is even more helpful.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

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