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

  • Home
  • SEARCH
  • 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 3599520
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:22:40+00:00 2026-05-18T20:22:40+00:00

i have my own class SomeObject with a few members. now, i have another

  • 0

i have my own class “SomeObject” with a few members.

now, i have another class “WorkingClass” containg this object as privat member.

My Question is: i want to create a Getter for the “SomeObject”, but i don’t want anyone to modify it.

which way is better, 1 or 2?

class WorkingClass
{
private:
    SomeObject sObj;

public:
    //... 1)
    const SomeObject &const GetSomeObject()
    {
        return mousePosition;
    }

    //... 2)
    const SomeObject *const GetSomeObject()
    {
        return &mouseMovement;
    }
}

i know you can always cast away const, but still, i’m just trying to get my code clean and fail-safe

EDIT:

then i have a further question. when i have a smart-pointer member and use it a lot inside the class, and then suddenly want someone to have acces to read some values but nothing more, would this be a good solution or is that verbose again?

class X
{
private:
    boost::shared_ptr<SomeObject> sob

public:
    const const & GetSomeObject()
    {
        return *sob.get();
    }
}

and how about returning a “const boost::shared_ptr<…> GetX()” ? it may not be really neccessary, but still not useless, as the compiler would forbid GetX().reset(..) in such a case, and without the const boost::… declaration this useless operation would be permitted. or am i wrong?

  • 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-18T20:22:41+00:00Added an answer on May 18, 2026 at 8:22 pm

    Neither is good:

    • const SomeObject &const is ill-formed. You cannot const-qualify a reference. (You can, of course, qualify the referent type.)

    • const SomeObject *const is unnecessarily verbose. A function call expression o.GetSomeObject() is an rvalue expression and only class-type rvalues can be const-qualified. You may as well just say const SomeObject*. (const SomeObject *const can actually lead to issues with template instantiation, though such issues are rare.)

    As for whether you choose to return by pointer or by reference, it depends on how you are using the return value. Both can make sense in different circumstances. Regardless, you want to return a reference or pointer to a const object, not a const reference or a const pointer:

    const SomeObject& GetSomeObject() { ... }
    const SomeObject* GetSomeObject() { ... }
    

    Usually, returning a reference is preferable.

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

Sidebar

Related Questions

i have my own class SomeObject with a few members. now, i have another
I have create my own NSOpenGLView class, right now the data that i want
I have own project and i would like add for this class same as
I have created my own Attached Property like this: public static class LabelExtension {
I have a class that defines its own enum like this: public class Test
Simple question, I have code like this: class Context[A] { def t: A }
I have my own generic class which goes like this: C1<T> { T value;
I have my own class based in QWidget. I put this widget in QMainWindow
I have created a delegate object implementing the UITextFieldDelegate in its own class named
I'm trying to hide window after its startup. I have own window-class which is

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.