Structs in Classes (program flow)

Sep 13, 2010 at 12:15pm
I need to parse a large amount of command line arguments. I was thinking the way I could do this from main was to have a class that dealt with this, but then I would need to pass-back, or pass-to another class all the various chunks of info.

The easiest way I thought of doing this would be a struct, but then I haven't used them before additionally all the examples I've seen do not have structs from within classes.

I'm wondering if it's possible to do this, and if so do I need the same struct in the class and main (so a copy in each) or do I reference the struct in the class by class.struct.data.
Or should I just chuck in a pile of set/get methods to deal with the data and throw out the notion of using a struct?
Sep 13, 2010 at 12:45pm
I'd go with get methods. And if you want, you can overload the [] operator for convenience.

This could help -> http://cplusplus.com/forum/beginner/26251/#msg140026

Boost provides a good solution too -> http://www.boost.org/doc/libs/1_43_0/doc/html/program_options.html
Last edited on Sep 13, 2010 at 12:47pm
Sep 13, 2010 at 2:20pm
Really so it can't be done? That's silly... you can have nested structs, you can create structs with objects/classes as it's fields(although it's an expensive copy doing that).
I figured you could do it the other way around as well.
Sep 13, 2010 at 2:28pm
It can be done, I suppose, but it seems a bit redundant to me... You already have the data inside the class you use to parse the command line, why would you want to copy it to another construct?
Sep 13, 2010 at 2:36pm
Yeah I suppose, I thought it would be easier than 8+ lines of getThis() getThat(); in my main.

I figured out how to do it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Class {
public:
  // constructors and members

  struct ClassStruct {
  };

  ClassStruct method(int x, std::string y, char c);
};

Class::ClassStruct Class::method(int x, std::string y, char c)
{
  // definition
}


Now I just need to figure out if it's worth it :P
Last edited on Sep 13, 2010 at 2:37pm
Oct 3, 2010 at 7:06am
Better way: (because I finally read my lecture notes, and found it)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// rec in rec header 

#ifndef REC05_H
#define REC05_H

  struct recPersonName {
    char first[30+1];
    char surname[30+1];
  }; 

  struct recStudent {
    unsigned id;
    recPersonName name;
    recDate born;
    recDate enrolled;
  }; 
#endif


1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using std::cout;
using std::endl;

#include "rec05.h"

int main()
{
  recStudent a1[10], a2[21];
  // do something with it.

  return 0;
}
Topic archived. No new replies allowed.