Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9247125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:39:16+00:00 2026-06-18T09:39:16+00:00

What’s the best practice for returning a pointer-to-non- const from a function, where that

  • 0

What’s the best practice for returning a pointer-to-non-const from a function, where that pointer was obtained by modifying a (non-const) pointer-to-const? Like this:

NODE *top_level(const NODE *input)
{
  while (input->parent != nullptr)
    input = input->parent;  // NODE::parent is (non-const) NODE*

  return input;  // Compile failure: 
                 // Cannot convert from 'const NODE *' to 'NODE *'
}

I could const_cast the const away on return, which seems fine, but is there a better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:39:18+00:00Added an answer on June 18, 2026 at 9:39 am

    Best practice, at least in the standard library, is to provide const and non-const overloads. E.g. std::strchr is declared in <cstring> as

    char *strchr(char *s, int c);
    char const *strchr(char const *s, int c);
    

    In a similar vein, functions like std::map<T>::find have overloads such as

    iterator find(const Key& key);
    const_iterator find(const Key& key) const;
    

    Note that that there’s no const qualifier on the first version, even though find itself has no reason to modify the map.(*) The point is that you get something out of find that can be used to modify the map, so by a kind of “transitivity of mutability”, find cannot be const. The same situation applies in your problem, I think.

    Alternatively, you could use a const_cast, but to me, that would feel like breaking a promise.

    The funny thing about this situation is that, if you can guarantee that your function is never called on the top item of the tree (or whatever the input is), then there’s no need for casts or overloads:

    struct node {
        node *parent;
    };
    
    node *top(node const *n)
    {
        node *p = n->parent;
        while (p->parent != 0)
            p = p->parent;
        return p;
    }
    

    compiles without any warnings.

    (*) If std::map were implemented as a splay tree, find would have to modify it, but I don’t think splay trees are allowed by the standard because of complexity guarantees.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've got a string that has curly quotes in it. I'd like to replace
I know there's a lot of other questions out there that deal with this
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
I am using jsonparser to parse data and images obtained from json response. When
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.