C++ :: Member Function Pointer Assignment?

May 15, 2013

I have a class with member functions and a pointer like this:

Code:
class MyClass{
private:
typedef void(MyClass::*memFnPtr_t) ();
public:
memFnPtr_t fnptr;
void fn1();
void fn2();
};

Now in a regular function I create an instance of the class and try to assign the pointer:

Code:
MyClass mc;
mc.fnptr = &mc.fn1;

The compiler does not like it and suggests this instead:

Code:
mc.fnptr = &MyClass::fn1;

This seems to work but what if I have two instances of the class:

Code:
MyClass mc1, mc2;

How does the compiler know to distinguish between

Code:
mc1.fn1

and

Code:
mc2.fn1

when the assignment now looks identical:

Code:
mc1.fnptr = &MyClass::fn1;
mc2.fnptr = &MyClass::fn1;

View 14 Replies


ADVERTISEMENT

C++ ::  In Function Assignment Makes Pointer From Integer Without A Cast

Jul 4, 2013

The log file gives me: In function ‘memFileAlloc’ assignment makes pointer from integer without a cast..When compiling the drivers for the Matrox card in the DL580. The offending code is:

STACK_LINKAGE MEMHANDLE memFileAlloc(
UINT32 dwSize,
const char* pszFileName,
int iLine) {
void* pvChunk;
#if MEMORY_STATS

[code]...

I think the offending line is:
pvChunk = ClientMemAlloc(dwSize + sizeof(UINT32), NULL)
because that's what the log file tells me.

The system is a 16 core HP DL580 G4 with 8g RAM, RAID 0, Mandrivalinux 11.0 and the display is a Matrox Parhelia 256PCIx.

View 11 Replies View Related

C++ :: Pointer To Member Function?

Jun 6, 2012

I am trying to use "remove_if" with a predicate function inside a class. The code intends to remove the grid cells which an agent cannot move into (from among all possible cells).

Code:
void classname::function1()
{
vector<MoorePoint> neighbors;
....

[Code]....

That code would work if it was not in a class and the predicate was not a member function. However, now I receive long error messages which I guess refer to incompatibility of remove_if template with the predicate parameter (one error includes : error C2064: term does not evaluate to a function taking 1 arguments).

View 2 Replies View Related

C++ :: Assignment Operator But With Some Member Exceptions

Jan 9, 2015

The task is to use the assignment operator of a class, but change all the data except certain ones. For example, below we are to assign all Person data of 'other' except for 'name' and 'ID':

#include <iostream>
#include <string>
struct Person {
std::string name;
int ID, age, height, weight;

[Code] .....

Name = Bob
ID = 2047
Age = 38
Height = 183
Weight = 170

Name = Frank
ID = 5025
Age = 25
Height = 190
Weight = 205

Bob pretends to be Frank, but keeps his name and ID.

Name = Bob
ID = 2047
Age = 25
Height = 190
Weight = 205

But I think the way I did it is pretty lousy (note the wasted steps changing the name and ID only to revert them back? So the ideal solution should require no wasted steps, unlike the method above, and changes to what the exclusions should be should be in only one place (not two like above). Of course, we assume that Person shall have many, many data members (and constantly increasing), so that simply defining Person::operator= (const Person& other) to handle all data except for 'name' and 'ID' is out of the question.

View 3 Replies View Related

C++ :: Function Pointer To Non-static Class Member

Aug 19, 2014

I have the following problem: I am using NLOpt for optimization. The API provides functions to set the objective. This is done as follows:

double objective(const vector<double> &x, vector<double> &grad, void *data)
{
return x[1]*x[0];
}
int main(){
nlopt::opt opti(nlopt::LD_MMA,2);
opti.set_min_objective(objective,NULL);
vector<double> x(2);

[Code]....

Now I want to make the function objective a member of a class:

class Foo {
public:
double objective(...){..}
};

How can I give this method to opti.optimize? If I make objective static I can use

opti.optimize(Foo::objective,NULL);

but I do not want to have a static member. Is it possible to create an object of type Foo and give it to opti.optimize?

View 1 Replies View Related

C++ :: Calling Member Function By Object Pointer

Nov 26, 2013

I have the following piece of code.

Code:
#include<iostream>
using namespace std;
class Test {
public:
Test(){cout<<"Test"<<endl;}
void fun() {
int i=5;

[Code] ...

Compiled with g++.

Executing this give output fun5.

It is correct? I have not allocated any object and so this pointer is not created. Then how it is able to run and call the function.

View 4 Replies View Related

C++ :: Using A Member Within Pointer Function (that Is Located In Same Class) - Segfault

Jan 6, 2013

I'm currently programming a server which uses multiple threads- I have a class for one map in the game. Each map has a thread for timed events(tile regeneration, NPC regeneration, etc.), and a thread for handling NPCs(movement, combat, etc.). A basic structure of the class looks like this:

class Region {
public:
/* game values are here, they are public so
they can be accessed from outside of the class
inside of packet-handling functions and such */
int value;
void *Function();

[Code] ....

The program crashes when I use a member of the same class the function is located in- in the context I have shown about it would crash on "value++".

View 11 Replies View Related

C++ :: Pass Pointer To Member Function (polymorphic Structure)?

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

C++ :: Extractor Overloading And Member Wise Assignment For Cartesian Class

Apr 6, 2014

I am making a program with a Cartesian class. I want the user to be able to input 2 coordinates, but when I run it it doesn't ask for any values to be entered. It gives this output Please enter the first coordinates: Please enter the second coordinates:

(4.86129e-270, -1.97785e-41)
(4.86143e-270, -1.97785e-41)

Code:
#include <iostream>
#include <istream>
#include <ostream>
using namespace std;

[Code] ....

Also, I want to add a memberwise assignment function to assign the values of coord1 to coord2. How would I go about doing so?

View 7 Replies View Related

C++ :: Why Assignment Makes Pointer From Integer

Jul 27, 2013

when i compile the following program i get a compiler warning, but i don't understand why. for me the code seems to be all right and does legitimate this warning. so here is the code

// multiplication.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "crypto.h"
#define PROGNAME "multiplication"
void usage();
int isnumaber(char* str);

[code]....

View 2 Replies View Related

C/C++ :: Warning / Assignment From Incompatible Pointer Type

Apr 17, 2014

I'm working on a program and everything works except for the follow function:

void swapHex(int x, int byte1, int byte2) {
unsigned char *b1, *b2, tmpc;
printf("%d in hex is %x
", x, x);
printf("swapping byte %d with byte %d
", byte1, byte2);

[Code] ....

I get the following errors when compiling:

In function "swapHex":
warning: assignment from incompatible pointer type
warning: assignment from incompatible pointer type

View 2 Replies View Related

C++ :: Create Assignment Operator For A Class That Uses Pointer For Its Private Variable?

Mar 30, 2013

i am trying to create the assignment operator for a class that uses a pointer for it's private variable. The error is saying expected constructor, deconstructor, or type conversion before "operator. (which is the assignment operator. I have tried everything i could think of or find online and nothing has worked. below is the code for the assignment operator in the .h file and the .cpp file.

//Assignment constructor
indexList &operator=(const indexList <T> &rhs);
template <class T>
indexList<T>::indexList operator=(const indexList <T> &rhs) {
if(this != &rhs) {
numberOfElements = rhs.numberOfElements;

[Code]...

View 1 Replies View Related

C/C++ :: Value Assignment To Structure Member Inside The Structure?

Oct 7, 2014

Is it possible to assign a value to structure member inside the structure.like.....

struct control{  
char tbi:2 =0;
char res:1 =0;
};

View 6 Replies View Related

Visual C++ :: Access Member Function From Non-member Function In Same CPP File?

Dec 16, 2012

In my MFC, CMyPorpertyPageDlg is derived from CPropertyPage. How to access its member function from a nonmember function in the same CPP file?.

void Non_Member_Get_PorpertyPage()
{
CMyPorpertyPageDlg* pPageDlg = ....
}

View 4 Replies View Related

C++ :: Call To Member Function X Is Ambiguous - Overloaded Member From Header File

Feb 23, 2014

I get the following error in XCode whenever I try to access the member I created 'randomGen' in a separate class in a different header file. I have made sure to include the header file and have tried to access it through an object.

This is the code I enter when trying to access the method from randomiser.h in main.cpp. It is also an overloaded function with doubles and integers:

RandomG randomiser;
randomiser.randomGen(); // 'Call to member function 'randomGen' is ambiguous'

This is the code inside randomiser.h:

#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::endl;
class RandomG {

[Code] ....

This is the error inside xcode: [URL] ....

I have tried seperating the code for the functions in another class (main.cpp) and then running and it seems to works, so I'm not sure why I can't put everything in the .h file and then access it?

I would like it in a seperate file so it doesn't clutter my main. I am writing a game with SDL so that might be confusing and I would like the window to have a random title and other random properties, so it would be easier to use a function.

View 3 Replies View Related

C++ :: Call Member Function Inside Another Member Function?

Mar 21, 2013

If I wanted to call a member function inside another member function, how would I do that, if it's possible?

For example, if I have Find(int key) defined already and wanted to call it while i was overloading operator+.

View 2 Replies View Related

C++ :: How To Access Pointer To Class Member

Jan 10, 2015

I have the following scenario :

Code:
class test {
public :
int num;
};
int main() {
test t1;
test *ptr = &t1;
int test :: *mem_ptr = &test::num;
}

I need to access mem_ptr (pointer to a class member) through :

a. object (t1)

b. pointer to the object (ptr).

How can i do that ?

View 4 Replies View Related

C++ :: Non Constant Member Function Being Called Inside Constant Member Function?

Dec 28, 2012

#include <iostream>
class Hello {
public:
void Test() {

[Code].....

As i know a non-constant member function cant be called inside a constant member function but how the above code has been compiled successfully and giving the expected result .

View 5 Replies View Related

C++ :: Returning A Pointer To Encapsulated Data Member

Jun 22, 2014

So, I've got this class in SDL Player that has, among other things, an SDL_Texture* to hold an image that represents the player on the screen. I'd assume it's good practice to do get() and set() functions for the class; but because textures are handled via pointers, when I write a get() function I end up returning a pointer to an internal resource; which isn't good practice I hear as it "breaks" encapsulation.

Find my code below:

#ifndef PLAYER_H
#define PLAYER_H
#include "SDL.h"
#include "SDL_image.h"
#include "CTexture.h"
class Player {

[Code] .....

View 1 Replies View Related

C++ :: Delete Member Of Vector That Is Pointed By Pointer?

Apr 17, 2013

I have following:

struct Point {int* a; int b;};
vector<vector<Point> > numbers;
vector<int> example;

The numbers vector has a matrix of a sort and each of the members are pointing to one member in the example vector. A member numbers.at(2).at(3).a is pointing at example.at(3). Now, can I remotely delete a member in the example vector using the pointers? Like so:

delete (*(numbers.at(2).at(3).a));

I know there is a more convenient way to delete members, but this is a very specific case I'm working on.

View 3 Replies View Related

C++ :: Class Member Functions With Pointer Parameters?

Jan 30, 2013

Here is the assignment: (3pts) Given the following class header file, write the class’ source code for each of the accessor and mutator functions listed. (How the functions have listed their parameters, varying between passing by reference and by value.) Don’t forget to comment your code – it counts!

class Album {
private:
char * artist; // band or singer’s name
char * title; // title of the album

[code]....

The input will be an array. My questions: First, am I on the right track?

When using (char * a) for a function, for example, this is passing the address of a, correct? so then *artist=a; changes what the address of a points to?

also, the functions are bool when I would expect void. Why? for all of the set_" " functions, the parameter is *... but for set_record_label it is *&. That appears to be a mistake to me. Is that right?

what is the difference between *& and * as parameters?

View 5 Replies View Related

C :: Calling Function Via Function Pointer Inside Structure Pointer

Mar 14, 2013

I'm trying to call a function via a function pointer, and this function pointer is inside a structure. The structure is being referenced via a structure pointer.

Code:

position = hash->(*funcHash)(idNmbr);

The function will return an int, which is what position is a type of. When I compile this code,

I get the error: error: expected identifier before ( token.

Is my syntax wrong? I'm not sure what would be throwing this error.

View 3 Replies View Related

C++ :: Prevent Destructor Delete Member Pointer From Constructor

Dec 3, 2013

I have little problem which causing memory leaks.

Parent > Multiple Child(Parent parent) > Child destructor deleting parent => next Child destructor crash

Example code: without using:

class Parent {
public:
Parent() {
for(int i = 0; i < x; ++i) {
for(int j = 0; j < y; ++j)
childs[i][j] = new Child(this);

[Code] ....

If you read code, on Parent destructor i = 0 & j = 1 its going crash.

Parent will be deleted aswell, but it give me assert: _block_type_is_valid(phead- nblockuse)

View 3 Replies View Related

C++ :: Embedded Micro Controller - Pointer To Class Member

Sep 19, 2014

This project is for an embedded micro controller. In the project i wrote a class that generically services uarts. then i declare 6 objects of that class and hand them configurations for each specific uart.

internally all the objects have a send buffer of data that is still to be sent that gets populated by the object member function.

how can i make an array of function pointers that can point to the same member but of six different objects.

for example (not a working one)

class uart {
private:
struct myData {
unsigned char data[20]
int head
int tail
int count;

[Code] ....

View 9 Replies View Related

C++ :: Declaring Vector Of Pointer As Private Class Member

Mar 6, 2013

Below is a working program. Please note that the vector <int*> pointer is declared as a public member of the class A.

The problem arises when I try to make it a private (instead of public). First, let's look at the File 1 below. It compiles and works fine.

File 1: main.cpp (working fine)

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

[Code].....

View 19 Replies View Related

C++ :: Pointer-to-member Array Crashes With Virtual Inheritance?

Sep 30, 2014

I've got the following code which demonstrates a problem :

Code:
struct A {
double x[2];
double i;
};
struct B : virtual public A {
double y[2];

[code]....

I'm wondering if this is a compiler bug. Why doesn't the pointer-to-(derived)-member work for the array if it works for the non-array?

View 4 Replies View Related







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