vector and pointer help C++

Started by
3 comments, last by nobodynews 17 years, 6 months ago
I have two things on my mind at the moment that i wish we be resolved. The first one is the initialization of vectors of structures and setting their variables. For instance i wanted to use the following code: vector<SDL_Rect> fred(1); fred[0] = {0, 3, 32, 45}; For those that don't know SDL_Rect is a structure of 4 variables. This code doesn't work. I was wondering if there was a way to do this as i see it is possible to do: SDL_Rect newRect = {0, 3, 32, 45}; So what is the difference? or the problem? Please help. Also i was wondering if anyone has any good links to a tutorial on pointers. I've been looking but i've not found anything 'exceptional'. Thanks...
Advertisement
You cannot use that type of assignment there. You can only use that assignment when you create an instance of the Rect yourself. In this case the vector creates the rects for you, so you are stuck with their initial values.

The best thing ( as an alternative to manually setting each of the rect.x,rect.y, rect.w and rect.h members to suitable values ) would be to write a function to do it for you:

void setupSDLRect( SDL_Rect &rect, int x, int y, int w, int h ){   rect.x = x;   rect.y = y;   rect.w = w;   rect.h = h;}int main(int,char**){   vector<SDL_Rect> fred(1);   setupSDLRect( fred[0], 0, 3, 32, 45 );}


You could also try this way, but this involves a second rect being created:
int main(int,char**){   vector<SDL_Rect> fred;   SDL_Rect rect = {0,3,32,45};   fred.push_back( rect );}


Unfortunatly I have no pointer links for you that I know to be good. However, the function "setupSDLRect" uses a reference to pass the rect into the function and modify it inside the function. You may not be familiar with references, look into them if so and if you need help just ask.
The following is an initialization:
T foo = bar;


The following is an assignment:
foo = bar;


The {a1, a2 ... an} syntax only works for initializations. This is why:
SDL_Rect newRect;newRect = {0,3,32,45};

Doesn't work, because the second statement is an assignment. Your first snippet shows a construction initialized with the argument 1, followed by an assignment. Therefore you can't do it.
aaah now i see :) thanks.
and thanks for the function idea, i'll use that.
Quote:Original post by charlando
I have two things on my mind at the moment that i wish we be resolved. The first one is the initialization of vectors of structures and setting their variables. For instance i wanted to use the following code:
vector<SDL_Rect> fred(1);
fred[0] = {0, 3, 32, 45};
For those that don't know SDL_Rect is a structure of 4 variables. This code doesn't work. I was wondering if there was a way to do this as i see it is possible to do:
SDL_Rect newRect = {0, 3, 32, 45};
So what is the difference? or the problem? Please help.

Also i was wondering if anyone has any good links to a tutorial on pointers. I've been looking but i've not found anything 'exceptional'.

Thanks...


Initialization and assignment are two very different beasts. SDL_REct newRect = {0, 3, 32, 45}; is initialization, but the same syntax will not work for assignment. Eg., what you want. Now with a class you could just create a constructor that will do something similar to what you want. Of course, you should also have an assignment operator for completeness sake, but the default assignment will work as long as you don't have pointers. This won't work either because you can't modify SDL_Rect. However, for future reference this is how the code would look to do what you want:

#include <vector>#include <iostream>class CSDL_Rect{public:  int a, b, c, d; // I don't remember the names of the varialbes in SDL_Rect  CSDL_Rect(); // default constructor  CSDL_Rect(int, int, int, int); // initialization constructor  CSDL_Rect(const CSDL_Rect &rhs); // copy constructor    // other stuff, as needed};CSDL_Rect::CSDL_Rect() : a(0), b(0), c(0), d(0) {}CSDL_Rect::CSDL_Rect(int a_, int b_, int c_, int d_) : a(a_), b(b_), c(c_), d(d_) {}CSDL_Rect::CSDL_Rect(const CSDL_Rect & rhs) : a(rhs.a), b(rhs.b), c(rhs.c), d(rhs.d) {}int main(){  std::vector< CSDL_Rect > fred(3);  fred[0] = CSDL_Rect(0, 3, 32, 45);  fred[1] = CSDL_Rect();  fred[2] = CSDL_Rect(fred[0]);  for(int i = 0; i < fred.size(); i++)  {    std::cout << fred.a << " " << fred.b << " " << fred.c << " " << fred.d << std::endl;  }  return 0;}


Now, to answer your question... you can't do what you want. Just bite the bullet and assign the elements one at a time. Sorry.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

This topic is closed to new replies.

Advertisement