Post: [C++] Pointer Fun
03-13-2011, 06:37 PM #1
Ritztro
I am a Game Developer
(adsbygoogle = window.adsbygoogle || []).push({}); Well I made this little (dumb) app to practice the use of pointers and working with the stack. So here is the source. BTW someone try and do this with all references now. Winky Winky

    
#include <iostream>
#include <string>
#include <vector>

using namespace std;


void AddEmployee(string name, int age, int weight, int ft, int in);
void FreePointers();

class Person
{
public:
Person()
{
m_iAge = new int; m_iWeight = new int; m_iHeight = new int[2]; m_sName = new string;
*m_iAge = 14; *m_iWeight = 125;
m_iHeight[0] = 5; m_iHeight[1] = 10;
*m_sName = "Brent";
}
~Person()
{
delete m_iAge; delete m_iWeight; delete m_iHeight; delete m_sName;
*m_iAge = 0; *m_iWeight = 0; *m_iHeight = 0; m_sName = 0;
}
//static Person * Instance() { static Person instance; return &instance; }
int * GetAge() { return m_iAge; }
int * GetWeight() { return m_iWeight; }
int * GetHeight() { return m_iHeight; }
string * GetName() { return m_sName; }
void SetAge(int age) { *m_iAge = age; }
void SetWeight(int weight) { *m_iWeight = weight; }
void SetHeight(int ft, int in) { m_iHeight[0] = ft; m_iHeight[1] = in; }
void SetName(string name) { *m_sName = name; }

private:
int *m_iAge;
int *m_iWeight;
int *m_iHeight;
string *m_sName;
};

//#define brent Person::Instance()

vector <Person *> people;

int main()
{

AddEmployee("Brent", 14, 125, 5, 11);
AddEmployee("Brad", 12, 135, 5, 5);
AddEmployee("Katie", 9, 75, 4, Cool Man (aka Tustin);


for(int i = 0; i < people.size(); i++)
{
cout << "\nName: " << *(people[i]->GetName()) << endl;
cout << "Age: " << *(people[i]->GetAge()) << endl;
cout << "Weight: " << *(people[i]->GetWeight()) << endl;
cout << "Height: " << people[i]->GetHeight()[0] << "'" << people[i]->GetHeight()[1] << "\"" << endl;
}
cin.get();

return 0;

}

void AddEmployee(string name, int age, int weight, int ft, int in)
{

Person *temp = new Person;

temp->SetAge(age);
temp->SetWeight(weight);
temp->SetHeight(ft,in);
temp->SetName(name);

people.push_back(temp);
}
void FreePointers()
{
for(int i = 0; i < people.size(); i++)
{
delete people[i];
people[i] = 0;
}
}
Last edited by Ritztro ; 03-13-2011 at 06:41 PM.
03-14-2011, 01:05 AM #11
Ritztro
I am a Game Developer
Originally posted by kiwimoosical View Post
It acts the same. Just accessed differently.

---------- Post added at 08:24 PM ---------- Previous post was at 08:22 PM ----------



To use Puzzlez::callThisFunction(), callThisFunction() would have to be static. You have to use pointers Smile

-> and * are what you use, no &.


Ok, well I thought you couldn't Puzzlez so that is why I was a little lost lol. And I know that last part because & are references correct? I just read 30 pages in my book about them Smile
03-14-2011, 01:15 AM #12
kiwimoosical
Bounty hunter
Originally posted by Dutch View Post
Ok, well I thought you couldn't Puzzlez so that is why I was a little lost lol. And I know that last part because & are references correct? I just read 30 pages in my book about them Smile


* = dereference (indirection)
& = reference
-> points to
->* pointer to member
03-14-2011, 01:42 AM #13
Originally posted by Dutch View Post
Wait? You learned maps before vectors? lol! Well I personally think maps are way more fun but anyway, vectors are like dynamic arrays. You can call them like this vector <any type> vector1; Then you can add data to it like this: vector1.push_back(data for whatever type you chose); Then you can call it like an array: vector1[0];


:O? confused Happy

so like this?

    
{
vector <int> number;
number.push_back(50);
number.push_back(100);
std::cout<<number[0]<<std::endl;
std::cout<<number[1]<<std::endl;
}


Output:
    
100
50
03-14-2011, 01:46 AM #14
Ritztro
I am a Game Developer
Originally posted by TheUberFail View Post
:O? confused Happy

so like this?

    
{
vector <int> number;
number.push_back(50);
number.push_back(100);
std::cout<<number[0]<<std::endl;
std::cout<<number[1]<<std::endl;
}


Output:
    
100
50


the output would be:
50
100
but otherwise perfect

The following user thanked Ritztro for this useful post:

TheUberFail

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo