-1

Example: // test.h

#define MAX 3

test.cpp

static char *movies[MAX] = {
    "The Departed", "The Crow", "Hot Fuzz"};

//

Why not use Vector<char*>, or Vector<string*>, or an Array, or another data type? What benefits do i have over the other data types?

Let me preface this by saying that i'm coming from the Java world andi've been learning C++ for a few months.

breakou7z
  • 3
  • 3
  • 6
    Perhaps a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) might help. – Jason Apr 28 '22 at 10:11
  • 4
    Strictly speaking this isn't valid code, it should be `static const char *movies[MAX]` since string literals are immutable – UnholySheep Apr 28 '22 at 10:11
  • 1
    That seems more like C than C++. Where did you see that code? From where did you get it? – Some programmer dude Apr 28 '22 at 10:11
  • 3
    This is an array, making the part about "an array, or another data type" confusing – UnholySheep Apr 28 '22 at 10:13
  • 1
    Also note that `Vector` is wrong. Not only in captialization but also the contained type. It should be `vector` (or `std::vector` if you don't do `using namespace std;` which is [a bad habit](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)). – Some programmer dude Apr 28 '22 at 10:13
  • 6
    Also, C++ and Java are quite different languages, despite the fact they have some superficial similarities of syntax. Trying to learn C++ by using Java as a starting point will make you a terrible C++ developer (the reverse is also true - learning Java by using C++ as a starting point would make you a terrible Java developer). If you want to learn C++, get a C++ book, and ignore what you know from Java, because most of it is wrong in C++. – Peter Apr 28 '22 at 10:23

2 Answers2

0

What benefits do i have over the other data types?

One thing to notice if using std::vector is that it will be initialized dynamically at runtime while the static const char*[max] can be initialized at compile time. So it will save some(very little though) runtime.

Jason
  • 36,170
  • 5
  • 26
  • 60
-1

You will not have to directly cast the pointer to another type.. if some functions only accept the char ptr vector then you will not have any conversion to do

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 28 '22 at 18:10