Post: interesting c++ question
06-08-2011, 02:44 AM #1
|C++|
< ^ > < ^ >
(adsbygoogle = window.adsbygoogle || []).push({}); Why would i want to have a pointer as a member var..... try to explain using a dog class, i dont see how it could be useful, mainly because it renders the private and public parts of classes(& oop) useless...no?
(adsbygoogle = window.adsbygoogle || []).push({});
06-08-2011, 05:34 AM #2
Ritztro
I am a Game Developer
Originally posted by SLiiTH3R View Post
Why would i want to have a pointer as a member var..... try to explain using a dog class, i dont see how it could be useful, mainly because it renders the private and public parts of classes(& oop) useless...no?


Wait I am a little confused on what your saying. The first reason you could use a pointer is if you wanted something size to change dynamically without using a vector. Like so:
    #include <iostream>
#include <string>

using namespace std;

class Dog
{
public:
Dog(string name)
{
dogs = new string[1];
dogs[0] = name;
}

~Dog()
{
delete [] dogs;
dogs = NULL;
}

void AddDog(string name)
{
int current_size = (sizeof(dogs) / sizeof(string));
int new_size = current_size + 1;

//Now I want to change the arrays size
string * temp = dogs;
dogs = new string[new_size];
for(int i = 0; i < current_size; i++)
{
dogs[i] = temp[i];
}
delete [] temp;
temp = NULL;
//Now I have copied the original data into a new array of a bigger size
//Now I am going to add the new dog
dogs[new_size-1] = name;
for(int i = 0; i < new_size; i++)
{
cout << "Dogs name: " << dogs[i] << endl;
}
}

private:
string * dogs;
};

int main()
{
Dog Pack("Rufus");
Pack.AddDog("Hannah");
return 0;
}


Also some people use it so that they can save space for large amounts of data that they know will change. Ex: You have a whole bunch of verts but then you want to change those verts so to keep the same variable verts you would just delete the current data or do whatever you want to and then copy in the new data.
Kinda like this:

    float * verts;
verts = new float[300000];
//fill with data
delete [] verts;
verts = new float[500000];
//fill with different data


As you can see, with this way you save space, get to keep the same variable. Also sometimes people want function pointer as member variables (I love function pointers). Kinda like this:

    void (*FunctionToBeCalledLater) (); 

Then later they assign:

FunctionToBeCalledLater = &insert_function_here

Then use:
(*FunctionToBeCalledLater)();
//or
FunctionToBeCalledLater();


Hope that helped Smile

The following user thanked Ritztro for this useful post:

|C++|
06-09-2011, 02:23 AM #3
|C++|
< ^ > < ^ >
thank you, im not really at the level to understand some of the things you wrote(like arrays used in pointers, but im learning) and do you need to know 3dmath to learn opengl?
06-09-2011, 05:37 AM #4
Ritztro
I am a Game Developer
Originally posted by SLiiTH3R View Post
thank you, im not really at the level to understand some of the things you wrote(like arrays used in pointers, but im learning) and do you need to know 3dmath to learn opengl?


Well how old are you? You don't really need to but I have learned that it helps a lot. I am 14 and I am teaching myself linear algebra and calculus lol. So I think you can learn. I used this book: You must login or register to view this content.
Then I also learned from my cousin who is going to be an engineer.

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo