Easy syntax question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raistlinx
    New Member
    • Jul 2007
    • 14

    Easy syntax question

    Is there a difference between

    void DLNode::setNext (DLNode* node){} or int* a

    and

    void DLNode<Type>::s etNext(DLNode *node){} or int *a


    I've seen both used, is it just a style thing or is there some difference?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Use this when DLNODE is a class

    void DLNode::setNext (DLNode* node){}

    Use this when DLNode is a class template:

    void DLNode<Type>::s etNext(DLNode *node){}

    I wasn't use what you meant by or int* a.

    Comment

    • raistlinx
      New Member
      • Jul 2007
      • 14

      #3
      Thank you. What I was really looking for though was an explanation on the difference between something like int* a and int *a. Why is the '*' sometimes on the variable name and sometimes on the type?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Ah, now I see.

        In C, you can declare multiple variables on one line:

        int a,b,c,*d;

        a is an int
        b is an int
        c is an int
        *d is an int

        Without declaring a,b and c you have:

        int *d;

        This is fine in C++. But where C programmers see *d as an int, C++ programmers see d as a pointer to an int object sso they like to code:

        int* d;

        Actually, since whitespace doesn't count, you could also:

        int * d;

        So, it all boils down to a coding convention.

        Comment

        Working...