开发者

C++ how to pass values from one structure to another?

I have two different structures. The first, astm, which reads data from a .txt file, and the second, rebar, uses the values from structure one to read user input, and make calculations. Sturct astm will act as a menu. My question is, how do I pass the values from struct astm to struct rebar?

here are the two structures:

typedef struct
{
    int size; 
    double weight;
    double diameter;
 } astm;

 typedef struct
 {
    int size;// user input from astm.size
    double length; // user input
  } rebar;

here is the .txt file ( actual file doesn't contain "sz", "wt", or "diameter"):

sz   wt    diameter
2   0.167  0.250
3   0.376  0.375
4   0.668  0.500
5   1.043  0.625
6   1.502  0.750
7   2.044  0.875
8   2.670  1.000
9   3.400  1.128
10  4.303  1.27
11  5.313  1.41
14  7.65   1.693
1开发者_开发百科8  13.6   2.257

Example: Select a size: 4 Enther length: 500

Size: 4 diameter: 0.500 Total weight = length * weight = 3340.0


To answer your question:

astm myAstm;
myAstm.size = 5;
myAstm.weight = 30.0;
myAstm.diameter = 10;

rebar myRebar;
myRebar.size = myAstm.size;
myRebar.length = someCalculation(...)

However, a better approach would be to allow one to be embedded into the other:

typedef struct
{
    int size; 
    double weight;
    double diameter;
} astm;

typedef struct
{
    astm *myAstm;
    double length; // user input
} rebar;

Doing it this way would result in less redundant data lying around, and it would allow you to pass around exactly one structure which represents everything.

astm myAstm;
myAstm.size = 5;
myAstm.weight = 30.0;
myAstm.diameter = 10;

rebar myRebar;
myRebar.myAstm = &myAstm;
myRebar.length = someCalculation(...)

// To get at the size attribute:
printf("size is %d\n", myRebar.myAstm->size);


You could overload the assignment operator.

But be careful, operator overloading is not always the best way.

Maybe, a convert function, or a member function is the better choice.


Overload the assignment operator is one possible route, but not many are not fond of this. You could also pass them directly to the member variable.

objRebar.size = objastm.size;

Or you could make these into classes and add a member function to handle it for you. Is it necessary that you use a struct?


Ideally, you would like to have a map that looks up the weight / diameter based on the size. Since you are using C++, take a look at std::map ( http://www.cplusplus.com/reference/stl/map/ ).

change astm to hold only double weight, diameter; and when reading the txt file for each sz, do a map.insert(pair<int,astm>(sz,astm_var));

When calculating, simply lookup the astm from the map and evaluate the total weight from it.


I would solve this by creating a function:

void MoveInfo(const Astm &t, ReBar &bar);

But to do that you would need to have the struct names provided properly:

struct Astm { ... };
struct ReBar { ... };

Notice the position of the struct name.


If rebar was using astm directly (and you're actually using C++), you would have something like this:

struct astm
{
    astm( int size, float weight, float diameter )
    : size( size )
    , weight( weight )
    , diameter( diameter )
    {}

    int size;
    double weight;
    double diameter;
};

struct rebar
{
    rebar( int size, double length )
    : size( size )
    , length( length )
    {}

    rebar( const astm& astm ) //<<< Uses astm directly
    : size( astm.size )
    {
        // Do something with rest of astm
    }
};

However, this does not appear to be the case. Sounds like you want something like:

    std::vector<rebar> rebarVec;
    for ( int i = 0; i < numThings; ++i )
    {
        // Compute stuff from astm[ i ]
        rebar rebarItem( size, length );
        rebarVec.push_back( rebarItem );
    }

Is this what you're trying to accomplish?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜