C/C++ :: Const Pointer Pass By Reference In Print Function
Apr 21, 2014
I am trying use a print function to print out data in a struct. My questions are:
1. I have to use pass by reference. For the print function, I am passing the struct pointer as a reference, however, I don't want the print function to accidentally change anything. How can I make it use const to ensure that?
2. The deleteprt function doesn't look right to me. I feel like it should just be delete ptr not delete [] ptr.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;
struct Inventory {
[Code] .....
View 9 Replies
ADVERTISEMENT
Oct 4, 2013
I don't understand how my code not run.
#include "stdafx.h"
#include<iostream>
using namespace std;
struct student{
char name[30];
char birthday[20];
char homeness[50];
float math;
[Code] ....
View 3 Replies
View Related
Jun 20, 2014
i really don't know why has a error in my code, that pass a pointer of pointer (name of a matrix with 2 dimensions). Here is the source code of a simple example where appears segmentation fault when execute (but compiles normal):
#include <stdio.h>
#define LINHAS 3
#define COLUNAS 5
float a[LINHAS][COLUNAS];
void zeros(float **p,float m, float n){
int i,j;
for(i=0;i<m;i++)
[Code]...
View 6 Replies
View Related
Feb 9, 2015
//program to form a header file
/* using pass by reference in pointer */
#include <iostream>
#include<math.h>
[Code].....
View 14 Replies
View Related
Jan 24, 2013
What is the difference in pass by pointer and pass by reference? As per my understanding, there is no difference much.If a function accepts pointer, then NULL check can be performed.other than this i'm not able to see any big difference..
View 3 Replies
View Related
Mar 14, 2013
I have a struct which has an array inside of it:
struct someStruct{
int structArray[999];}
Now when I want to access that array, I have the following:
ptrSomeStruct->structArray[someIndex];
But now I want to pass structArray to this function by reference so that it can adjust the array as needed and I can continue to use the array back in my caller function:
void adjustArray(void *& someArray){}
How do I accomplish this?
View 2 Replies
View Related
Jul 30, 2014
I just want to know if there is any real difference between the two below, if yes, when would i use one over the other? I would thought the "&" is pointless in below function, as far as the data is concerned.., the only things is with "&", if the pointer address value is changed in Test function, it will affect the caller's copy of data. Both function should behave the same if data is changed.
Code:
Between
void Test(QSharedPointer<Data> data)
{
}
and
void Test(QSharedPointer<Data> & data)
{
}
View 6 Replies
View Related
Sep 19, 2014
Due to the nature of this requirement, I've made a very minimal example, which would adequately solve my issue, without resorting to use of pointers or copy constructors.
Basically I'm trying to pass an object as a reference to the template function, rather than a copy as it's seeing. I'm needing to do this without editing Obj::Call to accommodate a reference as its first parameter, as it'd break other calls.
You'll notice in the following code the object will be destroyed upon passing, while the object defined is still in-scope due to the infinite end loop.
#include <iostream>
#include <string>
using namespace std;
class Obj {
public:
string name;
[Code] ....
In the past I tried ref(), which appeared to stop this happening, however it created a blank copy of the object instead.
View 1 Replies
View Related
Nov 27, 2014
i have a project where i create a dice game, the user rolls 2 dice and the computer roles 2 dice. the player that wins 3 out of 5 rolls wins the game. I have completed the requirements, but i wanted to create a pass by value function for "void Dice()", I'm not too sure how that works?
Code:
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
//creating my variables and their values
int compinput;
[Code] .....
View 5 Replies
View Related
Jan 23, 2013
I've been given an assignment with the below questions.
1. What is the difference between pass by reference & pass by pointers?
2. What is the use of the initialization list in the constructor?
3. What is meant by a reference & its advantage?
4. Class has a reference, pointer and a const. Is it possible to write the copy constructor & assignment operator overloading funciton? how? ( Since reference is there, I'm not sure on how to write for it)
5. Example for a variable decleration and definition? (I know for function but for variable don kw how)
6. static and const static what is the difference??
View 1 Replies
View Related
Apr 12, 2013
i need to pass myboard.board (board is in the class Cboard and it is an array of int) to a function in a class called piece however this is troubling . i need to pass it as pointer os that i could change its value here under is my code.
main.cpp Code: #include<iostream>
#include"board.h"
#include "pieces.h"
[Code].....
View 7 Replies
View Related
Jan 2, 2014
I'm having a problem understanding something with pointers. I was trying to pass a pointer into a function in MSVC-2013, like
char* charptr;
and then calling
myfunct(charptr);
and then inside the function i would set charptr equal to another char ptr, simply like
charptr = anothercharptr;
But this actually caused a compile failure in MSVC, saying charptr is being used without being initialized. in Code::Blocks it just gives buggy output.
I solved this issue by calling the function like
myfunct(&charptr);
and declaring the function like
myfunct(char**);
and then I had to dereference the charptr in the function when assigning it to another ptr, so
*charptr = anothercharptr;
It seems like you should be able to just pass a ptr into a function and change its address to that of another pointer? My main question is really, what is the value of a pointer? I thought the value of a pointer was just the memory address it contains. But then I had to reference it to pass it into the function.
What is the difference between the value of the char* charptr written as either charptr and &charptr?
View 1 Replies
View Related
Sep 25, 2014
#include <iostream>
using namespace std;
void myfunc(int* ); // what do i put in these parameters to accept a mem loc of a pointer
int main () {
int x = 5;
[Code] .....
SOLUTION:
#include <iostream>
using namespace std;
//Purpose to create a function that returns a pointer to a pointer
int** myfunc(int**);
int main () {
int x = 5;
[Code] ....
View 3 Replies
View Related
Jun 13, 2013
void extf(int a) { }
template<typename P>
struct A {
// 1
template< void (*F)(P) >
static void call(P arg) {
[Code]...
Why it is not working? What would be a proper way to pass function pointer as a template parameter?
View 6 Replies
View Related
Jan 3, 2015
I am trying to create a callback system for input events in my game engine.
Here is a cut down version of the EventManager.h file
#include "Controls.h"
#include "Object.h"
enum MouseEventType{PRESSED, POINTER_AT_POSITION, PRESSED_AT_POSITION };
[Code].....
This works if the function pointer being passed to the event manager is not a member function.
I have two other classes Scene and Object that could potentially use this EventManager to create callback events. Scene and Object are both pure virtual objects. How can I pass a pointer to a member function of the child classes of both Scene and Object? I am fine with just having two separate watchEvent functions for Scene and Object but how do I pass the type of the Object or the type of the Scene? The child classes are unknown as they are being created by someone using this game engine.
For example, if I want to make a player object it would be something like
class PlayerObject : public Object{...};
Now that PlayerObject type has to find its way to PlayerObject::functionToCall(). I think I need templates to do this but, since I never used them before
This is how I intend to use this
class OtherScene : public Scene{
void p_pressed(void){
//pause
}
[Code].....
View 6 Replies
View Related
Jul 2, 2014
// explain difference between pass by value and pass by reference
void addOne(int i)
{
i++;
}
void addOne(int& i)
{
i++;
}
View 2 Replies
View Related
Dec 8, 2014
I have to write an example in which you use a function having as argument a pointer passed by reference in C++. Can you provide an example like this:
funz.h : void funz( int *&a );
funz.cpp : ? (1)
main.cpp:
#include "funz.h"
#include <iostream>
[Code]...
as I write in (1) and (2) ?
View 5 Replies
View Related
Feb 11, 2013
I am trying to use the Singleton design patterno on Linux, but it is not working:
in the .h I have:
Code:
public:
static ErrorH& Instance();
private:
ErrorH() {};
In the .cpp I have:
Code:
ErrorH& ErrorH::Instance() {
static ErrorH& self = ErrorH();
return self;
}
The errors I get are:
Code:
g++ --g -c ErrorH.cpp -o ErrorH.o
ErrorH.cpp: In static member function "static ErrorH& ErrorH::Instance()":
ErrorH.cpp:9: error: invalid initialization of non-const reference of type "ErrorH&" from a temporary of type "ErrorH"
make: *** [ErrorH.o] Error 1
This code works on Windows, how can I get it to work on Linux?
View 2 Replies
View Related
Sep 19, 2014
Basically I'm trying to pass an object as a reference to the template function, rather than a copy as it's seeing. I'm needing to do this without editing Obj::Call to accommodate a reference as its first parameter, as it'd break other calls.
You'll notice in the following code the object will be destroyed upon passing, while the object defined is still in-scope due to the infinite end loop.
#include <iostream>
#include <string>
using namespace std;
class Obj {
public:
string name;
Obj(string name): name(name) {cout << "create " << this << endl;}
[code]....
In the past I tried ref(), which appeared to stop this happening, however it created a blank copy of the object instead.
View 3 Replies
View Related
May 27, 2014
i want to store reference to a const object in my class as a member variable, as follow:
I basically want a readonly reference to |Data| in Device object.
Code:
class Device {
Device(const QList<QSharedPointer<Data>> & dataList) : _listRef(dataList) {
} protected:
const QList<QSharedPointer<Data>> & _listRef;
}
This does not allow me to initialize _listRef as something like NULL when it is not applicable.Also, i must change all my constructors and its child class to include an initialization of _listRef!!
What is the alternative? Is pointer the nearest? which of the following should be used?
Code:
const QList<QSharedPointer<Data>> * _listRef;
or
const QList<QSharedPointer<Data>> *const _listRef;
or
const QSharedPointer<QList<QSharedPointer<Data>>> _listRef; ????
View 7 Replies
View Related
Jul 20, 2013
The printArray function should take in the dynamically created array and the size of the array as parameters. It should print out the contents of the array.
#include <iostream>
#include <string>
using namespace std;
[Code].....
My problem is that how to write the code to print the array using pointers. I've been stuck for awhile trying to figure it out.
View 2 Replies
View Related
Apr 17, 2013
i think i understand both concept. i know that pass by value is that the function receiving those values makes actually a copy of those parameters. Passing by reference makes the variable in the main function to actually see those changes in the other function, instead of a copy, and apply them to the variable in the main function. my question is, why would i pass it by reference if i just can make a return type function.
View 5 Replies
View Related
Nov 27, 2014
I need to put my Dice() function as pass by reference and something as a pass by value, it can be in the same function.
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
[Code].....
View 6 Replies
View Related
Mar 17, 2013
i need to return a struct pointer dynamically allocated inside a function call void function() which is done using 'out parameters' in following code
struct my_struct {
int x;
} void my_function( my_struct** result ) {
my_struct* x = new my_struct{ 10 };
//...
*result = x;
}
Now i have a doubt, so if i want to print the return value from struct pointer, should i need to print it in the void function() or in the caller the function...
View 3 Replies
View Related
Nov 25, 2013
(Pass-by-Value vs. Pass-by-Reference)
Write a complete C++ program with the two alternate functions specified below, each of which simply triples the variable count defined in main. Then compare and contrast the two approaches. These two functions are
a) function tripleByValue that passes a copy of count by value, triples the copy and returns the new value and
b) function tripleByReference that passes count by reference via a reference parameter and triples the original value of count through its alias (i.e., the reference parameter).
View 7 Replies
View Related
Apr 10, 2014
I need to pass an array of 10 instances of a custom class to a function. The snippets of code are posted below. How would I do this right?
The prototype:
Code:
int Search(Vertex vertex[], ofstream &outfile);
The implementation in the main function.
Code:
Search(vertex[10], outfile);
View 2 Replies
View Related