当我试图通过继承类中的指针使用重新加载的友元函数时,它使用基类中的函数



我提前为我的英语不好道歉(如果有任何明显的错误,我很乐意知道(我的问题是不能使用继承类的重载函数。我创建指向基类的指针数组,在程序创建对象的过程中,它可以是基类也可以是继承的。当谈到使用重载函数(运算符<<(时,它经常使用基类的选项,但当我创建两个单独的对象(基类、从基类继承的类(时,重载运算符工作得很好。我应该朝哪个方向挖掘?有代码

#include <iostream>
#include "port.hpp"
using namespace std;
int main()
{
port *p_port[2];
p_port[0] = new VintagePort("BrandONe", 10, "NickName", 19978);
p_port[1] = new port("BrandNameTwo","StyleTwo",1);
for (int i = 0; i < 2; i++)
{
cout << *p_port[i] << endl;
}
for (int i = 0; i < 2; i++)
delete p_port[i];

cout << endl;
port p("BrandNameTwo","Style",2);
VintagePort vp("BrandName",19,"NickName",1982);
cout << p << endl;
cout << vp << endl;
}
#ifndef PORT_HPP_
#define PORT_HPP_
#include <iostream>
class port
{
private:
char *brand;
char style[20];
int bottles;
public:
port(const char *br = "none", const char *st = "none", int b = 0);
port(const port &p);
virtual ~port() { delete[] brand; }
port &operator=(const port &p);
port &operator+=(int b);
port &operator-=(int b);
int BottleCount() const { return bottles; }
virtual void Show() const;
friend std::ostream &operator<<(std::ostream &os, const port &p);
};
class VintagePort : public port
{
private:
char *nickname;
int year;
public:
VintagePort();
VintagePort(const char *br, int b, const char *nn, int y);
VintagePort(const VintagePort &vp);
~VintagePort() { delete[] nickname; }
VintagePort &operator=(const VintagePort &vp);
void Show() const;
friend std::ostream &operator<<(std::ostream &out, const VintagePort &vp);
};
#endif
#include "port.hpp"
using std::ostream;
port::port(const char *br, const char *st, int b)
{
brand = new char[strlen(br) + 1];
strcpy(brand, br);
strcpy(style, st);
bottles = b;
}
port::port(const port &p)
{
brand = new char[strlen(p.brand) + 1];
strcpy(brand, p.brand);
strcpy(style, p.style);
bottles = p.bottles;
}
port &port::operator=(const port &p)
{
if (this == &p)
return *this;
delete[] brand;
brand = new char[strlen(p.brand) + 1];
strcpy(brand, p.brand);
strcpy(style, p.style);
bottles = p.bottles;
return *this;
}
port &port::operator+=(int b)
{
bottles += b;
return *this;
}
port &port::operator-=(int b)
{
bottles -= b;
return *this;
}
void port::Show() const
{
std::cout << "Brand: " << brand;
std::cout << "nKind: " << style;
std::cout << "nBottles: " << bottles << std::endl;
}
ostream &operator<<(ostream &out, const port &p)
{
out << p.brand << ", " << p.style << ", "
<< p.bottles << std::endl;
return out;
}
VintagePort::VintagePort()
{
nickname = nullptr;
year = 0;
}
VintagePort::VintagePort(const char *br, int b, const char *nn, int y)
: port(br, "vintage", b)
{
year = y;
nickname = new char[strlen(nn) + 1];
strcpy(nickname, nn);
}
VintagePort::VintagePort(const VintagePort &vp) : port(vp)
{
nickname = new char[strlen(vp.nickname) + 1];
strcpy(nickname, vp.nickname);
year = vp.year;
}
VintagePort &VintagePort::operator=(const VintagePort &vp)
{
if (this == &vp)
return *this;
port::operator=(vp);
delete[] nickname;
nickname = new char[strlen(vp.nickname) + 1];
strcpy(nickname, vp.nickname);
year = vp.year;
return *this;
}
void VintagePort::Show() const
{
std::cout << "nNickname: " << nickname << std::endl;
port::Show();
}
ostream &operator<<(ostream &out, const VintagePort &vp)
{
out << vp.nickname << ", ";
// operator<<(os, (const port &)vp);
operator<<(out, (const port &)vp);
return out;
}

此处为程序的结果

BrandONe, vintage, 10
BrandNameTwo, StyleTwo, 1

BrandNameTwo, Style, 2
NickName, BrandName, vintage, 19

对不起,我是新来的,如果我弄错了

重载和重写是非常不同的事情。

重载是在编译时使用静态类型选择的
由于数组元素的类型为port*,因此会选择port&重载。

如果你想要一个";动态的";operator<<,创建一个接受基类并只分派给可以重写的虚拟成员函数的函数。

示例:

class port
{
// ...
public:
virtual std::ostream& print(std::ostream* os) const
{
out << brand << ", " << style << ", "
<< bottles << std::endl;
return out;
}

};

std::ostream& operator<<(std::ostream& os, const port& p)
{
return p.print(os);
}
class VintagePort : public port
{
// ...
public:
std::ostream& print(std::ostream& out) const override
{
out << vp.nickname << ", ";
return port::print(out);
}
};

最新更新