1

I am trying to push the values of 1 class vector to other class vector using dynamic casting. But I am getting a segmentation fault.

When I debugged the program using gdb I found that dynamic_cast is not happening so there is no value to push to the vector.

Here I am trying to copy the elements from std::vector<BPatch_point *> *points to std::vector<ldframework::Point *> *lpoints.

BPatch_point and Point are completely unrelated classes.

Could you please help me on this?

int main(int argc , char *argv[])
{

        BPatch bpatch;
        int pid;
        if (argc != 3) {

                exit(1);
        }

        pid=atoi(argv[1]);

        char name[ 40 ];
        cout<<"The attached pid is "<<pid<<endl;


        BPatch_process *appProc = bpatch.processAttach("",pid);
        BPatch_image *img = appProc->getImage();

        std::vector<BPatch_function *> functions;
        std::vector<BPatch_point *> *points;


        img->findFunction(argv[2], functions);
        if(functions.size()==0) {
                cout<<"unable to find the function "<<argv[2]<<endl;
                return -1;
        }
        else {
              cout<<"The "<<argv[2]<<" function is found"<<endl;
        }
        points = functions[0]->findPoint(BPatch_entry);
        if ((*points).size() == 0) {
                cout<<"Not able to find the points"<<endl;
        }
        cout<<"The points is "<<(*points)[0];
        std::vector<ldframework::Point *> *lpoints=NULL;
        for(unsigned int i=0; i<(*points).size();i++)
    {
        lpoints->push_back(dynamic_cast<ldframework::Point *>((*points).at(i)));
    }
}
10
  • You'd have to do each element on its own, or just use a std::vector<boost::any>>
    – paulm
    Commented May 30, 2014 at 7:32
  • 2
    What's the relationship between BPatch_point and Point?
    – Mat
    Commented May 30, 2014 at 7:34
  • 4
    Then what do you expect that cast to do? You'll need some sort of conversion if they're unrelated.
    – Mat
    Commented May 30, 2014 at 7:35
  • 3
    You need a conversion between BPatch_point and ldframework::Point; the fact that they're stored in vectors is irrelevant.
    – ildjarn
    Commented May 30, 2014 at 7:41
  • 2
    You are asking us how to convert an object of class A to an object of unrelated class B without giving any information on A and B. What answer are you expecting?
    – iavr
    Commented May 30, 2014 at 7:56

2 Answers 2

3

What you need to do is transform objects one by one, not cast them. Fortunately the standard library makes it very easy.

#include <vector>
#include <algorithm>
#include <iterator>

ClassB * ConvertAtoB(ClassA * a)
{
    // create a new object of type ClassB here
}

int main()
{
    std::vector<ClassA*> a;

    // fill 'a' with data
    // ...

    // then transform it into 'b'
    std::vector<ClassB*> b;
    std::transform(a.begin(), a.end(), std::back_inserter(b), ConvertAtoB);
}
2

Here I am trying to copy the elements from std::vector *points to std::vector *lpoints. Could you please help me on this?

BPatch_point and Point are completely unrelated classess.

This can be translated to:

I have a zoo with elephants. Can you please help me on how can I convert these elephants to oranges and put them in an orange container box?

When classes are unrelated, their only common thing is void * - "a pointer to something". Another option is to use a placeholder for any value - such as boost::any.

But the core question is: why do you want to move classes of one type to container of classes of another type. There's 99.8% chance, that you're doing something wrong in the first place and that's the place you should find solution for.


Edit: (in response to comments)

Can you please suggest how it can be done using boost::any method or void * method

Replace std::vector<ldframework::Point *> with either std::vector<boost::any> (if you can use boost library in your project) or std::vector<void *>. Then you will be able to put anything there.

Though I'm still pretty convinced, that you're doing something very wrong. Feel free to use the described solution though if you really know, what you're doing.

7
  • For my project requirements it needs to be done. I am 100% sure this needs to be done. But I am not aware of how it can be done. Thanks for the suggestion Commented May 30, 2014 at 7:46
  • 1
    @user2659232 'But I am not aware of it can be done.' No it cannot be done with unrelated classes and dynamic_cast<>. Commented May 30, 2014 at 7:48
  • ok. I am really sorry then. Can you please suggest how it can be done using boost::any method or void * method Commented May 30, 2014 at 7:50
  • 2
    @user2659232 Please provide a conclusive reason for this (in the question, not in the comments), i.e. explain the requirement that forces you to do this mad conversion. As Spook suggested, this is very very bad programming practice.
    – filmor
    Commented May 30, 2014 at 7:55
  • 2
    std::vector<void *> screams for a follow up question "How do I convert a void * back to its original type?".
    – nwp
    Commented May 30, 2014 at 8:00

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.