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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:02:20+00:00 2026-05-27T17:02:20+00:00

I have a class, and let’s called it class myClass class myClass{ // some

  • 0

I have a class, and let’s called it class myClass

class myClass{

  // some math operations
  myClass get_difference(myClass &b)
  {
       print_operation(*this, b);
       do_something else
       return...
  }
  myClass get_sum(myClass &b)

// pseudocode 
void print_operation(const myClass *a, const myClass &b)
{
     if function == get_sum
         print a << "plus" << b;
     if function == get_difference
         print a << "minus" << b;
}

  // overload cout as well
};

Suppose I called the following

myClass anObject(1,2);
myClass anotherObject(3,4);

anObject.get_sum(anotherObject);
anObject.get_difference(anotherObject);

get_sum / get_difference will call print_operation, but I want to be able to determine the caller so a different output format is used.

Naive approach: Use switch-case
Add a new parameter called “id”. Give each function (the caller) an id, and use switch-case statements in print_operation.

However, is there an alternative? A more elegant solution?

Thanks.

  • 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-27T17:02:21+00:00Added an answer on May 27, 2026 at 5:02 pm

    Have you considered adding a virtual const std::string& getFormatted() const in the caller?

    If the format will be a function of both arguments to your operator, you would have to create some kind of table of combinations to look up your format.

    If the format is only a function of the length of the printing of each argument (much simpler), you could use virtual size_t getFormatLength() const.

    Note: print_operation() doesn’t know anything about the caller, except that it has a getFormatted() function, yet the caller gets to format itself based on the value of op.

    This is OOP/polymorphism at work.

    As Andrew Marshall answered in his comment above, part of OOP/encapsulation is, you should not know anything about the implementation of the caller.

    Polymorphism, done right, should try to encapsulate the implementation details away from the caller.

    class myClass
    {
      public:
        virtual std::string getFormatted( const std::string& op ) const = 0;
    };
    
    class A : public myClass
    {
      public:
        virtual std::string getFormatted( const std::string& op ) const
        {
          // switch on the value of op or the length of op, etc...
    
          return std::string( "this, formatted according to class A specs and op" );
        }
    };
    
    class B : public myClass
    {
      public:
        virtual std::string getFormatted( const std::string& op ) const
        {
          // switch on the value of op or the length of op, etc...
    
          return std::string( "this, formatted according to class B specs and op" );
        }
    };
    
    void print_operation(const myClass &a, const myClass &b )
    {
      std::string op;
    
      if ( function == get_sum ) {
        op = "plus";
      } else if ( function == get_difference ) {
        op = "minus"; 
      }
      std::cout << a.getFormatted( op ) << op << b.getFormatted( op );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let say I have abstract class called: Tenant and Customer. The tenant in this
In Visual Studio 2008 (C++) I have a class destructor (Let's call this class
Let's say that I have class , that uses some functionality of dict .
I have a console application. A class (let's say Worker) does some work in
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have: class myClass std::list<myClass> myList where myClass does not define the
Let's say I have class that acts as a smart pointer and releases some
I have this PHP class class myclass{ function name($val){ echo this is name method
let's say if you have class doing something on some other thread. And has
I have a class that represents some business object, such as this: Public Class

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.