I am currently writing a program involving complex numbers (converting between rectangular and polar... and eventually basic mathematical operations)
I am stuck on creating a global function that will take a pointer to an object of my class (Complex) and change the complex number stored in that object for its complex conjugate.
The main issue I am having is getting a pointer to an object, I am new to C++ and have been searching everywhere for a way to do this... I'm very confused, could somebody please just point me in the right direction... even if its just topic wise ? :)
Also do you need to change the declaration of the class Complex for this to work?
should i be creating a vector of pointers? or using new? or is there some other way i can store the pointers?
I am stuck on creating a global function that will take a pointer to an object of my class (Complex) and change the complex number stored in that object for its complex conjugate.
I'm not gonna show how to change the complex what( conjugate? ) lol I'm not sure my math is that solid to whatever you mean, but let's tackle the main issue.
1) Getting a pointer from an object
2) Defining a function that takes a pointer.
3) is there some other way i can store the pointers?
void functionThatTakesAPointer( Complex *pComplex )
{
pComplex->setA( 3.6 );
pComplex->setB( 1.7 );
// Do anything on the Complex object as you would anywhere else, just use the -> operator
}
int main()
{
// starting from line 17
{
// your for...loop
Complex newNumber(type, a, b);
newNumber.getType();
functionThatTakesAPointer( &newNumber );
myNums.push_back(newNumber);
// the rest of the code.
}
}
If all you wanna do is change the values of your object instance, you can simply iterate over the vector container and directly do your manipulation, but if you wanna play with pointers OR you need the pointer semantics( you're sure pointer is what you really NEED ), then all you have to do is change the vector to:
int main(){
char type;
double a;
double b;
vector<Complex*> myNums; // <----- See the new signature now!
string line;
ifstream myFile;
myFile.open("numbers.txt.");
while(!myFile.eof()) {
myFile >> type >> a >> b;
getline(myFile, line);
Complex *newNumber = new Complex(type, a, b); // <---- see this too?
newNumber->getType();
functionThatTakesAPointer( newNumber ); // Assume you still use the function I defined in the previous code
myNums.push_back( newNumber );
}
cout << endl;
for( int i = 0; i != myNums.size(); ++i ) delete myNums[i]; // free the used memory.
system("pause");
return 0;
}
Alternatively, rather than use a pointer, you can pass the object to the function by reference.
I just put together a very basic example which does not use your own class, but shows actually changing an object. The objects are given an initial value in sequence from 0 to 9. Then some of them are modified and the entire vector displayed.
Thank you very much for your help, I have managed to get my program working now..... :)
your answers have helped me understand pointers much better, I appreciate the time you guys have taken to answer my question!