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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:27:47+00:00 2026-06-05T12:27:47+00:00

I have a function that returns a pointer to an object. Like this: class

  • 0

I have a function that returns a pointer to an object. Like this:

class SomeClass
{
public:
    Object* getObject() const { return &obj; };
private:
    Object obj;
}

The problem is that I don’t the calling function to change the returned object, it might call functions that operate on it internally, but don’t change the obj inside SomeClass…
An example might be easier to understand:

-If the function returns a pointer to a const Object const Object* getObject() const the calling function won’t be able to use non-const methods of the object (but I want them to be able).

-If I simply return the pointer, the calling functions might change the object:

Object* pointer = sclass.getObject();
*pointer = Object();

So the Object inside sclass is now a different object (which I don’t want to happen).

Is there an workaround?

  • 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-05T12:27:49+00:00Added an answer on June 5, 2026 at 12:27 pm

    tl;dr version: Return a const Object *.

    If the function returns a pointer to a const Object const Object* getObject() const the calling function won’t be able to use non-const methods of the object (but I want them to be able).

    But that’s the whole point of const-correctness! Any Object method that doesn’t modify the observable state of the object should be declared const. If you’re consistent about this, then your problem should disappear. If you’re not consistent about this, then the whole thing falls apart (which is probably the cause of your problem!).

    Obviously, you can’t do anything about const methods that then subvert things by casting away the const-ness of this, but that’s always a problem.

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

Sidebar

Related Questions

I have need for a function pointer that takes two arguments and returns a
I have a base class that I want to look like this: class B
I have a smart pointer type, and would like to construct an object that
I have an object that has a mathematical function behind it. It seems like
We have a Button that is styled like this: <a class=mini pink button>Arrange Viewing</a>
I have two classes like this (simplified and renamed): class Base { public: virtual
I have a function that returns two values in a list. Both values need
I have a function that returns the request parameters for each request: private function
I have a function that returns log10 values. On converting them to normal numbers,
I have a function that returns a value that can be true if the

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.