473,385 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Call by Reference not working, am i doing it wrong

Digital Don
Hi,

I am working on my OpenGL C++ assignment to create the STARCASTLE game. Most of my code uses call to function in different classes (files) without any parameters but when I need to send parameters to other classes I cannot send the reference instead a new instance is created in teh other end.

I am doing it the following way tell me if it is right...

in StarCastle.cpp in timerFunction

Expand|Select|Wrap|Line Numbers
  1.  ship.HandleShipPlasmaCollision(cannon)
ship and cannon are Global Class variables of Class type SpaceShip and Cannon in StarCastle.cpp

I want to send cannon and change a variable depending on the situation in the HandleShipPlasmaCollision() function which is in SpaceShip.cpp

The function in SpaceShip.cpp is as follows:

Expand|Select|Wrap|Line Numbers
  1.  
  2. void SpaceShip:: HandleShipPlasmaCollision(Cannon &cannon)
  3. cannon.setFiringMode(true); /*---This effects hee but when it returns to the calling funtcion I donot get the changed value there..---*/
  4. }
  5.  
How should I send the parameter so that when I change the value in the SpaceShip.cpp for the Cannon Class variable the effect is seen also in teh StarCastle.cpp?

My assignment is due tomorrow morning at 9 AM and I am stuck with this small silly problem...

Please reply soon

Thank You in advance....
Sep 6 '07 #1
2 1275
Studlyami
464 Expert 256MB
when you call the function pass a pointer to the variable

FunctionCall( *Variable);
then on the function definition
void FunctionCall(int &Variable);
Sep 6 '07 #2
Hi,

I am working on my OpenGL C++ assignment to create the STARCASTLE game. Most of my code uses call to function in different classes (files) without any parameters but when I need to send parameters to other classes I cannot send the reference instead a new instance is created in teh other end.

I am doing it the following way tell me if it is right...

in StarCastle.cpp in timerFunction

Expand|Select|Wrap|Line Numbers
  1.  ship.HandleShipPlasmaCollision(cannon)
ship and cannon are Global Class variables of Class type SpaceShip and Cannon in StarCastle.cpp

I want to send cannon and change a variable depending on the situation in the HandleShipPlasmaCollision() function which is in SpaceShip.cpp

The function in SpaceShip.cpp is as follows:

Expand|Select|Wrap|Line Numbers
  1.  
  2. void SpaceShip:: HandleShipPlasmaCollision(Cannon &cannon)
  3. cannon.setFiringMode(true); /*---This effects hee but when it returns to the calling funtcion I donot get the changed value there..---*/
  4. }
  5.  
How should I send the parameter so that when I change the value in the SpaceShip.cpp for the Cannon Class variable the effect is seen also in teh StarCastle.cpp?

My assignment is due tomorrow morning at 9 AM and I am stuck with this small silly problem...

Please reply soon

Thank You in advance....
If Cannon and ship are global variables, where and how have you instantiated the object. I feel there is some bug somewhere your are instantiating object cannon. Ideally what you have done should work fine. And evidence is that your code is compiling fine.
Sep 6 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: chirs | last post by:
I am trying to understand a piece of code. In a javascrpit file, there is a function: function ItemStyle(){ var names=; addProps(this,arguments,names,true); }; In the html file, it calls...
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
5
by: Steven C | last post by:
Hello: I've seen several recent threads on this, but I'm still not clear how to reference a *method* on one form from another. I want to add a customer in the child form, and then update the...
2
by: John McDonald | last post by:
I am working through tutorials covering developing serviced components. It is appears as though they register correctly using regsvcs and do appear in the MMC snap in. However when I attempt to...
4
by: qualitynice | last post by:
HELP :-)... I'm creating an embedded activex object in an asp.net page using the HtmlGenericControl class. I'm doing this because when I tried to embed it directly in the aspx page, it stopped...
1
by: Tom | last post by:
I am dynamically loading a DLL that I wrote that contains forms, a module, and classes. I am doing this via: Dim asm As = .LoadFrom(MyDLLPath) Dim TypeToLoad As Type = asm.GetType(MyDLLName &...
6
by: HolyShea | last post by:
All, Not sure if this is possible or not - I've created a class which performs an asynchronous operation and provides notification when the operation is complete. I'd like the notification to be...
15
by: dennis.richardson | last post by:
Greetings all. Here's a problem that's been driving me nuts for the last 48 hours. I'm hoping that someone has come across this before. I have a C# Application that reads a UDP broadcast...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.