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 1073593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:00:42+00:00 2026-05-16T21:00:42+00:00

In C++, is a const method returning a pointer to a non-const object considered

  • 0

In C++, is a const method returning a pointer to a non-const object considered bad practice? For example. consider the following methods:

// Makes perfect sense
bool isActive() const { return m_isActive; }

// Makes perfect sense, both consts ensure nothing is modified
const SomeObject* someObject() const { return m_someObject; }

// Makes perfect sense, no const because SomeObject is mutable,
// thus MyClass is indirectly mutable
SomeObject* someObject() { return m_someObject; }

// Makes no sense, if you're just returning a pointer to a const object,
// then the method should clearly be const since nothing can be modified
const SomeObject* someObject() { return m_someObject; }

The last combination puzzles me: is the following considered bad practice?

SomeObject* someObject() const { return m_someObject; }

Because the class that method returns is not const, but the method is, it is somewhat pointless since you could modify SomeObject and thus indirectly modify your class… so is that form considered bad practice? e.g. should you only use one of the following in place of it?

const SomeObject* someObject() const { return m_someObject; }
SomeObject* someObject() { return m_someObject; }

Real-world situation: I’m writing a smartphone game which has a Game class. The Game class contains several methods which return pointers to utility classes like InputManager, AudioManager, etc.

class Game
{
public:
    InputManager* inputManager() const { return m_inputManager; }
    ...

private:
    InputManager* m_inputManager;
    ...
}

Does it make ANY sense at all to make inputManager() const, since the object it returns is not? (and should not be, since the InputManager needs to be mutable for its state to be updated, etc.)

Or should I remove the method’s const modifier to more accurately reflect the method’s behavior? That way if I had a const Game, I couldn’t call inputManager() and then mess with the Game‘s input manager even though the Game was const.

  • 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-05-16T21:00:42+00:00Added an answer on May 16, 2026 at 9:00 pm

    It depends on whether the non-const pointer is pointing to something inside the const object, or whether it is newly allocated memory that is meant to be variable.

    Consider something that returned a copy of a string; it’s fine for the returned copy to be non-const, even if the original string is const. The ownership of the copy is being passed from the method to the calling code; ensuring that the copy is appropriately destructed is now the calling code’s problem (quite possibly a very minor problem).

    However, it would be a very bad idea to return a non-const pointer to something from inside a const object.

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

Sidebar

Related Questions

Consider the following methods static ComponentType & getTypeFor(const type_info &t){ ComponentType * type =
In the following method, public void InspectList(IList<int> values) { if(values != null) { const
I'm adding some lazy initialization logic to a const method, which makes the method
I've got a const method in my class, which cannot be changed to non-const.
I have this virtual method: const string& my_class::to_string() const { string str(this->name + string(
I've written a method that I'd like to declare as const, but the compiler
I wrote two methods with a void type parameter: procedure Method1(const MyVar; size: cardinal);
The following method calculates cost. It access other classes when required. Cloud class holds
Qt's QImage has two methods: uchar* QImage::bits(); const uchar* QImage::bits() const; But how to
I would like to code a method capable of returning a range of iterators

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.