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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:24:32+00:00 2026-05-11T03:24:32+00:00

While designing an interface for a class I normally get caught in two minds

  • 0

While designing an interface for a class I normally get caught in two minds whether should I provide member functions which can be calculated / derived by using combinations of other member functions. For example:

class DocContainer {  public:    Doc* getDoc(int index) const;    bool isDocSelected(Doc*) const;    int getDocCount() const;    //Should this method be here???   //This method returns the selected documents in the contrainer (in selectedDocs_out)   void getSelectedDocs(std::vector<Doc*>& selectedDocs_out) const; }; 

Should I provide this as a class member function or probably a namespace where I can define this method? Which one is preferred?

  • 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. 2026-05-11T03:24:33+00:00Added an answer on May 11, 2026 at 3:24 am

    In general, you should probably prefer free functions. Think about it from an OOP perspective.

    If the function does not need access to any private members, then why should it be given access to them? That’s not good for encapsulation. It means more code that may potentially fail when the internals of the class is modified.

    It also limits the possible amount of code reuse.

    If you wrote the function as something like this:

    template <typename T> bool getSelectedDocs(T& container, std::vector<Doc*>&); 

    Then the same implementation of getSelectedDocs will work for any class that exposes the required functions, not just your DocContainer.

    Of course, if you don’t like templates, an interface could be used, and then it’d still work for any class that implemented this interface.

    On the other hand, if it is a member function, then it’ll only work for this particular class (and possibly derived classes).

    The C++ standard library follows the same approach. Consider std::find, for example, which is made a free function for this precise reason. It doesn’t need to know the internals of the class it’s searching in. It just needs some implementation that fulfills its requirements. Which means that the same find() implementation can work on any container, in the standard library or elsewhere.

    Scott Meyers argues for the same thing.

    If you don’t like it cluttering up your main namespace, you can of course put it into a separate namespace with functionality for this particular class.

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

Sidebar

Ask A Question

Stats

  • Questions 73k
  • Answers 73k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Does your application need to recycle? All of the recycle… May 11, 2026 at 2:05 pm
  • added an answer Parameterized Queries May 11, 2026 at 2:05 pm
  • added an answer Use the stopImmediatePropagation function of the jQuery event object. Keeps… May 11, 2026 at 2:05 pm

Related Questions

While designing a table my colleague here says that I should avoid identity column
While designing my site I am constantly faced with the issue of whether its
What are the topics we should pay attention while designing web site for Internet
While the C# spec does include a pre-processor and basic directives (#define, #if, etc),
I am part of a team creating a web application using PHP and MySQL.
I have read a lot of books about Microsoft Dot Net and I have
If I set a Friend-level scope on a setter, like this... Public Class MyClass
I'm writing an application that is basically just a preferences dialog, much like the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.