• Please remember to wrap not safe for work text/images in [NSFW][/NSFW] tags.
    Example: [NSFW][img]http://darklygaming.com/images/spinnysgapedass.jpg[/img][/NSFW]

Programming Q&A

Status
Not open for further replies.
Okay, time fer a light pestering.

Came across this little bugaboo in my travels and it sparked a question that I haven't been able to find an answer to...

Say I have a function

void hacks(int& b);

or

void hacks(int *b);

To call properly for the first:
int Al_Bundy;

hacks(Al_Bundy); (For the first)

hacks(&Al_Bundy); (second)

Both pass references into the functions instead of an actual value... Why two ways of doing the same thing? Are there advantages of doing it one way or the other? Or am I missing something?

I ask because I have seen both within the same open source project... yet both seem to do the same thing, no?

Kirock and Leroy BATSIGNAL!!!

Lol.

EDIT: in bold. I can't type..
 
Don't know C or C++ sorry. My only guess it that the "&" adds something extra to the variable Al_Bundy: make's it a string or a bigger string or a global string?
 

Leroy

2012 Troll of the Year
[quote1236444963=Keeripes!]
Okay, time fer a light pestering.

Came across this little bugaboo in my travels and it sparked a question that I haven't been able to find an answer to...

Say I have a function

void hacks(int& b);

or

void hacks(int *b);

To call properly for the first:
int Al_Bundy;

hacks(Al_Bundy); (For the first)

hacks(&Al_Bundy); (second)

Both pass references into the functions instead of an actual value... Why two ways of doing the same thing? Are there advantages of doing it one way or the other? Or am I missing something?

I ask because I have seen both within the same open source project... yet both seem to do the same thing, no?

Kirock and Leroy BATSIGNAL!!!

Lol.

EDIT: in bold. I can't type..
[/quote1236444963]

Its been years, and I'm hung over.

They both do the same thing, because the ampersand (&) tells the compiler that this is a call by reference (pseudo-pointer) and it essentially converts it to a pointer. You're right on the money.

On most geek forums, this would start a major argument. In _simple_ cases, for atomic datatypes (int, char), you would prefer to use the (&) because chances are, you're not going to want to babysit a pointer for an integer. For example:

int temp = 0;
int pressure = 0;
int volume = 0;

void calculateTPV( int &temp, int &pressure, int &volume );

It is so much safer to use CBR (call by reference) in this manner, and not have to worry about your goddamn pointer.

If more complicated cases, such as ADT's, (abstract data types), I'm not even sure, but you'd probably have to overload the & operator to work with your class.

For example:
TempClass *tempObj = new TempClass();

void calculateTPV( TempClass *tempObj );

Its just done by default, and because you're passing a POINTER to the object, not the object itself, when you change a value the object that you're pointing to, it is carried beyond the scope of the function call.



As a side note: Pointers are your friends. They make the most complicated problems easy. Don't fear them :) People who spout off about pointer dangers and all that mumbo jumbo, are dummies.

In my previous job, I actually wrote helper functions for my advanced pointer usage, to make sure that I standardized how I tooled with the pointer, and it made debugging so much easier. We can go off into that if you'd like.
 
First off, thanks to both of you for taking the time to look at my question! I do really appreciate it.

Just to make sure I got you right, for atomic data type I would use this

void smooth_playa(double& Kirock)

but if I had a Class defined MyClass I would use

void smooth_playa(MyClass *Leroy);

I guess this leads me to a further question. For a term project my team and I have to develop a dynamic power system load flow analysis program. Since impedances in AC are complex, I first had to develop a class to handle complex number structure and their mathematical operations. I do realize there is a complex library in C/C++ but it was faster for me to write my own than to learn the nuances of the stock library.

I called it Complex_Number.

For further analysis on the system I have to use complex number matrix operations, and so my team and I developed a Matrix class (called jMATRIX) around the Complex_Number class to handle all the necessary math functions and maintain the dynamically assigned array in which the data is held.

For both classes I overloaded operators to implement these mathematical functions and here is an example in context:

Code:
class jMATRIX {
   private:
     //Variables

   public:

   // Constructor Destructor etc.

   //Overloaded operators
   jMATRIX operator + (jMATRIX&);			// Matrix Addition
   jMATRIX operator - (jMATRIX&);			// Matrix Subtraction
   jMATRIX operator * (jMATRIX&);			// Matrix Multiplication

   etc. etc.
}

and as you know, the functions would then be:

Code:
jMATRIX jMATRIX::operator* (jMATRIX& param) {

   //code
}

Functionally I can now do complex matrix multiplication like so:

c = a * b;

So if I changed it to this:

Code:
    //In the class 

    jMATRIX operator * (jMATRIX *)

...

jMATRIX jMATRIX::operator* (jMATRIX *param) { //code}

Would I then have to do

c = &a * &b

or does the code stay the same and there is no difference?

The original jMATRIX class works fine, but I would rather learn the conventional approach so that I can develop good habits for projects that I will be doing in the future.

If you read this, thanks again Leroy and Kirock! High fives! from the dark side!

Oh, and I would definitely be interested in hearing about your helper functions for your advanced pointer usage, because I have an idea for a system but I will need to be solid on this pointer shit!

Keeripes! :) Smilin' for miles.
 
Status
Not open for further replies.
Top