May God strike down pointers

This is a discussion on May God strike down pointers within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by CornedBee I disagree. If you want to know how a linked list works, by all means write ...

  1. #16
    Registered User
    Join Date
    Mar 2008
    Posts
    71
    Quote Originally Posted by CornedBee View Post
    I disagree. If you want to know how a linked list works, by all means write one yourself. But learning the standard library of a language is as important as learning the language itself, so you should use it in non-trivial programs.
    Well...that changes things. Okay, I'll have a look as soon as I get back.


    Quote Originally Posted by CornedBee View Post
    You're thinking like a Java programmer. C++ is a multi-paradigm language. You're supposed to pick the best from every paradigm. And having stuff that is independent of an object being done by a free function is the right thing to do.
    That would fit, since I was learning Java for a while a couple of months back. In that case, where should I declare the function? In a separate header, or in the same one?

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by G4B3 View Post
    In a separate header, or in the same one?
    Where it belongs - meaning, if it's likely that you'd need it to be used in conjunection with whatever else is in that header, then there's no need for a new one.

    A header-file should be relatively "complete on it's own". Having a whole bunch of little header files that you need to include all (or nearly all) of to make a useful program serves no purpose.

    Obviously, if you have a large set of functions that belong together, then you would want to have that as a separate header, simply to keep the size of headers down. But that would be unusual.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    Registered User
    Join Date
    Mar 2008
    Posts
    71
    Okay, one more question. I have a class that's made up entirely of static variables used throughout the program, such as screen size, number of objects, and general stuff like that. I'm next to positive that a class isn't the way to group them. So how should I? Should I make them global?

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,647
    Quote Originally Posted by G4B3
    I have a class that's made up entirely of static variables used throughout the program, such as screen size, number of objects, and general stuff like that. I'm next to positive that a class isn't the way to group them. So how should I? Should I make them global?
    What exactly do you mean by "general stuff"?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by G4B3 View Post
    Okay, one more question. I have a class that's made up entirely of static variables used throughout the program, such as screen size, number of objects, and general stuff like that. I'm next to positive that a class isn't the way to group them. So how should I? Should I make them global?
    Global variables are GENERALLY not the right solution. However, if you have a bunch of things that you need to access all over the program, making a structure that holds them, and have one global instance of that structure MAY be a solution.

    Or make it a regular object, and pass it around (by (const) reference to the various places it is needed).

    Or make it a singleton object.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #21
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    However, if you have a bunch of things that you need to access all over the program, making a structure that holds them, and have one global instance of that structure MAY be a solution.
    How is that different from just having global variables?

    That's what I did in Java, but only because we can't have global variables in Java.

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cyberfish View Post
    How is that different from just having global variables?

    That's what I did in Java, but only because we can't have global variables in Java.
    It is different because you can EASILY search for all references to "theglobaldataobject", plus given a correct name, it makes it obvious that it is a global variable.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #23
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You can also prefix your globals with g_. Now, what's the difference again?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #24
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by CornedBee View Post
    You can also prefix your globals with g_. Now, what's the difference again?
    That works too - as long as it is actually done consistently.

    As I said, ideally, you should have NO global data. But sometimes it's either awkward to get the right data from the place that knows the right stuff, to where it is needed later on (and it may need to persist across multiple object instance construction/destruction), so it is after all "better" to have a global collection of variables.

    In my view, the standard method of a singleton class is pretty close to a global variable as well.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #25
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    What about putting them into a separate namespace rather than a class?
    (although, from the sounds of it, I think a class is the right place)
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    What about putting them into a separate namespace rather than a class?
    (although, from the sounds of it, I think a class is the right place)
    I don't think namespace is in any way BETTER than a class or struct, really.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of pointers to an array pointers
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 07-28-2008, 11:45 AM
  2. Hey guys..need help on pointers
    By Darkozuma in forum C++ Programming
    Replies: 5
    Last Post: 07-25-2008, 02:57 PM
  3. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  4. GOD and religion
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 10-14-2001, 05:13 PM
  5. Foundations
    By mithrandir in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 10-05-2001, 02:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21