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() {
开发者_如何学Go 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.
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?
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.
精彩评论