1

If I have this function:

void initPoints(sf::Vector2f points[]);

Why can I do this:

sf::Vector2f[] vecs = {sf::Vector2f(0,0), etc};
initPoints(vecs);

But can't do this?

initPoints({sf::Vector2f(0,0), etc});

VS gives me an error on the second one, but not on the first one.

25
  • 2
    Because function parameter sf::Vector2f points[] really means sf::Vector2f* points. It is a syntactic strangeness of the language. Commented Nov 17, 2014 at 19:34
  • 1
    Which compiler are you using? I'm not trying to be difficult: the code you want to use is wrong for a slighlty different reason in C++11 than it was in earlier versions of the language, but it's possible to add an overload to fix that code in C++11. Commented Nov 17, 2014 at 19:37
  • 2
    After all, whats the error the compiler give you?? Commented Nov 17, 2014 at 19:41
  • 3
    @Fly {a, b, c} is not an array. It is a special kind of initializer that can be used to initialize arrays. But it cannot be used to initialize a pointer. As I said in my first comment, your function's parameter is a pointer. Commented Nov 17, 2014 at 20:02
  • 2
    int* is a pointer to an int. That's all. The thing it, C++ gets pointer arithmetic from C, so you can use a pointer to an int to access ints next to it, which means you can almost treat a pointer to int as an array of int. This is so confusing and dangerous that you'll quickly learn to avoid it and use types with better defined semantics. Commented Nov 17, 2014 at 20:15

2 Answers 2

2

By using std::vector or std::array you can resolve your problem easier :) Furthermore, std::vector is RAII-conform, so you don't have to manage the memory by yourself. Generally, STL classes are better than C-type arrays.

#include <vector>
#include <initializer_list>
// ...
std::vector<sf::Vector2f> vecs = { Vector2f(0,0), etc };

Then:

initPoints(const std::vector<sf::Vector2f>& vec) {
    // ...
}
initPoints(vecs);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. I actually already tried with std::vectors, but what I need is to be able to init an array/vector in a single line, since my "real" problem is that I have a class extending a superclass, and I'm calling the superclasses constructor - The constructor of my superclass needs an array/vector/whatever, and I can't create a variable for this vector/array first, so I need to basically do it in one line. I asked my question the way I did because I find it actually interesting in general why this isnt possible and how to solve it.
Something like this: BaseClass::BaseClass() : SuperClass(std::vector<sf::Vector2f> { Vector2f(0,0), etc }) {} ? (using <initializer_list>)
1

C++ generally doesn't allow you to pass an actual array as a function parameter. It has a convenience feature that makes you think that's possible, in that you can actually pass a pointer.

In other words:

void initPoints(sf::Vector2f points[]);

Is the same thing as

void initPoints(sf::Vector2f* points);

Note that initPoints doesn't know the length of points, so generally you also pass a length parameter:

void initPoints(sf:Vector2f* points, size_t length);

What you're trying to do simply isn't valid pre-C++11. In C++11 you can overload initPoints() to take a std::initializer_list<sf::Vector2f>, and the syntax will work fine.


The kind of array you're using is often called a "C-style array." It exists in C, and has existed in C++ from the beginning. It has various limitations, such as what you've just run into. It looks like you really want a std::vector. There is some nuance to using std::vectors, and I don't know your level of C++ understanding, so I don't know if the phrases "usually you don't want to pass them by value" or "I would recommend you imitate STL functions and pass begin/end iterators instead" mean anything to you. You will eventually come across the parts of the language that make those statements useful. You don't need to worry about them right now.

1 Comment

There's no exception. void initPoints(sf::Vector2f[3] points); is syntactically invalid. And void initPoints(sf::Vector2f points[3]); is the same as ``void initPoints(sf::Vector2f* points);`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.