我想创建一个指针数组来保存该类实例的地址,所以当我调用我的scanner
函数时,它将搜索具有相同的对象pcode
并打印它们。我有点这样做,但现在当我尝试使用不同的继承类对象时,我得到一个“内存访问冲突”,我无法通过基本静态扫描函数访问继承的类函数。
抱歉这里的混乱是我的代码
using namespace std;
class product{
public:
product();
product(long&,string&);
void const printer();
void setCode();
void getCode(long);
void static scanner();
static product *point; //when this was static did compile but not now
static int a;
private:
string pname;
long pcode;
};
class PrepackedFood:public product{
//snip
private:
double uPrice;
};
class FreshFood:public product{
//snip
private:
double weight;
double pricepk;
};
产品.cpp
product *product::point=new product [15]; //when i use try to use dynamic cant
int product::a(0);
product::product(){
pcode=0;
pname="unknown";
point[a]= this; //here works for normal arrays not now
a++;
}
product::product(long& c,string&n){
pcode=c;
pname=n;
}
void product::scanner(){
long a;
int i=0;
while(i<3){
if (point[i]->pcode==a){
point[i]->printer();
break;
}
i++;
}
}
void product::setCode(){
cout<<"enter product name\n ";
cin>>pname;
cout<<"enter product code _____\b\b\b\b\b\a";
cin>>pcode;
}
//blah blah for other members
主文件
#include "product.h"
#include <iostream>
int main(){
int i=0;
cout<<"enter fresh foods"<<endl;
FreshFood f[3];
for(int a=0;a<3;a++)
f[i].setCode();
product::scanner();
return 0;
}
是内存地址问题还是完全不同的问题?为什么scan{this->print()}
调用基函数?有没有办法调用继承的print()
函数?