Thread: trying to figure out someone's code for a plugin

  1. #1
    Registered User
    Join Date
    Jul 2006
    Location
    Kingston, Ontario
    Posts
    12

    trying to figure out someone's code for a plugin

    Hi everyone,

    I'm fairly new to C++ programming and I need to try and figure out a bunch of code that someone who worked here previously wrote. Right now I am trying to figure out this one function that I'll paste below:

    Code:
    void InitCustomNode(void *custom)
    {
    	//Called only once when the plugin is first initialized
    	//The text between the quotations gets printed in vizard's output
    	strcpy(((VizCustomNodeObj*)custom)->version,"BMLNetWalker v2.0 (WinSock2)");
    
    	//You can set the size of the data field to any value.
    	//After this function is called, Vizard will allocate the
    	//data field of the plugin to the size given here. Then you
    	//can put any values you want in the data field and the user
    	//can get those values by calling the "get" command on the custom
    	//child object in the script
    	((VizCustomNodeObj*)custom)->dataSize = 1;
    
    	//Return whether the plugin initialized successfully
    	((VizCustomNodeObj*)custom)->status = 1;
    }
    I'm particularly interested in trying to put stuff in the data field like it says above but I have no idea how to do that...Can anyone tell me what the "->" does? VizCustomNodeObj* is in a header file that looks like this:

    Code:
    #include <osg/Node>
    
    #define MAX_CUSTOM_NODE_NAME		32
    #define MAX_CUSTOM_NODE_MESSAGE		100
    #define MAX_CUSTOM_NODE_DATA		3
    typedef void (CUSTOMNODE_PROC)(void *);
    
    typedef struct VizCustomNodeObj {
    	char	name[MAX_CUSTOM_NODE_NAME];
    	char	version[MAX_CUSTOM_NODE_NAME];
    	char	status;
    	short	user[MAX_CUSTOM_NODE_DATA];
    	int		command;
    	char	mesg[MAX_CUSTOM_NODE_MESSAGE];
    	float	x, y, z, w;
    	float	*data;
    	int		dataSize;
    
    	osg::ref_ptr<osg::Node>		node;
    
    	CUSTOMNODE_PROC		*initProc;
    	CUSTOMNODE_PROC		*addProc;
    	CUSTOMNODE_PROC		*closeProc;
    	CUSTOMNODE_PROC		*commandProc;
    
    } VizCustomNodeObj;
    So I'm pretty lost with this whole thing, if anyone can point me somewhere in the right direction I'd really appreciate it. Thanks!

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by paulpars
    I'm particularly interested in trying to put stuff in the data field like it says above but I have no idea how to do that...Can anyone tell me what the "->" does? VizCustomNodeObj* is in a header file that looks like this:
    operator-> is the pointer equivilent to the dot operator. For instance:
    Code:
    struct foo {
       int bar;
    };
    
    int main() {
       foo baz;
       foo *qux = new foo;
    
       baz.bar = 5;   // The static object uses the . operator
       qux->bar = 5;  // The pointer uses the -> operator
    
       delete qux;
       return 0;
    }
    Sent from my iPad®

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    -> is a struct member access operator just like . is, but -> is used on pointers to structures.
    Code:
    int main(void)
    {
      struct foo { int a; };
      struct foo stct;
      struct foo *stctptr = &stct;
    
      // The following are equivalent:
      stct.a = 5;
      stctptr->a = 5;
    
      return 0;
    }
    If you understand what you're doing, you're not learning anything.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Code:
    VizCustomNodeObj foo;
    strcpy( foo.version,"BMLNetWalker v2.0 (WinSock2)");
    
    VizCustomNodeObj *fooPtr = &foo;
    strcpy( fooPtr->version,"BMLNetWalker v2.0 (WinSock2)");
    Both do the same thing to the same variable (namely foo).
    Use -> when you have a pointer to a struct, and . when you have a struct.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    For what it's worth, the following are also equivalent:
    Code:
    stctptr->a = 5;
    (*stctptr).a = 5;
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 04-20-2009, 07:35 AM
  2. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  3. Replies: 1
    Last Post: 03-21-2006, 07:52 AM
  4. Replies: 0
    Last Post: 02-21-2002, 06:05 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM