LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-23-2010, 02:22 PM   #1
brazilnut
Member
 
Registered: Nov 2007
Posts: 113

Rep: Reputation: 16
Function pointers to class members, cpp


Hi, i'm a bit stuck playing with the following class setup for glut. The actual issue is with the function pointers.

Basically the following code is the opengl redbook cube example slightly modified...

Quote:
/*
* Copyright (c) 1993-1997, Silicon Graphics, Inc.
* ALL RIGHTS RESERVED
*/
Code:
#include <GL/glut.h>
#include <stdlib.h>

using namespace std;

class myApp
{

public:
	
	myApp(int argc, char** argv)
	{
		glutInit(&argc, argv);
		glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
		glutInitWindowSize (500, 500); 
		glutInitWindowPosition (100, 100);
		glutCreateWindow (argv[0]);
		init ();
		glutDisplayFunc(display); 
		glutReshapeFunc(reshape);
		glutKeyboardFunc(keyboard);
		glutMainLoop();
	}
	
	void init(void) 
	{
		glClearColor (0.0, 0.0, 0.0, 0.0);
		glShadeModel (GL_FLAT);
	}
	
	void display(void)
	{
		glClear (GL_COLOR_BUFFER_BIT);
		glColor3f (1.0, 1.0, 1.0);
		glLoadIdentity();
		gluLookAt(0.0,0.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0);
		glScalef (1.0, 2.0, 1.0);
		glutWireCube (1.0);
		glFlush ();
	}
	
	void reshape (int w, int h)
	{
		glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
		glMatrixMode (GL_PROJECTION);
		glLoadIdentity ();
		glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
		glMatrixMode (GL_MODELVIEW);
	}
	
	void keyboard(unsigned char key, int x, int y)
	{
		switch (key) {
			case 27:
				exit(0);
				break;
		}
	}

};

int main(int argc, char** argv)
{
	myApp app(&argc, argv);
	
	return 0;
}
Thanks...


oops...
Code:
g++ -Wall -I/usr/include/ -L/usr/X11R6/lib -lX11 -lXi -lXmu -lglut -lGL -lGLU -lm cube.cpp -o cube

Last edited by brazilnut; 05-23-2010 at 02:23 PM.
 
Old 05-23-2010, 05:39 PM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Non static member functions of a class cannot be called without using an object of that class.

So the address of such a function is not the same data type as the address of an ordinary function whose signature looks the same.

If you don't want to involve an object of the class, then you can make the functions static class members, then the data type of the address of the function would be the same as the data type as that of an ordinary (non member) function with the same signature.

If you do want to involve an object of the class, things get a bit more complicated. You would need to think through the desired behavior before it would make any sense to start digging into the tricky syntax of bind member function.

Last edited by johnsfine; 05-23-2010 at 05:41 PM.
 
1 members found this post helpful.
Old 05-26-2010, 10:18 AM   #3
brazilnut
Member
 
Registered: Nov 2007
Posts: 113

Original Poster
Rep: Reputation: 16
Hi,

Thanks for your reply (I have thanked, even though it doesn't seem to work for me)...

I did try the static method before posting but it didn't work, I kept on getting the same errors. After your post I tried statics again (using above demo) and hey it worked, so I expanded out into properly defined classes (h + cpp) and I got the errors again...

After a little more searching I see that you don't use the static keyword on the function body, only the declaration.

So yes, learnt something new...

Thanks again :-)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
CPP: Multiple pointers to one heap object problem. RHLinuxGUY Programming 2 03-27-2008 09:01 AM
C++ templated Node class: pointers to different instantated class types jhwilliams Programming 3 08-20-2007 06:20 PM
CPP: Is it possible to use the members of an inherited class? RHLinuxGUY Programming 5 04-30-2007 02:48 AM
Inheriting class members (Qt C++, QApplication class) jtshaw Programming 2 01-15-2004 11:52 AM
cpp: pointers Wonko the Sane Programming 3 11-17-2003 03:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration