Thread: Class functions can't use const =/

  1. #1
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640

    Class functions can't use const =/

    For a project due tonight ;-p. The project is done, everything works, but I was going back through the code and saw a few of my node class functions don't accept const input when they should be (for good programming, right?). So I added a few consts in, and I'm getting an error that confuzes me (dealing with converting a const to a non-const). I can't see what needs changed for it to work.

    Relevant code:
    Code:
    void setLR(const Node<T> *l, const Node<T> *r) { setLeft(l); setRight(r); }
    void setLeft(const Node<T> *l) { left = l; }  //the error is on this line
    void setRight(const Node<T> *r) { right = r; }
    Error:
    Code:
    error C2440: '=' : cannot convert from 'const Node<T> *' to 'Node<T> *'
            with
            [
                T=tool
            ]
            and
            [
                T=tool
            ]
            Conversion loses qualifiers
    The node class is templated and for the project I'm using, I have a small tool class I use it with. Any ideas what the problem is?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You can't pass const nodes to the function because it attempts to store them internally as non-const. This makes sense when you consider that at a later point, something might actually modify those nodes. Therefore, pass non-const nodes and you'll be fine. Linked list nodes are non-const by nature anyway - if you don't need to modify the list, what's the point? The strength of the linked list is fast insertion and removal.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Quote Originally Posted by CornedBee
    ...
    Right, but shouldn't they be able to accept const nodes? Because the function itself does not change the node passed to it, just *this.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    But 'left' is of type 'Node *', and you can't assign a pointer-to-const (like 'l') to a pointer-to-non-const.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Ah, thanks. I guess that makes sense. But how would you do it so that the pointer doesn't change what it's pointing to? That's what I was trying to do (I believe).
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    If Node is a template, make the template type const. The Node itself can't be const in order to be included in the list.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Ok, I think I just did it:

    Code:
    void setLeft(Node<T> *const l) { left = l; }
    I just did that for all of the above functions, and I'm pretty sure it does that (makes it unable to change where the pointer points too). Put const after the * and it does that, right? (The program still works as normal, and assuming that code gets called on all inserts, it does that).
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    You could also try const_cast<...>(...), but that's cheating the prototype, or build a new node with the value stored in the pointer.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by neandrake
    Ok, I think I just did it:

    Code:
    void setLeft(Node<T> *const l) { left = l; }
    I just did that for all of the above functions, and I'm pretty sure it does that (makes it unable to change where the pointer points too). Put const after the * and it does that, right? (The program still works as normal, and assuming that code gets called on all inserts, it does that).
    Yes, it prevents the pointer passed as argument from being changed. Quite useless, if you ask me. It's just a copy. It's like
    void func(const int arg);
    Fine, so arg can't be changed inside func. However, that is hardly important for the caller because arg is a copy of the passed argument anyway.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Well, would it be possible to pass as a reference also?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    A reference to the pointer? Yes, but it wouldn't make sense. A reference to the node? Yes again, but a pointer makes more sense.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Function template has already been defined
    By Elysia in forum C++ Programming
    Replies: 19
    Last Post: 04-14-2009, 10:17 AM
  3. declaring and using functions in a class
    By quiet_forever in forum C++ Programming
    Replies: 1
    Last Post: 09-16-2007, 02:22 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  5. Problems: Operator overloading.
    By Dual-Catfish in forum C++ Programming
    Replies: 17
    Last Post: 06-18-2002, 06:38 PM