Forcing variables to be declared at top of a function

This is a discussion on Forcing variables to be declared at top of a function within the C++ Programming forums, part of the General Programming Boards category; I was wondering if there was an option (for c++) to force the variables to be declared in the top ...

  1. #1
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704

    Forcing variables to be declared at top of a function

    I was wondering if there was an option (for c++) to force the variables to be declared in the top of the function. I try to do it myself, but I have a habbit of creating variables as I need and placing right above where I need them. Id prefer to be forced to keep the code more readible, you know, from lack of will power
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  2. #2
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    I'm going to answer off the top of my head and say no. Just catch yourself and the habit will build into you.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Do not break your habit. IMHO you are much better of declaring variables where needed rather than at the top of the function. Remember this is c++ not c and as such whenever you declare a variable that has a constructor you incur the cost of that construction/destruction. So it is only common sense to declare variables in c++ where you need them.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    You might be able to use #pragma directives or some clever #pragma coding to create a warning if your variables are ungrouped or all-over.

    This wouldn't be that simple though.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 09:56 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM

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