C# :: Passing Object As Parameter Creates A Copy Of It
Dec 17, 2014
Here is my issue: I am making a simple audioplayer in Xamarin.android but i want every time i change the track to make a crossfade effect. So im using 2 mediaplayers at the same time for the fade. The problem is that im defining one time the players and i pass the player as a parameter like this:
public MediaPlayer player = null;
public MediaPlayer player2 = null;
....
If i have to fadeout the player and start the next one im doing it like this:
if (player != null){
if (player.IsPlaying) {
cts = new CancellationTokenSource();
token = cts.Token;
FadeOut (player, 2000 ,token);
[Code] .....
So my problmem is that player and player2 remain always null. Why? i guess c# creates a copy of player and player2 and use this one. How i can pass a mediaplayer as parameter and always use only player and player2?
View 8 Replies
ADVERTISEMENT
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
Mar 16, 2013
will copy constructor does object initialization using another already created object? I understand that it can be applied for object initialization and not for assignment.Is it correct?
View 10 Replies
View Related
Aug 23, 2013
Code:
#include <stdio.h>
void ASCII_to_EBCDIC( size_t, unsigned char *);
void EBCDIC_to_ASCII( size_t, unsigned char *);
void to_ASCII(unsigned char *);
void to_EBCDIC(unsigned char *);
/* conversion tables */
static unsigned char
[Code] ....
The above snippet is for a buffer/string, where as i want to pass file name as a parameter and want function to process the file line by line?
View 2 Replies
View Related
May 1, 2013
#include <cstdlib>
#include <iostream>
struct tax_node {
char form; // tax form letter
int version; // tax form number
[Code] ....
I cannot seem to get why function print_contents will not work. The couts at the end of the program is just to test that it printed correctly. But, if I need it to print the contents such when print_contents(ptr2) is called. I think it should be tax_ptr in the parameter list but I am not quite sure.
View 1 Replies
View Related
Jan 7, 2015
gcc v.8.3 -std=gnu++11
[URL]
I'm trying to pass a function as a parameter and failing. It seems simple, until I get the error messages.
Here is the code:
class MinimalSolver {
typedef double (*func)(double sum, double char);
void driver();
[Code]....
View 2 Replies
View Related
Nov 16, 2014
I want to create events and then, functions which are subscribed to the event can access information about the event. For example, in Class 2 below, I want it to be able to access things such as touch.position, etc. of class 1.
Class 1:
public delegate void TouchEventHandler (EventArgs e);
public event TouchEventHandler TouchBegan;
public Vector2 touchPosition;
void Update () {
if (Input.touchCount > 0) {
[Code] ...
Class 2:
void OnTouchBegan (EventArgs e) {
Debug.Log ("Ran");
}
View 5 Replies
View Related
Oct 6, 2014
How come when we pass an array as a parameter to a function, the entire array is copied and is available to function?
View 1 Replies
View Related
May 2, 2013
Write a program that inputs 10 integers from the console into an array, and removes the duplicate array elements and prints the array. By removing, I mean that you should make it appear as if the elements hadn't been there. You may assume that all the integers are between 0 and 100, Write at least 1 function in addition to the main function, and pass an array into that function as a parameter. e.g.
Please enter your 10 numbers: 1 2 3 4 5 6 7 8 9 10 The array contains: 1 2 3 4 5 6 7 8 9 10
Please enter your 10 numbers: 1 1 3 3 3 6 7 8 9 9 The array contains: 1 3 6 7 8 9
Please enter your 10 numbers: 1 1 1 1 1 1 1 1 1 1 The array contains: 1
The bolded area is where I'm having trouble. How I can go about doing this, passing an array into the function as a parameter?
Here is my code:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main () {
const int MAX = 10;
int a[MAX] = {0};
int i;
[Code]...
View 5 Replies
View Related
Dec 26, 2013
Pseudocode:
template<typename T /*, some parameter for member_function */>
class Foo {
public:
void someFunction() {
T t;
t.member_fuction(...);
} }
I'm trying to make the call to T::member_function a templated value because member_function might vary by name in my scenario. Since std::mem_fn isn't a 'type', i can't do something like Foo<std::string, std::mem_fn(&std::string::clear)> foo;
I also want to take into account that member_function might have more than one parameter. That is, the first parameter will always be something known but there might be other defaulted parameters.
The only thing I can think of is to make a proxy structure, something like this:
template<typename T, T> struct proxy;
template<typename T, typename R, typename... Args, R (T::*member_function)(Args...)>
struct proxy<R (T::*)(Args...), member_function> {
R operator()(T &obj, Args.. args) {
return (obj.*member_function)(std::forward<Args>(args)...);
} }
Which would then allow me to do (for example) this:
Foo<std::string, proxy<void(std::string::*)(), &std::string::clear> >
when Foo is implemented like this:
template<typename T, typename member_function>
class Foo {
public:
void someFunction() {
T t;
member_function()(t);
} };
That implementation works for me.
View 10 Replies
View Related
Oct 6, 2013
I've been playing around with this piece of code:
#include <iostream>
#include <string>
#include <algorithm>
template <void(*funky)(const std::string&)>
void callback()
{funky("Hello World!");}
[Code] ....
But when I try to build it, I get this error on line 24:could not convert template argument 'lambda' to 'void (*)(const string&) {aka void (*)(const std::basic_string<char>&)}'|
I thought the lambda expression I wrote would decay to a function pointer matching the template parameter. I can guess that the constexpr qualifier might have changed the type, but without it my compiler complains that lambda needs to be declared as constexpr...
So is there a way to pass lambda expressions as template parameters?
Without having to use std::function
View 4 Replies
View Related
Sep 25, 2012
I created the following code to pass the the variable 'inputVoltage' by reference to the function 'input'. It certainly works when I run the program, but I dont think it is a standard way of doing it, i.e. the use of '*' and '&' is not according to convention ? Or perhaps the way did it is acceptable ?
int input (double *inputVoltage);
int main ( {
double inputVoltage;
input(&inputVoltage);
[Code]....
View 2 Replies
View Related
Jul 29, 2013
I have a class and I would like to be able to pass an extra parameter to the function that is executed.
BigInt operator / (BigInt N,BigInt D) {
...
}
is what I have now. but I would like to do something like this. so the default value for a is 10. and if the user does something like N/D (12) Then the value of a is 12.
BigInt operator / (BigInt N,BigInt D, int a=10) {
...
}
View 2 Replies
View Related
Jan 24, 2015
Does putting a ref when passing a struct to a parameter work? Say i have this code
struct struct1 {
public int i;
public void show() {
Console.WriteLine(i);
[Code] ....
The output doesn't change. The result is
0
100
500
0
View 1 Replies
View Related
May 30, 2013
I'm making a code that uses a Function pointer. The problem is, when I try to compile appears an error like:
error: no matching function for call to 'rnVector::rnVector()'
Here's part of the code:
phiFunction::phiFunction(double (*f)(rnVector), rnVector (*df)(rnVector)) {
//... Here comes the code stuff...
}
View 12 Replies
View Related
May 30, 2013
I've was trying out a function template to automatically get the type of a lambda, but it seems that it won't compile
I've tried two different ways:
1.
template<class HASHER>
auto make_unordered_map(size_t bucketCount, HASHER const && hf)
-> unordered_map<string const, HASHER>&& {
return unordered_map<string const, int, HASHER>(bucketCount, hf);
} auto x = make_unordered_map(1, [](string const& key)->size_t { return key[0]; });
2.
template<class HASHER>
auto make_unordered_map(size_t bucketCount, HASHER const && hf2)
-> unordered_map<string const, int, decltype(hf2)> {
return unordered_map<string const, int, decltype(hf2)>(bucketCount, hf2);
} auto x = make_unordered_map(1, [](string const& key)->size_t { return key[0]; });
The test code are located here:
1. [URL] ....
2. [URL] ....
They are both based on the code that is stated to work in those examples. I.e.:
auto hf = [](string const& key)->size_t { return key[0]; };
unordered_map<string const, int, decltype(hf)> m (1, hf);
View 13 Replies
View Related
Jan 14, 2015
Is there a way to copy a derived class object thru a pointer to base?
For example:
class Base { public: Base( int x ) : x( x ) {}
private: int x; };
class Derived1 : public Base { public: Derived( int z, float f ) : Base( z ), f( f ) {}
private: float f;};
class Derived2 : public Base { public: Derived( int z, string f ) : Base( z ), f( f ) {}
[Code] ....
The question is whether *B[0] would be a Derived1 object and *B[1] a Derived2 object?If not, how could I copy a derived class thru a pointer to the base class?
View 1 Replies
View Related
Jun 11, 2013
I have a class matrixType that has some overloaded operators (+, -, *, and <<). With a view to having clearly-delineated, perfectly-formatted, four-sided matrices, as shown below:
A = 1 2 3
4 5 6
7 8 9
or
A + B = 1 2 3
4 5 6
7 8 9
and NOT this jagged ones shown below:
A = 1 2 3
4 5 6
7 8 9
or
A + B = 1 2 3
4 5 6
7 8 9
,
I want a scheme in which the string literals (A, A+B, etc.) could be passed as parameters to the overloaded stream insertion (<<) operator function so that I could use the string’s length to determine how much offset from the display screen’s left to apply to each matrix’s row (by using the setw() function). However, I do know that the << operator is a binary operator, meaning the function cannot take more than two parameters: that is what compounds my problem!
View 10 Replies
View Related
Feb 20, 2012
class Base
{
char * ptr;
public:
Base(){}
Base(char * str)
[code].....
Obj1 is a derived class object where base class char pointer is initialized with "singh" and derived class char pointer is initilized with "sunil". I want to create Obj2 out of Obj1. Separate memory should be created for Obj2 char pointer (base part and derived part as well) and that should be initialized with the strings contained in Obj1.
Here the problem is: Derived class part can be initialized with copy constructor. How to initialize the base class char poniter of Obj2 with the base class part of Obj1. char pointers in
both the classes are private.
I tried using initializer list but could not succeed.
Is there some proper way to do this?
View 4 Replies
View Related
Feb 21, 2013
I have a school project in which need to create a function that takes a File Object as a Reference Parameter. Supposedly, it should allow me to read the first piece of data from others separated by a space from a file. The later be able to continue reading from the next piece of data.
I know how to set things up to read from the data file, such as using
Code:
#include <iostream> , and
Code:
ifstream inputFileName;
inputFile.open ("nameOfFile");
I also understand how to pass a variable by reference to a function. But I simply cannot put the two items together!
I tried using
Code:
void getFileInput (ifstream &); //parameter
getFileInput ("nameOfFile") // call
void getFileInput (ifstream &dataFileName)
In the function I used
Code:
ifstream inputFile;
inputFile.open (dataFileName);
I'm just not getting an understanding of this concept!
View 4 Replies
View Related
Apr 17, 2014
I am writing a program for a poker game.. I created a class to get cards and create deck.. the problem I am having now is how to deal 5 cards to an array to ve evaluated later. I want to call the array player1[5]. I tried to use pointer but I get the following error
#include <stdlib.h>
#include <iostream>
#include <ctime>
#include <algorithm>
using namespace std;
const char* FaceString[13] = {"Ace", "2", "3", "4",
"5", "6", "7", "8",
"9", "10", "Jack", "Queen", "King"};
[code]....
View 4 Replies
View Related
Dec 4, 2013
I've been working on a little project and hit a snag. I'm using nodes for a queue and stack class that were created using an existing list node class. I create an object for a student class and I want to enqueue that object.
int main() {
Queue sLine;
Customer stu;
Queue<Student &>
cLine.enqueue(cust);
}
That's basically the coding of it in main. However when I follow the error which says uninitialized reference member ListNode<Student& info>::data;
#ifndef LISTND_H
#define LISTND_H
template< class T > class List;
template< class NODETYPE >
class ListNode {
friend class List< NODETYPE >;
[Code] .....
What I may have been doing wrong? Trying to work within certain contexts.
View 2 Replies
View Related
Feb 13, 2013
This is supposed to check for valid input
if(((c.integer < 0)&&(c.numerator < 0))||(c.denominator<=0)) {
c.integer = 0;
c.numerator = 0;
c.denominator = 0;
}
if((c.integer>0)&&(c.numerator<0))
[Code] ....
And it does just fine, until it's passed into a function, then it doesn't work:
void validInput(Mixed a) {
if(((a.integer < 0)&&(a.numerator < 0))||(a.denominator<=0)) {
a.integer = 0;
a.numerator = 0;
a.denominator = 0;
[Code] ....
It works, when the object is passed, except for two cases (one where the minus sign shifts) and whenever there is a zero or a negative integer in the denominator.
Also, I'm passing the function like validInput(c);
View 4 Replies
View Related
Mar 29, 2013
Code:
void lexer(ifstream& inputfile) {
string line;
getline(inputfile,line);
[Code] ......
I am trying to pass input file between two functions. The code compiles but immediately upon running the program, there is a "bad cast" run time error.
View 9 Replies
View Related
May 5, 2014
I need to create a function in my program to open an input file and another function to open an output file and I need to use the files in other functions so Im trying to pass the stream object by reference but then i need a condition that tells the compiler not to reopen the file because then it will delete everything and make me input the file names again. Heres the two functions.
void InputFileOpen(ifstream &inFile) {
string file_name;
if(!inFile.is_open()){
cout<< "Enter the input file name: ";
cin>> file_name;
inFile.open(file_name, ios::in);
[code]....
View 4 Replies
View Related
Nov 23, 2013
Code:
#include <Data.h>
int main(int arg_count, char** argv) {
if(arg_count!=3){
cerr<<"Files misisng
";
exit(1);
[code]...
This actually should work, because it is passing address of polymorphisms object.I have tried changing prototype of test in Data.h, but failed.passing object address/pointers in C++.
View 5 Replies
View Related