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-13-2011, 06:46 PM #2
Why not use static variables in the class? btw, i dont understand, you are trying to delete the int it self and not the memory address of the int? in the deconstructor? or am i missing something xD

---------- Post added at 01:46 PM ---------- Previous post was at 01:44 PM ----------

does new int declare it as a pointer!? :O
03-13-2011, 07:12 PM #3
kiwimoosical
Bounty hunter
    #include <iostream>

class Puzzlez
{

public:
void callThisFunction() const { std::cout << "Hello, World!" << std::endl; }
};

int main()
{
//call member method callThisFunction() without an Puzzlez object

return 0;
}


Call "callThisFunction()" without using:
    Puzzlez whatever = new Puzzlez();
whatever.callThisFunction();


Better pointer fun.
03-13-2011, 08:17 PM #4
Ritztro
I am a Game Developer
Originally posted by kiwimoosical View Post
    #include <iostream>

class Puzzlez
{

public:
void callThisFunction() const { std::cout << "Hello, World!" << std::endl; }
};

int main()
{
//call member method callThisFunction() without an Puzzlez object

return 0;
}


Call "callThisFunction()" without using:
    Puzzlez whatever = new Puzzlez();
whatever.callThisFunction();


Better pointer fun.


Cant I just use Puzzlez::callThisFunction() ? Or do I have to use function pointers? Because I am almost to that chapter in my book.

---------- Post added at 01:17 PM ---------- Previous post was at 01:03 PM ----------

Originally posted by TheUberFail View Post
Why not use static variables in the class? btw, i dont understand, you are trying to delete the int it self and not the memory address of the int? in the deconstructor? or am i missing something xD

---------- Post added at 01:46 PM ---------- Previous post was at 01:44 PM ----------

does new int declare it as a pointer!? :O


I don't use static variables because I want multiple ones. Not one. Unless I am misunderstanding static. Could you care to explain it to me?
03-13-2011, 08:17 PM #5
Ritztro
I am a Game Developer
Originally posted by TheUberFail View Post
Why not use static variables in the class? btw, i dont understand, you are trying to delete the int it self and not the memory address of the int? in the deconstructor? or am i missing something xD

---------- Post added at 01:46 PM ---------- Previous post was at 01:44 PM ----------

does new int declare it as a pointer!? :O


I don't use static variables because I want multiple ones. Not one. Unless I am misunderstanding static. Could you care to explain it to me?
03-13-2011, 09:45 PM #6
Originally posted by Dutch View Post
I don't use static variables because I want multiple ones. Not one. Unless I am misunderstanding static. Could you care to explain it to me?


Well static variables functions remain the same even after the function has finished for example

    
void doExample()
{
static bool FirstTime = true;
if(FirstTime)
FirstTime = false;
}


also in classes a static member is the same for every object, so if we have two objects they share any static members, good for keeping a count of how many objects, like add StaticInt++ and StaticInt- in constructors and deconstructor.

Although im not sure what happens to a static variable declared normally, like in global or in int main.
03-13-2011, 10:02 PM #7
Ritztro
I am a Game Developer
Originally posted by TheUberFail View Post
Well static variables functions remain the same even after the function has finished for example

    
void doExample()
{
static bool FirstTime = true;
if(FirstTime)
FirstTime = false;
}


also in classes a static member is the same for every object, so if we have two objects they share any static members, good for keeping a count of how many objects, like add StaticInt++ and StaticInt- in constructors and deconstructor.

Although im not sure what happens to a static variable declared normally, like in global or in int main.


Ok so I understand static but why would I use it here? I want each call to the class to have its own age.
03-13-2011, 10:03 PM #8
Originally posted by Dutch View Post
Ok so I understand static but why would I use it here? I want each call to the class to have its own age.


No im just saying, for in general, Smile, btw how does a vector work? is it simular to maps? Happy
03-14-2011, 12:06 AM #9
Ritztro
I am a Game Developer
Originally posted by TheUberFail View Post
No im just saying, for in general, Smile, btw how does a vector work? is it simular to maps? Happy


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];
03-14-2011, 12:24 AM #10
kiwimoosical
Bounty hunter
Originally posted by TheUberFail View Post
Well static variables functions remain the same even after the function has finished for example

    
void doExample()
{
static bool FirstTime = true;
if(FirstTime)
FirstTime = false;
}


also in classes a static member is the same for every object, so if we have two objects they share any static members, good for keeping a count of how many objects, like add StaticInt++ and StaticInt- in constructors and deconstructor.

Although im not sure what happens to a static variable declared normally, like in global or in int main.


It acts the same. Just accessed differently.

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

Originally posted by Dutch View Post
Cant I just use Puzzlez::callThisFunction() ? Or do I have to use function pointers? Because I am almost to that chapter in my book.

---------- Post added at 01:17 PM ---------- Previous post was at 01:03 PM ----------



I don't use static variables because I want multiple ones. Not one. Unless I am misunderstanding static. Could you care to explain it to me?


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

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

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo