简体   繁体   中英

dynamic object pointer array C++

I want to make a pointer array that holds address of instances of that class so when I call my scanner function it will search for objects that have the same pcode and print them. I kind of did that but now when I try to use different inherited class objects I get a "memory access violation" and I can't access the inherited class functions through the base static scan function.

sorry for the mess here is my code

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;
};

product.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

main.cpp

#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;
}

Is it a memory address problem or something entirely different? And why does scan{this->print()} call the base function? Is there a way to call the inherited print() function?

Alright, now that I finally simplified your question text, here's the answer:

  • void const printer(); This doesn't compile. If you want calls to a function to call that of the most derived type, you must mark the function virtual . Also, the const goes after the name.
  • void setCode(); It's generally not a good idea for a store product to read from the console directly. It's job is to be an item, and items don't parse numbers. An external function should parse the input.
  • static product *point; This should probably be replaced with a std::vector<product*> . That is a dynamic array of pointers to individual products. Pointers to each product allows the polymorphism of virtual functions to work how you want them to.
  • product *product::point=new product [15]; creates an array of 15 product objects. Not PrepackedFood , or any other type of object, these are only products . No more or less. Also, you're leaking this memory. Use a vector
  • point[a]= this; This doesn't even compile, since point[a] is a product , and this is a pointer.
  • product::product(long& c,string&n) This function doesn't register the product. Any products made with this aren't registered in your array and cannot be found. Luckily, you aren't using it.
  • void getCode(long) { I'm not even sure what this code does it's so bad.
  • void product::scanner(){ Use a for loop instead of a while loop, that's just confusing. Also, you're only scanning the first three items made, it should probably scan all of the ones made. Using a vector would help here.
  • If a product is ever destroyed (which is very very common), then this entire design will fail and fixing that is going to be very complicated. I don't know what you're doing but I recommend that you stop now.

Did I get all of it?

I think you need to try to implement your "array" using a container. std::list would be a good start. Have a look at this article , scroll down to the example with a for loop, this should help you to clean up your code and fix memory access issues. Keep in mind that you can store whole objects in your list, not just pointers to them.
Also, you may try to decouple your data from your code. Try using a struct for your data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM