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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:25:48+00:00 2026-06-19T01:25:48+00:00

class T {}; class AccessT { public: boost::shared_ptr<const T> getT() const {return m_T;} boost::shared_ptr<T>

  • 0
class T
{};

class AccessT
{
public:
    boost::shared_ptr<const T> getT() const {return m_T;}
    boost::shared_ptr<T>       getT()       {return m_T;}

private:
    boost::shared_ptr<T> m_T;
};

Question> I saw lots of similar codes as above in the legacy project.
I really don’t understand the point of doing so.
Why not just simply provide the following instead:

class T
{};

class AccessTModified
{
public:
    boost::shared_ptr<T> getT() const { return m_T; }

private:
    boost::shared_ptr<T> m_T;
};

The initial argument may be that boost::shared_ptr<const T> getT() const will not let const object modifies the T by accident. If that is the case, is it a practice that all such functions should provide two versions? To me, I feel very tedious!

  • 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-19T01:25:49+00:00Added an answer on June 19, 2026 at 1:25 am

    You are correct: the purpose of boost::shared_ptr<const T> getT() const is to ensure that const objects can’t modify the T by accident.

    In C++, this is known as const correctness and is usually considered to be good C++ design. As you said, it often results in getters having two versions (a const and a non-const version). It can be initially tedious (although it’s not bad once you get used to it), but the results can be quite useful. Const correctness lets you declare functions like

    void DoSomething(const AccessT& item);
    

    that promise not to modify item so that the compiler throws an error if DoSomething does anything that’s declared as possibly modifying item.

    Although const correctness is usually considered to be good C++ design, some developers decide that the overhead of having to declare const and non-const versions of some functions to be more trouble than it’s worth.

    The C++ FAQ has a whole section on const correctness, if you’re interested in more information.

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

Sidebar

Related Questions

Imagine the following situation: class IAlarm : public boost::enable_shared_from_this<IAlarm> { boost::shared_ptr<IAlarm> getThisPointerForIAlarm() { return
i have a class that has this function: typedef boost::shared_ptr<PrimShapeBase> sp_PrimShapeBase; class Control{ public:
Compiler: MSVS 2008 Boost: 1.49 Code: #include <boost/signals2.hpp> #include <boost/thread.hpp> class SigOwner { public:
i am using boost mutex in MessageQueue class as a private member in the
Whats the correct way of implementing this class? //Header #include <boost/shared_ptr.hh> class MyClass {
I have a class declaration in Utils.h: class Utils { private: static boost::mutex outputMutex;
I have a strange error. class INST { public: boost::mutex m_mutex; }; std::vector<INST> m_inst;
How can I access a variable in one public class from another public class
I'm trying to access a static method in a Utils class I created: public
Do I have to lock access to instance members? Example: public class HttpModule :

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.