Archived

This topic is now archived and is closed to further replies.

Esap1

2 Really Simple C++ Questions

Recommended Posts

Esap1    122
I had two VERY simple C++ Questions. I was looking at some code, and saw that they define a class Vector. Then when they did this: Vector thesevectors[8]; Ive never seen this before, what does that do? 2nd Question. What does this do? void setFace(unsigned int face_id, const Face& face); I mean, whats the const for? I see he uses it ALOT. Thanks for the answers to my stupid little questions PS: The reason I have little gabs in my knowledge because I learned pretty much everything I know from reading(for a lot of years), and a friend thats gone now. later,

Share this post


Link to post
Share on other sites
jtecin    122
Well, I''m no C++ guru by any means, but I think I can answer your questions:

The first thing just creates an array of vectors. In other words, 8 different vectors all named the same thing but assigned a different number.

Secondly, by putting const in front of a variable, the value cannot be changed anywhere else in the program, otherwise you get a compiler error. It is actually funny, because by putting const in front of a variable name, it is no longer a variable!



Masters Software

Share this post


Link to post
Share on other sites
Esap1    122
WOOPS, Kinda mistyped my first question, made myself look like an idiot, I meant what does this do:

Vector thesevectors[8];

And I kinda figured const made it a constant, but Why do they do that in Function Parameters and Return Values(I think)?

Share this post


Link to post
Share on other sites
NuffSaid    122
In reply to your question about const. Look at the way the parameter is passed (by reference). That means that if that parameter is changed by the function in anyway, the original value will change. I''ll try give an example

    
void func(int &a)
{
a += 1; //adds 1 to a

}

After calling this function, a will have a value of 6 because
it was modified by the function

int a = 5;
func(a);

However, if we define function func like this

void func(const int &a)
{
a += 1;//this will cause a compiler error

}

It becomes illegal.


That''s why you make a ref variable const if you don''t want the value to change. Hoped that did help

Share this post


Link to post
Share on other sites
Bordem    122
what do you mean what does this do? it does exactly what it looks like makes an aray of 8 variables of the vector type
which was defined somewhere in the code
might look like that or might be a class or whatever
typedef struct {
float x;
float y;
float z;
};vector


Im Always Bored
--Bordem
ICQ: 76947930

Share this post


Link to post
Share on other sites
Esap1    122
AH, CRAP I forgot the board would take it out here I meant this:
    
Vector<float> vectors[32];

What the do, Ive never seen that before?

PS. I was wondering why every time I typed it, it came out
Vector vectors; NOW I look like an idiot.

Share this post


Link to post
Share on other sites
Bordem    122
vector< float > thesevectors[8];
creates 8 floats in the vector container

the vector container is a premade template that works like a smart array, you can acces members with the [] operators and you can also use stuff like thesevectors[0].push_back(SomeFloat); to add a float to the end of the array.there is also a insert( ) function to add in a float in a specified location
this works for more than floats you can use any variable type that is defined.



Im Always Bored
--Bordem
ICQ: 76947930

Share this post


Link to post
Share on other sites
Bordem    122
p.s. the reason why < float > wouldnt show up is if you dont have spaces between the < and text and the next > it recognizes it as a html tag I belive

Im Always Bored
--Bordem
ICQ: 76947930

Share this post


Link to post
Share on other sites
Esap1    122
So Vector is a Template not a class, I never understood what a Template was. Care to elaborate?

Share this post


Link to post
Share on other sites
blide    122
A template allows a class to be generic. This means that a class can be a "container" for some type of object and be able to perform certain functions on it like sorting and stuff. Normally you would have to make a seperate container class for each different type of thing you want to hold, but with templates you can just make one! Here is an example:
    
//without template

class container
{
public:
int vals[100];
void sort()
{ //sort algorithm

}
};
container myc;
myc.vals[0] = 2; //....and so forth

//the problem with THAT way is that you can hold only ints

//in that...and you need to make a new one for other types


//using templates you could now make it generic:

template<typename type>
class container
{
public:
type vals[100];
void sort{}
};
container<int> myc;
myc.vals[0] = 3; //....and so forth

//how this works is that you specify a typename which is then

//replaced with the actual type used when creating an

//object, before template people used void* for generic
//data, but this method has flaws and so that is why templates
//were created!

//one thing to note is that the compiler cannot perform any

//checking into template code until it is actually used by

//passing it some type(ie int, float or even user defined).
//you could actually write garbage into code and the compiler

//wont care until you actually call that part



I hope this helps you understand what templates are...I still don''t see why they are so useful though...I have never really needed a container before...but oh well someday maybe.
Also that brings me to another point... Vector is from the STL which is basically a set of different types of containers...such as ones that allow only adding to front, other that allow more ways to access and etc...


-blide
blide@mail.com

Share this post


Link to post
Share on other sites
Bordem    122
templates are kind of big to explain in a forum you could write a lot about them so Ill just point you to an online C++ book http://www.bruceeckel.com/ThinkingInCPP2e.html
(chapter 16 volume 1) has a intro to templates.



Im Always Bored
--Bordem
ICQ: 76947930

Share this post


Link to post
Share on other sites