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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:59:28+00:00 2026-05-25T09:59:28+00:00

There probably is a fairly simple and straight-forward answer for this, but for some

  • 0

There probably is a fairly simple and straight-forward answer for this, but for some reason I can’t see it.
I need to restrict calling methods from a class only to some methods implemented by derived classes of some interface.

Say I have

class A{
    public:
        static void foo();
};

class myInterface{
    public:
        virtual void onlyCallFooFromHere() = 0;
}

class myImplementation : public myInterface{
    public:
        virtual void onlyCallFooFromHere()
        {
            A::foo(); //this should work
        }
        void otherFoo()
        {
            A::foo(); //i want to get a compilation error here
        }

}

So I should be able to call A::foo only from the method onlyCallFooFromHere()

Is there a way to achieve this? I’m open to any suggestions, including changing the class design.

EDIT:

So… I feel there’s a need to further explain the issue. I have a utility class which interacts with a database (mainly updates records) – class A.
In my interface (which represents a basic database objects) I have the virtual function updateRecord() from which I call methods from the db utility class. I want to enforce updating the database only in the updateRecord() function of all extending classes and nowhere else. I don’t believe this to be a bad design choice, even if not possible. However, if indeed not possible, I would appreciate a different solution.

  • 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-25T09:59:29+00:00Added an answer on May 25, 2026 at 9:59 am

    [Disclaimer: this solution will stop Murphy, not Macchiavelli.]

    How about:

    class DatabaseQueryInterface {
    public:
      ~virtual DatabseQueryInterface() = 0;
      virtual Query compileQuery() const = 0; // or whatever
      virtual ResultSet runQuery(const Query&) const = 0; // etc
    };
    
    class DatabaseUpdateInterface : public DatabaseQueryInterface {
    public:
       virtual Update compileUpdate() const = 0; // whatever
    };
    
    class DatabaseObject {
    public:
      virtual ~DatabaseObject() = 0;
    protected:
      virtual void queryRecord(const DatabaseQueryInterface& interface) = 0;
      virtual void updateRecord(const DatabaseUpdateInterface& interface) = 0;
    };
    
    class SomeConcreteDatabaseObject : public DatabaseObject {
      protected:
         virtual void updateRecord(const DatabaseUpdateInterface& interface) {
            // gets to use interface->compileUpdate()
         }
    
         virtual void queryRecord(const DatabaseQueryInterface& interface) {
            // only gets query methods, no updates
         }
    };
    

    So the basic idea is that your DatabaseObject base class squirrels away a private Query object and a private Update object and when it comes time to call the protected members of the subclass it hands off the Update interface to the updateRecord() method, and the Query interface to the queryRecord() method.

    That way the natural thing for the subclasses is to use the object they are passed to talk to the database. Of course they can always resort to dirty tricks to store away a passed-in Update object and try to use it later from a query method, but frankly if they go to such lengths, they’re on their own.

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

Sidebar

Related Questions

There is probably is simple fix for this but I currently have code similar
I'm using .Net 2.0 and this is driving me crazy but there's probably some
I feel like the answer to this question is probably very simple, but I'm
This is probably fairly straightforward but i can't seem to find a reasonable explanation
I'm new with Objective-C, so there probably is a simple solution to this. I
I'm new to the OOP paradigm, so there's probably a simple explanation for this
I was searching Google for a page offering some simple OpenMp algorithms. Probably there
This is probably going to be a really simple explanation, but I'm going to
This is probably a fairly simple thing to do - I have a site
This is probably a fairly easy question, but I'm new to JavaScript and jquery....

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.