我们可以在C ++中的成员函数中找到调用者吗?

时间:2011-12-19 03:00:59

标签: c++ function

我有一个类,我们称之为类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
};

假设我调用了以下内容

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

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

get_sum / get_difference将调用 print_operation ,但我希望能够确定调用者,以便使用不同的输出格式。

天真的方法:使用switch-case 添加一个名为“id”的新参数。给每个函数(调用者)一个id,并在print_operation中使用switch-case语句。

然而,还有其他选择吗?更优雅的解决方案?

感谢。

3 个答案:

答案 0 :(得分:2)

Cpp函数不知道谁是调用者,除非你破解堆栈,这有点复杂。因此,一般来说,您必须将信息(在函数参数,模板参数,数据成员..)中传递给print_operation,以告诉它要打印的操作。

所以答案是没有更优雅的解决方案。

答案 1 :(得分:2)

您是否考虑在来电者中添加virtual const std::string& getFormatted() const

如果格式是操作员的两个参数的函数,则必须创建某种组合表来查找格式。

如果格式只是每个参数打印长度的函数(更简单),则可以使用virtual size_t getFormatLength() const

注意:print_operation()对调用者一无所知,只是它有一个getFormatted()函数,但调用者根据op的值来格式化自己。

这是OOP /多态性在起作用。

正如Andrew Marshall在上面的评论中所说,OOP /封装的一部分是,你不应该对调用者的实现有任何了解。

多态性,做得正确,应该尝试将实现细节封装在调用者之外。

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 );
}

答案 2 :(得分:1)

我不认为问题归结为知道呼叫者是谁。听起来你真的想要定义不同的格式化数据的方法,并且可能有不同的调用者需要不同的格式化程序。

从.NET中吸取教训,您可以考虑使用格式字符串来定义如何输出数据的设计,例如IFormattable.ToString。在该示例中,格式字符串用于区分不同的输出格式。在您的情况下,您可以使用整数,枚举或任何适当的值来定义它。

相关问题