2

In my .h file, I have:

struct tup{ tup() : token{{-1,"a","b","c","d","e","f"}, {-1,"a","b","c","d","e","f"}, ... {-1,"a","b","c","d","e","f"}} {} struct { int pos; std::string nj, ny, pa, ri, ct, fl; } token[100]; 

Where the "..." refers to 97 more lines of the same type of code. Is there a more elegant way to set default values for my token?

12
  • 5
    Make a default constructor for that inner struct? Also where is this "multi-dimensional" array mentioned in the title? Commented Jul 27, 2015 at 18:04
  • @NunsBeachSurfer Why are there only 97 initializers instead of 100?:) Commented Jul 27, 2015 at 18:05
  • 1
    @VladfromMoscow 97 more lines they are already showing 3. Commented Jul 27, 2015 at 18:06
  • @NathanOliver When what is the problem simply to copy and paste these lines? Commented Jul 27, 2015 at 18:27
  • 1
    @VladfromMoscow I actually think they did and want to know a better way. I can't fault them for not posting 100 lines of the same code. Commented Jul 27, 2015 at 18:41

3 Answers 3

5

If you are open to using std::vector instead of an array, you an use:

struct tup{ tup() : tokens(100, {-1,"a","b","c","d","e","f"}) {} struct token { int pos; std::string nj, ny, pa, ri, ct, fl; }; std::vector<token> tokens; }; 
Sign up to request clarification or add additional context in comments.

1 Comment

@NunsBeachSurfer, you are welcome. I am glad I was able to help.
0

You should use a constructor in the cpp file (and classes instead of structs). The actual content of the array is an implementation detail that shouldn't be in the header. The best way is to use a loop (100 items is a tiny number for a computer, you won't see the difference) in an method that initializes the class. It also allows you to handle errors better (constructors cannot return error codes easily; you could use a reference or a member for that, but it's ugly). This is the best approach I can think of.

In your parser, are all the strings just characters? you could simply use a char or char[n+1] if n is the largest size for yours strings. With std::strings, you have static allocation for small sizes (wasting a bit of space) and dynamic allocation for larger sizes. If you have to handle allocation errors, an member for initializing the class is what you need. Thus, the header will only provide the minimal information necessary for other files (structure and size, not data).

If you want to be able to read your code later, I also suggest that you use meaningful variables names (their sizes is not a problem).

That limit of 100 looks like code smells to me. Why not use dynamic allocation and unrestricted size? you could simply push new token values in a collection when you build them. The default values should only be in the token's constructor, not in a array with a dummy size. What if later you extend your parser to handle more tokens? hard-coded maximum values for arrays are a bad practice if you want your code to evolve. When you reach the limit, you often experience bugs before you notice what's wrong.

Comments

0

Here is a demonstrative program that shows how the constructor could be defined provided that your compiler supports C++ 2014. Otherwise you have to change type std::remove_extent_t to its equivalent in C++ 2011.

#include <iostream> #include <algorithm> #include <iterator> #include <type_traits> #include <string> struct tup{ tup() { std::fill( std::begin( token ), std::end( token ), std::remove_extent_t<decltype( token )>( {-1,"a","b","c","d","e","f"} ) ); } struct { int pos; std::string nj, ny, pa, ri, ct, fl; } token[100]; }; int main() { tup tup; std::cout << tup.token[0].pos << ' ' << tup.token[0].nj << std::endl; std::cout << "//..." << std::endl; std::cout << tup.token[99].pos << ' ' << tup.token[99].nj << std::endl; } 

The program outputs is

-1 a //... -1 a 

That is in any case you can use standard algorithm std::fill as I showed or some other way for example using a lambda expression.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.