0

I am trying to do something like this with 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 class B

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

3 Answers 3

1

If you want to treat the objects in the data[] generically (i.e by calling a common function on them to extract a description or values) then define a class hirachy for your objects and in your showContentsofArray function call virtual methods on your (common base class) object pointers.
This is a textbook example of Polymorphism:
"polymorphism allows values of different data types to be handled using a uniform interface."
In the example below the base class BaseObject defines the uniform interface.

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

Will output:

Im an A object  
Im a B object  
I bounce!  
Sign up to request clarification or add additional context in comments.

Comments

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

And every Class should have an implementation of the method print() that knows how to print its values.

You could also use inheritance.

EDIT:

@Ricibob's answer is correct, but if you need to do the casting inside the function, you need to do something like this:

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

You can't evade inheritance.

2 Comments

i tried this, it gives me an error saying, left of '->' must point to class as it is currently referencing to (void *)....any idea where i am going wrong
showContentsOfArray should take an array of base class pointers NOT void. Generally you shouldn't need to be dealing with void ptrs in C++. Once you eliminate the void* and work with base class ptrs and virtual functions you eliminate the need for casts and the code looks cleaner. If you apply that change then you've basically got my answer.
0

Just cast back to the original type:

A* p1 = static_cast<A*>(data[0]);
B* p2 = static_cast<B*>(data[1]);

2 Comments

What if I don't know the order of contents in the array. Is there a way I can make a check and then cast it? Like in Java, I can get the class name of the object and then type cast it back. Is there anything similar in c++
If you want something like that, I would suggest boost::variant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.