1

I need some advice, because I am new to c++

For my wind turbine analysis program, I divided it up into various objects that handle certain tasks.

One object handles file input/output, and in my troublesome case i read from a file to get data, and this data is filled into an array of objects called blade, which each holds arrays of things like stresses and coordinates all related to the each blade.

Another task is post processing, and I want this post-pro object to be able manipulate the blades data.

So, my main instantiates a post-pro object, which starts the input/output object and tells it to read the data into Blade object array. So far so good, now I want to get the filled blade object array back into post-pro so i can do some stuff with it.

This leads to my question, but I will first ask question 0:

0: does this way of working with objects sounds correct?

And the actual question:

1: Returning a pointer of the object array seems like the way to go, and for some reason i got it into my head that a shared_ptr is the way to go. But I don't know the syntax for looking at the variable data. Here is some example code from the post-pro class:

void PostProcessor::start() {
        VLMio io;//input/output object
        io.loadData(theFileName);//load file
        test = std::tr1::shared_ptr<Blade>(new Blade());//start up shared ptr called test
        test = io.testReturn();//attempt to receive blade obect array into that pointer, is this correct?
        cout<<test[0].x[0]<<endl//this line is trouble? is this how I would see the first x coord on the first blade? 
    //i.e is the syntax the same as for regular object pointers?
}

Here is an example of what load data may look like, it populates some Blade objects with data that has been read from a file:

void IO::loadData() {
blades = new Blade[numberOfBlades];
blades[0].x[0] = 123;//just for example
blades[0].stress1 = 1234;//just for example
}

I haven't yet worked out how to return these blade objects, but it may look something like this:

std::tr1::shared_ptr<Blade> testReturn() {
//somehow attach a shared_ptr to the blades array pointer thing
//somehow return a shared ptr
}

In summary, is the right way to do it, and what is the syntax for member variables of smart pointer objects, I hope you understand, sorry I am quite new.

CptLightning
  • 555
  • 1
  • 8
  • 18

2 Answers2

2

If you want to use straight up arrays (as in C), you'll need to created an array of Blades, whereas your code creates just one. You'll also need to review this question: TR1 Shared Arrays

But rather than messing with all that, how about just returning a std::vector<Blade> or perhaps a std::tr1::array<Blade, N> if it will have a fixed size?

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • yes, see updates, I have created an array of blades for the pointer now i think. I can't return an vector as each blade object contains arrays and other data members such as other objects, they are variable size – CptLightning Apr 27 '11 at 22:38
  • @CptL Yes you can. And C++ objects of given class always have the same size. –  Apr 27 '11 at 22:43
  • so do i fit my object into a vector somehow? – CptLightning Apr 27 '11 at 22:44
  • @CptL There is no "somehow" involved - you can store any kind of C++ class in a std::vector. –  Apr 27 '11 at 22:47
  • @unapersson , that sounds promising, I will look into it – CptLightning Apr 27 '11 at 22:50
  • @unapersson , Is there any disadvantage if I just return a Blade pointer rather than a vector, ie the function in I/O would be Blade* IO::testReturn() { return blades; } – CptLightning Apr 28 '11 at 16:04
  • @CptL You have to create the array of blades somehow, which means you have to manage it, and in this case you can't use a shared_ptr (because they don't work with arrays). A vector will do all the memory management for you. –  Apr 28 '11 at 16:08
  • maybe the advantage of using vectors to hold objects comes elsewhere, and the advantage isn't for returning. That's what I'm thinking, time to read some C++ books. But for now, I've been using Blade* throughout. – CptLightning Apr 28 '11 at 16:08
0

This line:

    test = std::tr1::shared_ptr<Blade>(new Blade());//start up shared ptr called test

is not allocating an array as you seem to think it is allocating a single Blade instance. An array would look like:

    new Blade[somesize]

but then you could not use shared_ptr to store the returned value. As others have suggested vector<Blade> is probably what you want.

  • see updates, I think I can have mutliple blade objects returned now. I can't return a vector as each blade object contains arrays and other data members such as other objects, they are variable size – CptLightning Apr 27 '11 at 22:39
  • @CptL I think you have a basic lack of understanding of how C++ objects are layed out in memory - all objects of the same class have the same layout and the same size. –  Apr 27 '11 at 22:48
  • yes I do have a basic lack of understanding of a lot of things C++, but I do actually know that I was just a bit mixed up there – CptLightning Apr 27 '11 at 22:53