Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
cpp将对象存储在指针数组中 我试图用C++做这样的事情。_C++ - Fatal编程技术网

cpp将对象存储在指针数组中 我试图用C++做这样的事情。

cpp将对象存储在指针数组中 我试图用C++做这样的事情。,c++,C++,void showContensofArray(void *data[]) { //In this function have to display the values of respective objects. // Any ideas how do I do it? } int main(){ A phew(xxx,abcdefg); //object of class A B ball(90),ball2(88); //object of

void showContensofArray(void *data[])
{
      //In this function have to display the values of respective objects. 
      //  Any ideas how do I do it?
}
int main(){
    A phew(xxx,abcdefg); //object of class A

    B ball(90),ball2(88);  //object of class B

    void *dataArray[2];
    dataArray[0] = &ph1;
    dataArray[1] = &ball;
    showContentsofArray(dataArray); //function
}

只需转换回原始类型:

A* p1 = static_cast<A*>(data[0]);
B* p2 = static_cast<B*>(data[1]);
A*p1=static_cast(数据[0]);
B*p2=静态_转换(数据[1]);

您无法逃避继承。

如果您想以一般方式处理data[]中的对象(即通过对其调用公共函数来提取描述或值),那么请为对象定义类hirachy,并在showContentsofArray函数中对(公共基类)对象指针调用虚方法。
这是一个教科书上的例子:
“多态性允许使用统一接口处理不同数据类型的值。”
在下面的示例中,基类BaseObject定义了统一接口

class BaseObject {
    virtual string description() { return "Base object"; }
    virtual bool bounces() { return false; }
}
class B : public BaseObject {
    string description() { return "Im a B object" }
    bool bounces() { return true; }
}
class A : public BaseObject {
    string description() { return "Im an A object" }
}

void showContensofArray(BaseObject* data[], int size) {
    for (int i=0; i<size; i++) {
        cout << data[i]->description();
        if (data[i]->bounces())
             cout << "I bounce!";
    }
}

int main() {
    A phew(xxx,abcdefg); //object of class A
    B ball(90),ball2(88);  //object of class B

    BaseObject* dataArray[2];
    dataArray[0] = &ph1;
    dataArray[1] = &ball;
    showContentsofArray(dataArray);
}

如果我不知道数组中内容的顺序该怎么办。有没有办法让我检查一下,然后再投进去?与Java一样,我可以获取对象的类名,然后键入cast back。如果你想要这样的东西,我会建议你在c++中是否有类似的东西。我尝试过这个,它会给我一个错误提示,“->”的左边必须指向类,因为它当前引用的是(void*)…任何关于我将要出错的地方的想法showcontentsofarray应该使用基类指针数组而不是void。一般来说,你不需要在C++中处理空的PTR。一旦消除了void*并使用基类ptr和虚拟函数,就不再需要强制转换,代码看起来更干净。如果你应用这个变化,那么你基本上已经得到了我的答案。
#include <iostream>

using namespace std;

class Base{
public:
    virtual void print()=0;
};
class A: public Base{
public:

    void print(){
        cout<<"Object A"<<endl;
    }
};

class B: public Base{
public:
    void print(){
        cout<<"Object B"<<endl;
    }
};

void showContensofArray(void* data[], int len)
{
    int i;
    for(i=0;i<len;i++){
        ((Base*)(data[i]))->print();
    }
}

int main(){
    A a;
    B b;
    void* v[2];
    v[0]= &a;
    v[1] = &b;
    showContensofArray(v,2);
    return 0;
}
class BaseObject {
    virtual string description() { return "Base object"; }
    virtual bool bounces() { return false; }
}
class B : public BaseObject {
    string description() { return "Im a B object" }
    bool bounces() { return true; }
}
class A : public BaseObject {
    string description() { return "Im an A object" }
}

void showContensofArray(BaseObject* data[], int size) {
    for (int i=0; i<size; i++) {
        cout << data[i]->description();
        if (data[i]->bounces())
             cout << "I bounce!";
    }
}

int main() {
    A phew(xxx,abcdefg); //object of class A
    B ball(90),ball2(88);  //object of class B

    BaseObject* dataArray[2];
    dataArray[0] = &ph1;
    dataArray[1] = &ball;
    showContentsofArray(dataArray);
}
Im an A object  
Im a B object  
I bounce!