带有指向ClassB的指针的ClassC,它获取指向ClassC的指针

时间:2016-04-22 18:33:47

标签: c++ class pointers inheritance

我真的不知道在标题中说些什么。我有3个类,第一个获取指向第二个类的指针,该指针获取指向第三个类的指针。我知道如何在每个中添加一些值(使用内存分配),但我不能从第3类中获取第一个中的元素。这是一个显示我的问题的例子:

这是我的第三堂课:

class ClassC{
    int _int;
public:
    ClassC(){}
    ClassC(int a){_int=a;}
    int getInt()const{return _int;}
    virtual std::string whereIam() const {return "in classC";}
    ClassC& operator=(const ClassC& c){
        _int=c.getInt();
        return *this;
    }
};

class ClassChild : public ClassC{
public:
    ClassChild(){}
    ClassChild(int a):ClassC(a){}
    virtual std::string whereIam() const {return "in child of classC";}
};

这是我的第一和第二课(他们看起来一样):

class ClassB{
    ClassC** _classC;
    int _nbElementClassC;


public:
    int getElementClassC() const {return _nbElementClassC;}
    ClassC** getClassC() const {return _classC;}
    ClassB():_classC(0){_nbElementClassC=0;}

    void addClassC(ClassC& c){
        // creat an array base size+1, copy the elements then add the new one
        ClassC **newTab=new ClassC*[++_nbElementClassC];
        if(_classC){
            for(int i=0;i<getElementClassC()-1;i++){
                newTab[i]=_classC[i];
            }
            newTab[getElementClassC()-1]=&c; // add the new element
            delete[] _classC;
        }
        _classC=newTab;
        // delete[] newTab; ERROR WHEN I TRY TO FREE IT
    }
    ClassB& operator=(const ClassB& b){
        _nbElementClassC=b.getElementClassC();

        if(_classC)
            delete[] _classC;
        _classC=b.getClassC();

        return *this;
    }
};

class ClassA{
    ClassB** _classB;
    int _nbElementClassB;
public:
    int getElementClassB() const {return _nbElementClassB;}
    ClassB** getClassB() const {return _classB;}
    ClassA():_classB(0){_nbElementClassB=0;}
    void addClassB(ClassB& c){
        // creat an array base size+1, copy the elements then add the new one
        ClassB **newTab=new ClassB*[++_nbElementClassB];
        if(_classB){
            for(int i=0;i<getElementClassB()-1;i++){
                newTab[i]=_classB[i];
            }
            newTab[getElementClassB()-1]=&c; // add the new element
            delete[] _classB;
        }
        _classB=newTab;
        // delete[] newTab; ERROR WHEN I TRY TO FREE IT
    }
};

一个简单的主要内容:

int main()
{
    ClassA a;       
    addEverything(&a);

    return 0;
}

我想要的是用其他人填充我的第一堂课:

void addEverything(ClassA* a){

    ClassChild c1(1),c2(2),c3(3),c4(4),c5(5);
    ClassB *b1=new ClassB(),*b2=new ClassB(),*b3=new ClassB();

    b1->addClassC(c1); // Adding c1 to b1
    b1->addClassC(c2); // Adding c2 to b1
    b2->addClassC(c3); // Adding c3 to b2
    b3->addClassC(c4); // Adding c4 to b3
    b3->addClassC(c5); // Adding c5 to b3

    // Now I add my 3 classes int the first class
    a->addClassB(*b1);
    a->addClassB(*b2);
    a->addClassB(*b3);


    std::cout << b1->getElementClassC() << std::endl; // gives the right number

    // ERROR HERE, BAD ACCESS, return _int is wrong
    std::cout << b1->getClassC()[0]->getInt() << std::endl;
    std::cout << a->getClassB() << std::endl; // works, give the adress

    // My goal is to do something like that
    for(int i=0;i<a->getElementClassB();i++){
        for(int j=0;j<a->getClassB()[i]->getElementClassC();j++){
            std::cout << a->getClassB()[i]->getClassC()[j]->getInt() << std::endl;
        }
    }
}

以下是我的问题: a->getClassB()[i]是正确的,getClassB()返回ClassB成员

那么为什么getClassB()[0]->getClassC()不起作用? 我想将所有内容存储在我的第一个类中,然后显示childClass元素

我的错误: int getInt()const{return _int;}由于NULL,我有EXC_BAD_ACCESS 因为b1->getClassC()[0]->getInt()

如果您有任何疑问,我就在这里。我不知道怎么做,也许我要再为ClassB ** _classB添加一颗星;或者我的->

有问题

0 个答案:

没有答案