C++ :: Holding Function In Variable And Changing It Dynamically

Jun 30, 2014

I need a variable that will just hold a function that I can change in the middle of the application, even to different function type with different amount of parameters ... is this even possible? At the moment I have this

struct REPLY {
string *sReply;
function<int()> special = [](){return 0;};
REPLY()

[Code] .....

And then change it back to blank function returning 0 after calling it

(reply.special)();
reply.special = [](){return 0;};

This works fine (there is no reason why it should not, right? Now, if I want to have another function, let's say

void test2(string str) {
MessageBox(NULL, str.c_str(), NULL, NULL);
}

How do I point this one to the variable special, when I want to call it like that (or something similar)

(reply.special)("test string");

is this even possible? if so, how? i tried to create function pointer (didnt compile at all) or use template (neither did this) and how to do this as I discovered functional lib just a while ago.

View 12 Replies


ADVERTISEMENT

C++ :: Setter Function Not Holding Value?

Nov 28, 2013

I have a character with x and y coordinates. When I set the coordinates in DungeonLevel, the values are gone by the time I try to get them in the main function.

Here is my main function:

#include <iostream>
#include <random>
#include <ctime>

[Code]....

View 2 Replies View Related

C++ :: Dynamically Changing Property Of A Class?

May 27, 2013

Working on a console application and I am trying to figure out a way that allows users to enter an object property along with a value. For example

class Box{
public:
int height;
int width;
int length;

[Code] .....

For example if a user inputs "height" for Name and "13" as value I would like to find a way to change ab's height to 13. I want to make this work so that one can add another class and get the same functionality.

I tried looking online and I think maps are a way to do this and I tried doing that but I don't know how to proceed after/what code to write that would allow me to change it.

Note: I can't use if else statements or hardcoding to match the input to member as I want to make this extensible.

View 1 Replies View Related

C++ :: Dynamically Changing Array Elements Inside Of Structures?

Feb 1, 2015

I have run across what I believe to be a syntax problem which I don't understand. I have a structure with two character array and I need to be able to change the size of those array dynamically. I have to use character arrays and I think the dot notation. I am not sure if I can use arrow notation. I can not do this problem using strings and vectors.

#include <iostream>
#include <cstring>
using namespace std;

[Code]....

View 4 Replies View Related

Visual C++ :: CDHtmlDialog - Changing HTML File Dynamically

Apr 9, 2014

I am using CDHtmlDialog in my MFC application to create a UI with HTML, CSS & JavaScript.

In my project there's one dialog created from CDHtmlDialog and a corresponding HTML file for that dialog.

Now during run time i want to change the contents of the HTML file.

Not just the HTML part, i want to change the CSS & JavaScript too.

So basically what am trying to do is, change the entire content of the default HTML file.

Is this possible? If yes, how can i do that?

View 14 Replies View Related

Visual C++ :: MFC Dialog / Changing Images (JPG / PNG) Dynamically In HTML Page?

Sep 6, 2013

I created a MFC dialog using CDHtmlDialog and added a HTML page with many controls and one of them is a IMAGE tag. I got a default image loaded from the hard drive displayed in the HTML page. Up to this part, everything works fine.

Now what i want to do is, dynamically change the image based on the user click on one of the HTML buttons. I don't want this image source to be hard coded in the HTML nor the image source send from the MFC app.

What i want is for the MFC app to send the image data to the HTML in some (binary) format and then the page to display the image.

The possible option i was thinking of was to read the image file which is already in my hard drive as a binary file and send the binary data across to the HTML as a memory buffer, and then use JavaScript to decrypt this binary data and display the image.

Basically what i want is for the HTML page to display a image from the memory. how to do this?

View 5 Replies View Related

C++ :: Changing Variable In For Loop

Apr 28, 2013

I have a problem with my code which I can't work out:

double Mi = 200*pow(10,30);
cout << "
Enter accreted mass increment in solar masses
";
cin >> dm;
cout << "

[Code] ....

Basically the loop works, but gives the wrong results.

I need, at the end of the loop, to sort of "redefine" Mi as "Mi + Macc". I then need it to repeat the loop, and at the end add another Macc so that Mi becomes "Mi + Macc + Macc", etc.

View 5 Replies View Related

C++ :: Changing Global Variable In A Thread

Jul 28, 2013

I have a small problem with my program. It is kinda a mess but I will try to explain you what I am trying to do. I have some threads. One of it, it attempts to detect a game client. So my code is sort of like that:

DWORD ProcessID; // The process ID of the game client
void test() {
char* text;

[Code]....

So basically, its like the variable changes, but only inside the thread... why does that happen?

View 15 Replies View Related

C++ :: Simulated Radio Station Holding A Contest - Guess Number / Sequential Search

Dec 14, 2014

I have an assignment to write the code for a simulated radio station holding a contest. The contest is for a "caller" (user input obviously) to guess a number from 1 to 500. The "randomly generated" numbers are already chosen and are being stored in a .txt file. The code is to search for number guessed by the caller and if they are wrong, next caller until a caller is correct. The end result is to display the winning number, the indexed location the number was found, and how many callers guessed. This is what I have...

Code:
#include<iostream>
#include<fstream>
using namespace std;
int sequenSrch(const int prizeArray[], int arrayLength, int searchedItem);
int guess();

[Code] ....

I'm not sure if I am even on the right track... when I pass something into the sequenSrh(); the code compiles, asks for the number to be guessed and ends.

View 2 Replies View Related

C++ :: Changing Recursive Function Into Iterator Function?

Nov 29, 2014

I had the following question in my exam and received 3 out of a possible 4 marks

Here is the question:

Assume the following recursive function:
int f(int i)
{
if(i < 2)
return i;
return f(i - 2) + f(i -1);
}

Translate this function into an iterative version:

Here is my solution:

int f(int i)
{
int prev,next,final;
prev = 0;

[Code]....

View 9 Replies View Related

C/C++ :: Changing Recursive Function Into Iterator Function

Nov 30, 2014

I am trying to translate the following recursive function into its iterative function:

int f(int i)
{
if(i < 2)
return i;
return f(i - 2) + f(i -1);
}

This is what I tried to do:

int f(int i)
{
int prev,next,final;
prev = 0;
next = 1;
if(i==1)
final = i;

[Code] .....

View 1 Replies View Related

C/C++ :: Changing Recursive Function Into Iterator Function?

Nov 30, 2014

I am trying to translate the following recursive function into its iterative function:

int f(int i) {
if(i < 2)
return i;
return f(i - 2) + f(i -1);
}

This is what I tried to do:

int f(int i) {
int prev,next,final;
prev = 0;
next = 1;

[Code] ....

View 1 Replies View Related

C :: Changing The Value Of Something From A Function

Oct 30, 2013

I was talking to someone earlier about how to change the value of something from a function, and they said what was needed was to use a ** to change something, and was wondering if I could get a walk - through of what happens. I understand a single pointer well enough, but a pointer through a pointer is kind of confusing to me. Here is a simple example.

Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
int add(int ** TOP, int * stack);

int *stack = NULL;

[Code] ....

Why is it that when the program prints the address of TOP in main, it is different than the address of TOP in the function? Is it because it is a different instance of TOP because it is in the function? When I put the number on *TOP, and come out of the function back to main, it then says the address of TOP is the number entered into *TOP, and am not sure why. And the **TOP ++ at the end I am thinking it increments malloc by 1, therefore bringing the pointer TOP up to point at the next element, or am I completely off base there?

View 7 Replies View Related

C :: Functions To Prove Variables Not Changing By Another Function

Jul 8, 2013

It is said that variables in a function cannot be changed by another function. Only by using pointers can variable values be changed. I am writing some functions to try to prove this theory, but I can't get it right.

Code:

#include <stdio.h>
#include <stdlib.h>
int main(void){
int x = 10;
printf("default x value is %d ",x);

[Code] ......

Code:

#include <stdio.h>
void try1(int x){
printf("x in try1 is %d
", x);
x++;
printf("x in try1 after ++ is %d

[Code]...

View 13 Replies View Related

C++ :: Changing Values In A Matrix Using Void Function

Jun 29, 2013

I have the following void function devised to assign "+1" or "-1" to each element of a matrix at random. While the function does what I want, when I print the values of the matrix all are set to zero:

#include <vector>
#include "aRand.h"
#include <iostream>
void initConfig(std::vector<std::vector<int> > premat, int nL, int nN) {
int * pnRand;
pnRand = 0;

[Code]...

The function pnRand_plus returns a pointer to an array of random numbers from 1 to 100, with seed time(NULL) + i. The values printed in main are zero, despite the values printed during the function run are fine (-1s and +1s).

View 2 Replies View Related

C++ ::  Calling A Function With Dynamically Defined Name

Feb 19, 2013

Is there a way to call a function whose name is defined in a file-stored-list?

In other words: The caller doesn't know in compile time the name of the function.

I'm not talking about polymorphism.

The problem is: I have a list of function names stored in a file, that may change every now and then, and I'd like to call them in the sequence they appear in that list.

View 2 Replies View Related

C :: Change / Fill List Without Changing Function Type

Feb 13, 2013

I am an IT student currently learning linked list. I have a problem with my code here. After I call addFront() my list doesn't change when I display it. How do I somewhat change/fill my list without changing the function type? I know it works on pointers still messed up with linked list.

Code:

typedef struct node *nodeptr;
struct node{int item;
nodeptr next;};
typedef nodeptr List;
}

[code]....

View 3 Replies View Related

C++ :: Changing Virtual Function Output Without Using Any Data Member

May 10, 2014

Instead of this:

#include <iostream>
struct Object {
int size; // Want to avoid this because size is (almost always) constant
Object (int s): size(s) {} // for every Object subtype.

[Code] ....

I want this:

#include <iostream>
struct Object {
virtual int getSize() const = 0;
};
struct Block: Object {
int getSize() const {return 5;} // always 5, except once in a blue moon it may change

[Code] ....

The Decorator Pattern works (getSize() can then return 6) but it is a poor choice for my program because it will cause more problems (due to many containers holding its old address, among other things. Any way to achieve this without any change of address, and without creating new storage somewhere, either inside the class or outside the class (too much responsibility to follow that stored value for the rest of the program just for this rare change, and creating a data member uses up too much memory for the many, many Block instances)?

View 5 Replies View Related

C :: How To Exclude Function Dynamically On Compile Time

Mar 6, 2013

I am working on a project, where I have to be able to exclude some code fast and dynamicly at compiletime.

I got a scheduler running and actually I just want to remove some of the tasks from it - but at compile time so that the code wont take up space in my microcontroller.

I know that I can use macros like #ifdef #endif etc. But I think that method makes the code unreadable and complicated.

How to archive such functionality a more elegant way?

View 1 Replies View Related

C++ :: Dynamically Allocated Array Of Function Pointers

Dec 13, 2013

I would like to know if this code is correct.

#include <iostream>
#include <string>
int say_one(const std::string &s) {
std::clog << s << ": One!

[Code] .....

View 7 Replies View Related

C++ :: Returning Dynamically Allocated 2D Array From Function?

Sep 18, 2014

How can I return a dynamically allocated 2d array from a function? Do I use like this:

int main(){
char **array;
array=func();
} char ** func(){
char** ptr=new char[5]; //five words
ptr[0]=new char[size of word1];
*ptr[0]=word1
........
return ptr;
}

View 8 Replies View Related

C++ :: Returning Dynamically Allocated Array From A Function

Jul 27, 2014

This a very simple program I created because I dont understand how do this. My goal is to be able to use the pointer *s5 throughout the program. For example I would to like to call other functions and pass that pointer through the function. I understand the dynamic allocation and pointers for the most part but Im confused here because the "new char[20]" variable will die after the function and I dont want it to.

#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
void testArray ( char *s5 );
int main ( int argc, char *argv[] )

[Code] .....

Also does strlen count the null terminator?

View 1 Replies View Related

C++ :: Passing Dynamically Allocated Array In Function?

Feb 13, 2013

In a program I'm working on now, i need a milti-dimensional array. To save space, I used dynamically allocated array by using pointers, something like this-

int *arr;
arr=new int[col*row];

And now i need to pass this array in a function. What are the parameters in the function declaration statement and at the function call statement?

View 4 Replies View Related

C++ :: Calling Dynamically Loaded Function From DLL Destruction Error?

Apr 18, 2013

I have two projects (Projects A and B). Project A is a dll project, defining a function called "regex".

Project B dynamically loads this DLL, and calls Project A's "regex" function via LoadLibrary/GetProcAddress.

Regex takes a pointer to an std::vector (std::vector<std::cmatch>).

When debugging ProjectB, I can see that, within the code from ProjectA (in the "regex" call), a loop that loops through the elements of the vector outputs all the elements in the vector to console as expected. But the loop in ProjectB ( which executes after ProjectA), which also loops through the vector, and, is supposed to output the elements of the vector, outputs empty strings, not, as I would expect, the same strings (which contain results), as in the loop in Project A.

How is this happening. Does this have anything to do with it being a DLL, and, maybe, somehow values/memory addresses (or something similar) of the vector/its elements being destructed across the Projects/Dlls?

Output and Code See Below:

Output Loop in A:

Un
Un

Output Loop in B: (empty) (i.e. none)

Project A DLL Header (interface.h):

#include "stdafx.h"
#include <vector>
#include <regex>
extern "C" {__declspec(dllexport) int __cdecl regex(std::string target,std::string rgx, std::vector<std::cmatch*>* matches);}

[Code].....

View 8 Replies View Related

C/C++ :: Parallelize Matrix Multiplication Function Dynamically And Statically

Oct 1, 2014

I've been trying to get my matrix multiplication program to run a few different ways. My assignments wants me to run it statically using chunks, but we're not supposed to use OpenMPs scheduler. So I'm not sure how that's possible. And secondly, we have to run it dynamically using locks/unlocks.

Static (not sure how to implement chunk)

#pragma omp section
{
printf("Thread %d doing matrix mult", threadID);
if (matrixAcols != matrixBrows) {
cout << "Error, operation not possible" << endl;

[Code]....

The code will run for a long time too. It uses as many threads as we tell it to but it never speeds up the results.

Quote
Thread 2 doing matrix mult
Thread 4 doing matrix mult
Thread 2 doing matrix mult
Thread 4 doing matrix mult
Thread 2 doing matrix mult
Thread 4 doing matrix mult
Thread 2 doing matrix mult
Thread 4 doing matrix mult
Thread 4 doing matrix mult
Thread 4 doing matrix mult
Thread 4 doing matrix mult
Thread 4 doing matrix mult
Thread 4 doing matrix mult
Thread 4 doing matrix mult

View 2 Replies View Related

C++ :: Store Function To A Variable And Call It Using That Variable?

Oct 15, 2013

I want to store few different functions to a variable for different structs/classes and then call it later using that variable, is it possible? something like

struct item {
int ID;
int special; // for function
};

item Key;
Key.special = UseKey(KEY_KING);

// now when I want to call function "UseKey(KEY_KING)" I want to use "Key.special", like this

if(iCurrRoom == ROOM_KING)
Key.special;
else if(iCurrRoom == ROOM_DRAGON)
Fireball.special;

View 5 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved