调用超类方法运算符== 我正在经历从java到C++的过渡,我正在尝试编写一个简单的程序。
有一个超类调用超类方法运算符== 我正在经历从java到C++的过渡,我正在尝试编写一个简单的程序。,c++,inheritance,g++,C++,Inheritance,G++,有一个超类Animal,具有以下接口: class Animal { public: Animal(int a, std::string n); bool operator==(Animal a); private: int age; std::string name; }; 它是子类Dog: class Dog : public Animal { public: Dog(int a, std::string n, double w); bo
Animal
,具有以下接口:
class Animal
{
public:
Animal(int a, std::string n);
bool operator==(Animal a);
private:
int age;
std::string name;
};
它是子类Dog
:
class Dog : public Animal
{
public:
Dog(int a, std::string n, double w);
bool operator==(Dog d);
private:
double weight;
};
bool Dog::operator==( Animal const& other ) const
{
return dynamic_cast<Dog const*>( &other ) != nullptr
&& Animal::operator==( other )
&& weight == other.weight;
}
我的问题是关于狗的操作符==
方法,它比较两条狗
下面是动物的操作符==
bool Animal::operator==(Animal a) //overriding of operator ==
{
return (age==a.age) && (name==a.name);
}
现在我想用动物的方法写狗的版本
就像我在Java中所做的那样:
boolean equals(Dog d){
return (super.equals(d)) && (this.name==d.name);
}
我需要的是C++代码等价的代码>(Super .Error(d))< /C>。如果它是一个具有普通名称的方法,那么它会很简单(Animal::equals(d)),但我不知道如何为运算符==,它有不同的语法。
它实际上非常简单:return Animal::operator==(d) && name==d.name;
使用超类的名称而不是<代码>超级的原因是C++中可以有多个超类,所以你必须弄清楚你想要哪一个。 或者,您可以通过使用它的重载调用它:
return ((Animal&)*this)==d && name==d.name;
由于在这种情况下,operator=
的参数将是动物&
和狗&
,因此它不能匹配Dog::operator==(Dog d)
,因此使用Animal::operator==(Animal a)
注意:您的签名非常不寻常。相反,请使用以下选项之一:
bool operator==(const Animal& a) const;
friend bool operator==(const Animal& left, const Animal& right);
它们不会在每次比较时复制动物,并且可以比较
const
动物。这实际上非常简单:
return Animal::operator==(d) && name==d.name;
使用超类的名称而不是<代码>超级的原因是C++中可以有多个超类,所以你必须弄清楚你想要哪一个。 或者,您可以通过使用它的重载调用它:
return ((Animal&)*this)==d && name==d.name;
由于在这种情况下,operator=
的参数将是动物&
和狗&
,因此它不能匹配Dog::operator==(Dog d)
,因此使用Animal::operator==(Animal a)
注意:您的签名非常不寻常。相反,请使用以下选项之一:
bool operator==(const Animal& a) const;
friend bool operator==(const Animal& left, const Animal& right);
它们不会在每次比较时复制动物,并且可以比较
const
动物。您可以使用详细符号调用操作员:
operator==(const Dog& dog) { return Animal::operator==(dog) && weight==dog.weight; }
您可以使用详细表示法调用运算符:
operator==(const Dog& dog) { return Animal::operator==(dog) && weight==dog.weight; }
Java
equals
的直接等价物是:
bool Dog::operator==(Dog d)
{
return Animal::operator==(d) && weight == d.weight;
}
但我不确定这是否是你真正想要的。首先,
你的论点是抄袭的,这意味着你的
比较副本。特别是当你打电话的时候
Animal::operator=
,您将传递一份Animal
部分的副本
属于狗的
,不是完整的对象。类类型通常是
通过引用;如果不想,请引用const
修改它们。所以底座上的签名应该是
比如:
同样地,在Dog
中。此外,Dog
可能需要一个动物常数&
,而不是狗常数&
。(在
Java,equals
始终采用Java.lang.Object
),这意味着
您必须验证它是一只狗
:
class Dog : public Animal
{
public:
Dog(int a, std::string n, double w);
bool operator==(Dog d);
private:
double weight;
};
bool Dog::operator==( Animal const& other ) const
{
return dynamic_cast<Dog const*>( &other ) != nullptr
&& Animal::operator==( other )
&& weight == other.weight;
}
操作符==
和操作符的实现=代码>实际上可以
可以通过从适当的类模板继承来完成
如果您有很多类,则避免使用一些样板文件
它必须支持==
和其他功能。与Javaequals的直接等价物是:
bool Dog::operator==(Dog d)
{
return Animal::operator==(d) && weight == d.weight;
}
但我不确定这是否是你真正想要的。首先,
你的论点是抄袭的,这意味着你的
比较副本。特别是当你打电话的时候
Animal::operator=
,您将传递一份Animal
部分的副本
属于狗的
,不是完整的对象。类类型通常是
通过引用;如果不想,请引用const
修改它们。所以底座上的签名应该是
比如:
同样地,在Dog
中。此外,Dog
可能需要一个动物常数&
,而不是狗常数&
。(在
Java,equals
始终采用Java.lang.Object
),这意味着
您必须验证它是一只狗
:
class Dog : public Animal
{
public:
Dog(int a, std::string n, double w);
bool operator==(Dog d);
private:
double weight;
};
bool Dog::operator==( Animal const& other ) const
{
return dynamic_cast<Dog const*>( &other ) != nullptr
&& Animal::operator==( other )
&& weight == other.weight;
}
操作符==
和操作符的实现=代码>实际上可以
可以通过从适当的类模板继承来完成
如果您有很多类,则避免使用一些样板文件
必须支持==
和其他功能。使用标准一致性签名:bool操作符==(const-Animal&a)const你知道如何在父类中调用普通函数吗?当你明白这一点时,你只需要记住操作符==
只是一个普通的成员函数。@crashmstr:这不是一个骗局,因为他说他已经知道如何使用Animal::equals(d)
。使用标准的一致性签名:bool操作符==(const-Animal&a)const你知道如何在父类中调用普通函数吗?当你明白这一点时,你只需记住,操作符==
只是一个普通的成员函数。@crashmstr:这不是一个骗局,因为他说他已经知道如何使用Animal::equals(d)
weight
是double
类型,你确定你的代码没有问题吗?@PiotrS。为什么不呢?这取决于实际值来自何处,但如果它们基本上是不可变的,并且来自人工输入,那么测试就可以了。@JamesKanze:例如,要消除你提到的这种“依赖性”吗?允许他们改变吗?@PiotrS。但如何改变?我仍然不认为有任何理由在这里避免这种平等。@JamesKanze:,请不要说“但是比较15.0==15.0
或1.0+1.0==2.0
!”,因为这不是一个大发现,也不是比较double
平等的借口weight
属于double
类型,你确定你的密码没有问题吗?@PiotrS。W