473,387 Members | 1,891 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,387 software developers and data experts.

Objects and pointers to member functions.

I am trying to achieve something like the following ....

class A {
void functionA1(void (*pt2FuncB1)()) {
pt2FuncB1();
}
}

class B {
A objA;

void functionB1() {
// Do Something
}

void functionB2() {
objA.function(&functionB1)
}

}

Where an object passes a pointer to a member function to a member object function. Is there a way to make this possible with minimal changes to class A? Class A and B do not share a namespace either.
Aug 8 '08 #1
8 1426
newb16
687 512MB
something like in p33.5 -
http://www.parashift.com/c++-faq-lit...o-members.html
Or give up and use static member.
Aug 8 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
You can't pass pointers to member functions. You see, the compiler supplies the address of the object as the first argument when the member functi is called.

By calling it by pointer somewhere else, the address of the object has been lost.

There should be no scenario where you need to do this. I would ask you to explain your design in greater detail. I expect there is another solution.
Aug 8 '08 #3
Here is a description of what I am trying to do.

Well I have built a system for rendering an opengl scene but I want the the library I have built to be able to be used with no alterations to my code and minimal changes to any opengl application which may use it in future. In my planned design the user creates an instance of the rendering system class in their application simply passing the a pointer to the applications normal rendering function, then the rendering callback calls the rendering function of the rendering system instead of the usual render function.

Thanks for the responses
Aug 9 '08 #4
I thought this should be possible given that the instance of my class is also a member of the object whose function I am trying to access.

I am thinking now more along the lines that making the users application a child class of my class might be the required solution.
Aug 9 '08 #5
Savage
1,764 Expert 1GB
I thought this should be possible given that the instance of my class is also a member of the object whose function I am trying to access.

I am thinking now more along the lines that making the users application a child class of my class might be the required solution.
Don't use callbacks.It's procedural way.
C++ supports OOP,so why not make a abstract class.
User would derive and implement pure virtual functions and then pass it to your rendering system which will in turn call derived class functions to render stuff.
Aug 9 '08 #6
Don't use callbacks.It's procedural way.
C++ supports OOP,so why not make a abstract class.
User would derive and implement pure virtual functions and then pass it to your rendering system which will in turn call derived class functions to render stuff.
This would work but doesnt exactly make it easy to implement into existing OpenGL application, and seems like overkill given that all I need to call is the render routine.

I think I can use 33.5 from the C++ faq I shall have a play and let you all know.
Aug 10 '08 #7
Thanks for the help, I have ended up using something like what savage suggested it is still a little messy so I am going to try simplifying the implementation for users a bit more. Not the one liner I was hoping for but it does the trick for now ... thanks again. Kinda frustrating and odd in some ways.
Aug 11 '08 #8
weaknessforcats
9,208 Expert Mod 8TB
Read this: http://bytes.com/forum/thread793836.html.

And then read this: http://bytes.com/forum/thread674645.html.

Having got that far, research using an Abstract Factory to create your rendering system instance.

If you think it's kinda messy, then it's probably worse than you think.
Aug 11 '08 #9

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

Similar topics

20
by: Corno | last post by:
Hi all, There's probably a good reason why a const object can call non const functions of the objects where it's member pointers point to. I just don't see it. For me, that makes the the const...
9
by: Aguilar, James | last post by:
Hey guys. A new question: I want to use an STL libarary to hold a bunch of objects I create. Actually, it will hold references to the objects, but that's beside the point, for the most part. ...
8
by: Steven T. Hatton | last post by:
I've had an idea kicking around in my head regarding how to create a library of classes (templates?) that provide the same kind of functionality as do Java classes which all derive from the UBC...
6
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a...
6
by: Dan Huantes | last post by:
I was presented a problem today where a class had member variable that was an object of a templated class. The class wanted to instantiate the object as a private member variable and call a...
22
by: sandy | last post by:
I am trying to make a simulated directory structure application for a course. I am using a Vector to store pointers to my Directory objects (as subdirectories of the current object). In my...
9
by: newbie | last post by:
Let's see two different usage of an STL container. I see (2) more often when reading code over (1), dose that give any benefit or it's purely a coding preference? Also, please see the (3), I see...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
167
by: darren | last post by:
Hi I have to write a multi-threaded program. I decided to take an OO approach to it. I had the idea to wrap up all of the thread functions in a mix-in class called Threadable. Then when an...
19
by: Daniel Pitts | last post by:
I have std::vector<Base *bases; I'd like to do something like: std::for_each(bases.begin(), bases.end(), operator delete); Is it possible without writing an adapter? Is there a better way? Is...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.