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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:52:20+00:00 2026-05-18T06:52:20+00:00

I have a C++ module that needs to get information from other classes, without

  • 0

I have a C++ module that needs to get information from other classes, without knowing those classes. The obvious approach is to use interfaces.

Let me give you an example. Suppose I have a library that manages books, and all books have their own characteristics and functionalities, and to allow the library to get a characteristic from a book or execute a functionaity, the book needs to implement an interface. Like this:

class Library
   {
   public:
      void addBook(IBook &book);
   };

class IBook
   {
   public:
      string getAuthor()    = 0;
      string getTitle()     = 0;
      string getISBNCode()  = 0;
      size_t getNofPages()  = 0;
      size_t getNofImages() = 0;
      double getPrice()     = 0;
      void   printBook()    = 0;
      void   convertToPdf() = 0;
   };

Unfortunately, it does not make sense to implement all these methods for all kinds of books.

  • Some books don’t have images (so I don’t want to implement getNofImages())
  • Some books don’t have an ISBN code
  • Some books can’t be bought, so they don’t have a price
  • Some books can’t be printed
  • Some books can’t be converted to PDF

Because I only have 1 interface, I am forced to implement everything for all books and return 0, return “” or do nothing int he implementation if it is irrelevant.

An alternative could be to split these interfaces in many interfaces, like this:

class IBook
   {
   public:
      string getAuthor()    = 0;
      string getTitle()     = 0;
      size_t getNofPages()  = 0;
   };

class IISBNGetter
   {
   public:
      string getISBNCode()  = 0;
   };

class IImagesGetter
   {
   public:
      size_t getNofImages() = 0;
   };

class IBuyable
   {
   public:
      double getPrice()     = 0;
   };

class IPrintable
   {
   public:
      void   printBook()    = 0;
   };

class IConvertible
   {
   public:
      void   convertToPdf() = 0;
   };

Book classes then only need to implement the interfaces they really want to support.

Adding a book to the library then becomes something like this:

bookid = myLibrary->addBook (myBook);
myLibrary->setISBNGetter  (bookid, myBook);
myLibrary->setImageGetter (bookid, myBook);
myLibrary->setBuyable     (bookid, myBook);

The advantage of having different interfaces is that it is clear for the library who supports what, and it never has the risk of calling something that is simply not supported.

However, since every book can have any possible combination of characteristics/functionalities, I end up with lots of interfaces with only 1 method.

Isn’t there a better way to organize the interfaces to obtain something like this?

I was also thinking about using Lambda expressions but behind the screens this is almost the same as having many many interfaces with only 1 method.

Any ideas?

  • 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-18T06:52:21+00:00Added an answer on May 18, 2026 at 6:52 am

    I’d have IBook to implement every method:

    class IBook
       {
       public:
          virtual ~IBook() {}
    
          virtual string getAuthor() { return ""; } // or some other meaningful default value
          virtual string getTitle() { return ""; }
          virtual string getISBNCode() { return ""; }
          virtual size_t getNofPages() { return 0; }
          virtual size_t getNofImages() { return 0; }
          virtual double getPrice() { return .0; }
          virtual void   printBook() {}
          virtual void   convertToPdf() {}
       };
    

    since your alternative is too messy for me, you’d end with lots of confusing interfaces. Otherways, you could check if the Visitor pattern could be applied here.

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

Sidebar

Related Questions

I have an HTTP Module that I use to clean up the JSON returned
I have some MySQL database server information that needs to be shared between a
I have a module that needs to return a reference URI in its payload.
I have module that implements custom content type via NodeAPI hooks ( hook_insert ,
I have a module that is being used to create only a few page
I have a module that reads the StandardError of a process. Everything works fine,
Suppose you have a module that you know is safe. You want to mark
Let's say that I have a module that has a Queue in it. For
I have a war module that is being built and is including a few
I have this module pattern that stores a bunch of vars. I want to

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.