关于重载运算符的返回类型 [英] about return type of an overloaded operator
问题描述
给定一个类模板Vector<>,我想重载operator +。
但我很难决定返回类型是否应该是
Vector< ; U>或者矢量< V>,如:
模板< typename U,typename V>
Vector< U_or_V> operator +(
const Vector< U>&,
const Vector< V>&);
当然,我希望U_or_V能够是(U()+ V())的类型,但是,
请原谅我有限的知识,我不知道如何实现这个目标。
目前我将U_or_V默认为U.但这有一些微妙的
影响。例如,违反交换法:
Vector< int> vi(0,0,0);
Vector< double> vd(1.1,1.2,1.3);
断言((vi + vd)==(vd + vi));
以上无辜的代码甚至可能无法编译,具体取决于如何声明operator ==
。如果确实如此,那么再次由运营商的详细信息决定==
来决定断言是成功还是失败。理想情况下,
加法表达式应该给出Vector< double>因为(int()+ double
())是双倍的。
任何想法都会受到赞赏!
Ben
>根据如何声明operator ==,上面无辜的代码甚至可能无法编译。如果确实如此,那么再次由运营商==的细节来决定断言是成功还是失败。理想情况下,两个
加法表达式都应该给出Vector< double>因为(int()+ double
())是双倍的。
如果你面临这样的困境,考虑一下C ++是怎样的双重
和int可能会出现这种情况。这有助于保持代码一致
并且您不必每次都解析您的心理设计。 Scott Meyer的
书籍Effective C ++有一些关于重载的见解。试试你的
本地B& N或Borders。
我的0.000002美分!
>
>如果您遇到这样的困境,请考虑C ++的双重和int在这种情况下的行为。这有助于保持代码的一致性
并且您不必每次都解析您的心理设计。 Scott Meyer的书Effective C ++对重载有一些见解。在您当地的B& N或Borders尝试。
感谢您的建议!
至于你的第一个建议,请看我确实努力保持语义与C ++的本机类型一致。这就是我想要的原因:
//伪C ++代码
模板< typename U,typename V>
向量< typeof(U()+ V())> operator +(
const Vector< U>&,
const Vector< V>&);
我的问题是如何以编译器
理解的方式编写typeof(U()+ V())。我考虑过一个特质课,但是专业化
在这种情况下会是无穷无尽的。
我会认真考虑你的第二个建议。
>
你的,
Ben
benben写道:如果您遇到这样的困境,请考虑C ++的双重和/或int在这种情况下的表现。这有助于保持代码的一致性
并且您不必每次都解析您的心理设计。 Scott Meyer的书Effective C ++对重载有一些见解。在您当地的B& N或Borders尝试。
感谢您的建议!
关于您的第一个建议,请看我确实努力保持语义与C ++的本机类型一致。这正是我想要的原因:
//伪C ++代码
模板< typename U,typename V>
Vector< typeof(U()+ V ())> operator +(
const Vector< U>&,
const Vector< V>&);
我的问题是如何写typeof(U()+ V() )以编译器理解的方式。我考虑过一个特质课,但在这种情况下,专业化将是无穷无尽的。
好的,我也是自从过去4年左右开始的初学者!
如何声明一个抽象基类B,其中U和V
派生,在运算符+中你可以有一个工厂设计模式返回
相应的类型。
一旦你返回了类型,你就可以重载==接受
类型为base B的参数因此,它适用于所有情况,并且
也可以进行比较。我认为这应该有效。
表格维基百科这是一个样本工厂模式:
#include< memory>
使用std :: auto_ptr;
class Control {};
class PushControl:public Control {};
class Factory {
public:
//返回基于classKey的Factory子类。每个
//子类都有自己的getControl()实现。
//这将在子类具有
//被声明之后实现。
静态auto_ptr<工厂> getFactory(int classKey);
virtual auto_ptr< Control> getControl()const = 0;
};
类ControlFactory:public Factory {
public:
virtual auto_ptr< Control> getControl()const {
返回auto_ptr< Control>(新的PushControl());
}
};
auto_ptr<工厂> Factory :: getFactory(int classKey){
//在此插入条件逻辑。样本:
开关(classKey){
默认值:
返回auto_ptr<工厂>(新的ControlFactory());
}
}
Given a class template Vector<>, I would like to overload operator +.
But I have a hard time deciding whether the return type should be
Vector<U> or Vector<V>, as in:
template <typename U, typename V>
Vector<U_or_V> operator+ (
const Vector<U>&,
const Vector<V>&);
Naturally, I would like U_or_V to be the type of (U() + V()), but,
pardon my limited knowledge, I have no idea how to achieve that.
Currently I am defaulting U_or_V just to U. But this has some subtle
implications. For example, the breach of the commutative law:
Vector<int> vi(0, 0, 0);
Vector<double> vd(1.1, 1.2, 1.3);
assert((vi + vd) == (vd + vi));
The above innocent code may not even compile depending on how operator==
is declared. If it did, it is again up to the details of operator== to
decide whether the assertion would succeed or fail. Ideally, both
addition expressions should give Vector<double> because (int() + double
()) is a double.
Any thought will be appreciated!
Ben
> The above innocent code may not even compile depending on how operator==is declared. If it did, it is again up to the details of operator== to
decide whether the assertion would succeed or fail. Ideally, both
addition expressions should give Vector<double> because (int() + double
()) is a double.
if you are faced with a dilemma like this, consider how C++''s double
and int may behave in this situation. That helps keep code consistent
and you don''t have to parse your mental design each time. Scott Meyer''s
book Effective C++ has some insights on overloading. Try it at your
local B&N or Borders.
My 0.000002 cents!
> if you are faced with a dilemma like this, consider how C++''s doubleand int may behave in this situation. That helps keep code consistent
and you don''t have to parse your mental design each time. Scott Meyer''s
book Effective C++ has some insights on overloading. Try it at your
local B&N or Borders.
Thanks for your advices!
As to your first advice, please see that I did make an effort to keep
the semantic consistent with that with C++''s native types. This is
exactly why I want:
// pseudo C++ code
template <typename U, typename V>
Vector<typeof(U()+V())> operator+ (
const Vector<U>&,
const Vector<V>&);
My problem is how to write typeof(U()+V()) in a way that the compiler
would understand. I have considered a traits class but specializations
will be endless in that case.
I will seriously consider your second advice.
Yours,
Ben
benben wrote:if you are faced with a dilemma like this, consider how C++''s double
and int may behave in this situation. That helps keep code consistent
and you don''t have to parse your mental design each time. Scott Meyer''s
book Effective C++ has some insights on overloading. Try it at your
local B&N or Borders.
Thanks for your advices!
As to your first advice, please see that I did make an effort to keep
the semantic consistent with that with C++''s native types. This is
exactly why I want:
// pseudo C++ code
template <typename U, typename V>
Vector<typeof(U()+V())> operator+ (
const Vector<U>&,
const Vector<V>&);
My problem is how to write typeof(U()+V()) in a way that the compiler
would understand. I have considered a traits class but specializations
will be endless in that case.
Ok, I''m a beginner too since the last 4 years or so!
How about you declare an abstract base class B from which U and V
derive, and in operator+ you can have a factory design pattern return
the appropriate type.
Once you have the type returned, you can then overload == to accept
parameters of type base B, which will therefore work in all cases, and
can do comparisons too. I think this should work.
Form wikipedia this is a sample factory pattern:
#include <memory>
using std::auto_ptr;
class Control { };
class PushControl : public Control { };
class Factory {
public:
// Returns Factory subclass based on classKey. Each
// subclass has its own getControl() implementation.
// This will be implemented after the subclasses have
// been declared.
static auto_ptr<Factory> getFactory(int classKey);
virtual auto_ptr<Control> getControl() const = 0;
};
class ControlFactory : public Factory {
public:
virtual auto_ptr<Control> getControl() const {
return auto_ptr<Control>(new PushControl());
}
};
auto_ptr<Factory> Factory::getFactory(int classKey) {
// Insert conditional logic here. Sample:
switch(classKey) {
default:
return auto_ptr<Factory>(new ControlFactory());
}
}
这篇关于关于重载运算符的返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!