C++ Whats the blitz "Type" command there?
Miscellaneous Forums/General Discussion/C++ Whats the blitz "Type" command there?
hi!
anyone knows where the blitz's "type.....end_type" command is in c++?
i dont know wheter it's typedef or class or whatever.
lil help please :)
thank you!
If your using C/C++ wouldn't you be using the struct's or class's and referencing those ?
type CPlayer
field age
end type
=
class CPlayer
{
int age;
}
Don't forget Class data members are Private by default you have to declair the as public unless your using structs.
so i guess i use class {} for this use.
questions:
- whats public and private?
- structs? is this anoter option? what should i take it i want to use this EXACTLY like bb (not bmx) types?
- how do i create and destroy objects of this class?
ps: didnt know so many of you are using c++ AND bb ;D
thanks for the fast reply :)
this sounds quite ok?
i guess i always use public then...
#include "stdafx.h"
#include <iostream>
using namespace std;
class adress {
public:
int age;
};
int main() {
adress martin;
martin.age = 16;
cout << martin.age;
cin.get();
}right that way?
Structs and Classes are functionally and syntactically identical apart from their default accessibility. Members of a struct are public by default ( and the same with inheritance ) while members of classes are private by default ( and the same with inheritance. )
So it really doesn't matter, providing that you use an access modifier if you need one.
wow, so i will use struct then :)
another question even my tutorial couldn't solve:
how do i loop through a class?
in php you loop trough an array like this:
foreach (array_name as x) {
print(x);
}how ya do it in c++?
Struct and class are the same apart from struct is public by default and class is private as default:
struct adress {
int age;
};
int main() {
adress martin;
martin.age = 16;
cout << martin.age;
cin.get();
}(It's address btw)
I suggest you do some tutorials on how to use them rather than using them exactly as BB does, as there are bound to be some differences you should be aware of.
To loop through a class you must put them in either an array or a list of some kind, such as a vector:
Class* classarray = new Class[n];
for (int i = 0; i < 5; i++)
classarray[n]...;
//MUST do this at the end of your program
delete classarray;
Been a while since I did C++ so might not be exact. The big headache in C++ is the memory management. Search for how to use Vectors which are easier to use and have some auto-memory management.
yeh sure :)
some "destructors" and "friends"...
will work that trough.
still two questions left:
first: how do i loop trough a struct?
and second: what am i doing wrong here? i want to define my sexlife as a seperate class. this doesnt make sense here, but it does make sense in complicated 3d engines etc...
#include "stdafx.h"
#include <iostream>
using namespace std;
struct adress {
public:
int age;
sexlife sexlife;
};
struct sexlife {
public:
int times_doing_it;
int have_some_fun() {
times_doing_it++;
}
};
int main() {
adress martin;
martin.age = 16;
martin.sexlife.times_doing_it = 2;
cout << "Age: " << martin.age << "\n";
cin.get();
}
what do you mean by "looped throuht a struct"
For a.adress = Each adress
Print a\name$
Next
like this
It's been a while since I did anything in C++
Sound like you need to create a linked list
sudo example
stuct
{
pointer to previous address of struct object
pointer to next address of struct object
data items
}
Then you have to do a lot of pointer referencing
I'll see if I can find an example or tutorial
this is what i got so far and it gives me a weird result... something like -11348679835
#include "stdafx.h"
#include <iostream>
using namespace std;
struct adress {
public:
int age;
};
int main() {
adress martin;
adress joe;
martin.age = 16;
joe.age = 10;
// cout << "Age (martin): " << martin.age << "\n";
// cout << "Age (joe): " << joe.age << "\n";
adress* classarray = new adress[1];
adress x;
for (int i = 0; i < 2; i++) {
x = classarray[i];
cout << x.age << "\n";
}
delete classarray;
cin.get();
}also on another topic: i needa know how you can define an object of a class of the type of another class.
why do i have to create a list and delete that one anyway? isn't that perfetic somehow? hm..in blitz you dont need lists. but in bmax and c++ you seem to do need those.
well. thats all i wanted to know for now.
thank you everybody who helped me here or even read the thread !
bye :)
"blitz you dont need lists"
I believe Blitz3D has its own internal list to keep track of it objects.
*Watches Blitz users display a lack of knowledge in C++*
You're all funny.
bmax uses lists, too. and i dont like it ;D
but well. you know, i earned most of my c++ syntax knowledge from php. YAY
I'd recommend you read some tutorials on C++ pointers, arrays, structs, classes, new/delete usage, and stl. There's just so much wrong with that last example you posted, I can't begin to explain it other than saying "go learn C++".
I will, however, give you a solution:
#include "stdafx.h"
#include <iostream>
#include <list>
using namespace std;
struct Address {
typedef list<Address*> List;
typedef list<Address*>::iterator ListIterator;
public:
Address();
~Address();
static List classList;
int age;
private:
ListIterator i;
};
Address::Address()
{
classList.push_front(this);
i = classList.begin();
}
Address::~Address()
{
classList.erase(i);
}
int main() {
Address martin;
Address joe;
martin.age = 16;
joe.age = 10;
// cout << "Age (martin): " << martin.age << "\n";
// cout << "Age (joe): " << joe.age << "\n";
Address::ListIterator i;
Address *x;
for (i = Address::classList.begin(); i != Address::classList.end(); ++i) {
x = *i;
cout << x->age << "\n";
}
cin.get();
}
thanks man :)
i will look at that right now!
i am able to get myself some tutorials. jus wanted to ask a thing tutorials didnt teach me...
adress* classarray = new adress[1];
adress x;
for (int i = 0; i < 2; i++) {
x = classarray[i];
cout << x.age << "\n";
}
delete classarray;You do not need to copy it into x. This will waste time and memory. You can just say classarray[i].age
If the amount of a class won't change you can use an array method like mine. The method above is good if the class/struct will be created and deleted often.
I just thought I'd mention one (important) thing about that code - you're allocating an array in "classarray", but deleting it with the "delete" operator. This will create a memory leak and will most likely cause very cryptic memory errors later on. When de-allocating an array, ALWAYS use "delete[]", like this:
adress* classarray = new adress[1];
adress *x;
for (int i = 0; i < 2; i++) {
x = &classarray[i];
cout << x->age << "\n";
}
delete[] classarray;Also notice how I changed "x" to a pointer which is an easy way to optimize the problem mentioned by Czar Flavius above.
Why do you all actually try to teach hack style C++ usage instead of clean one? Ie instead of show him the reasons why C++ should be forbidden and replaced with a real language, you might show the stuff that makes it valuable and powerfull.
Btw: If you use classes / structs, don't forget to implement a smart pointer system or you can nearly guarantee that you will have ugly memory leaks sooner or later (as it is C++, not C, most likely sooner than later)
eh... what else do you reccomend instead of classes? or how would you do it?
For something type like, there isn't anything else than struct / classes if you want to access it that "nicely".
But I would step away from the mix usage of struct in a class like way just because they added the support to do it.
If you want a data container with functionality use classes.
But whenever you use pointer instead of assigning the object itself, you better implement smart pointers as well so the stuff gets cleaned up on its own without you doing manually delete it over and over again ...