Results 1 to 5 of 5

Math Help - C++, Return void?

  1. #1
    Member Haven's Avatar
    Joined
    Jul 2009
    Posts
    197
    Thanks
    8

    C++, Return void?

    My computing prof wrote this function in class, and I am so confused.

    Code:
    void *headptr() 
    { 
        return (void *)head; 
    }
    What exactly is being returned here. Normally, head is of class type Node (one which we built), however I get errors when I try to assign a Node to it.

    Like so
    Code:
       Node *newnode = Object.*headptr();
    Error: *headptr() cannot be uses as a member point ,since it is of type 'void*'
    Any help would be appreciated
    Follow Math Help Forum on Facebook and Google+

  2. #2
    MHF Contributor undefined's Avatar
    Joined
    Mar 2010
    From
    Chicago
    Posts
    2,340
    Awards
    1
    Quote Originally Posted by Haven View Post
    My computing prof wrote this function in class, and I am so confused.

    Code:
    void *headptr() 
    { 
        return (void *)head; 
    }
    What exactly is being returned here. Normally, head is of class type Node (one which we built), however I get errors when I try to assign a Node to it.

    Like so
    Code:
       Node *newnode = Object.*headptr();
    Error: *headptr() cannot be uses as a member point ,since it is of type 'void*'
    Any help would be appreciated
    I don't know enough pointer syntax to be able to say what exactly was causing your compiler error, but I was able to find out that the function returns a void pointer; if you do an internet search for: void pointer, you will find relevant hits.

    The main usefulness seems to be that void pointers can point to any data type and can be cast accordingly.
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Senior Member
    Joined
    Jul 2006
    Posts
    364
    What is headptr? Just a regular function, or a member function (i.e. one defined inside a class)?

    Note that in C++ there is no automatic conversion from a void * to any other pointer and vice versa, as it was true in C, so you must explicitly cast.

    Also, "head" is unlikely to be of type Node, but rather Node *, since you can't cast a class to a pointer type.

    And I'm not sure what you're trying to achieve with that assignment. If you want to allocate a new node (on the heap), use

    Code:
    Node *foo = new Node;
    otherwise, if you've already got a node pointer and you want to assign to it the value of the head, use

    Code:
    Node *head = headptr();
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Senior Member
    Joined
    Oct 2009
    Posts
    447
    Pointers to void are not used much in C++ since templates offer similar functionality.
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Member
    Joined
    Nov 2009
    Posts
    130
    You can not do this:

    Node *newnode = Object.*headptr();

    newnode is pointer of type Node.

    headptr() returns pointer of type void.

    Clearly those do not match up.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Risk vs Return
    Posted in the Business Math Forum
    Replies: 0
    Last Post: February 2nd 2010, 06:51 AM
  2. Annual Return
    Posted in the Algebra Forum
    Replies: 1
    Last Post: July 12th 2009, 01:40 PM
  3. Expected Return
    Posted in the Statistics Forum
    Replies: 1
    Last Post: June 7th 2009, 04:44 AM
  4. Return on Equity
    Posted in the Business Math Forum
    Replies: 0
    Last Post: April 14th 2009, 10:07 PM
  5. The point of no return
    Posted in the Pre-Calculus Forum
    Replies: 5
    Last Post: August 13th 2008, 12:28 AM

Search Tags


/mathhelpforum @mathhelpforum