问题描述
我有一个类,我们称之为class myClass
class myClass {
/ /某些数学运算
myClass get_difference(myClass& b)
{
print_operation(* this,b);
do_something else
return ...
}
myClass get_sum(myClass& b)
$ b $ //伪代码
void print_operation(const myClass * a,const myClass& b)
{
if function == get_sum
print a<< plus<< b;
if function == get_difference
print a<< 减号<< b;
}
//超载cout以及
};
假设我打电话给以下人员:
myClass anObject(1,2);
myClass anotherObject(3,4);
anObject.get_sum(anotherObject);
anObject.get_difference(anotherObject);
get_sum / get_difference会调用 print_operation ,但我希望能够确定调用者,以便使用不同的输出格式。
天真的方法:使用开关盒
添加一个名为 ID。给每个函数(调用者)一个id,并在print_operation中使用switch-case语句。
然而,还有其他选择吗?更优雅的解决方案?
谢谢。
$ c> virtual const std :: string& getFormatted()const 在调用者中?
如果格式是两个参数 ,你将不得不创建某种组合表来查找你的格式。
如果格式只是每个参数打印长度的函数( )可以使用 virtual size_t getFormatLength()const
。
$ b 注意:print_operation()除了它有一个getFormatted()函数外,它不知道关于调用者的任何信息,但是调用者可以根据op的值来设置自己的格式。
正如Andrew Marshall在他上面的评论中回答的那样,OOP /封装的一部分是,你不应该知道任何关于调用者。
Polymorphism,done right,应该尝试封装来自调用者的实现细节。
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
{
//打开op的值或op的长度等等...
返回std :: string(这个,根据A类specs和op格式化);
}
};
class B:public myClass
{
public:
virtual std :: string getFormatted(const std :: string& op)const
{
//开启op的值或op的长度等等...
返回std :: string(这个,根据B类规范和操作符来格式化);
}
};
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);
}
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-caseAdd 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.
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 );
}
这篇关于我们可以在C ++的成员函数中找到调用者吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!