Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Reorganizing code

Reorganizing code

Scheduled Pinned Locked Moved C / C++ / MFC
questionworkspace
2 Posts 1 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Currently I have all these objects as a function / method "local " variables.
    They can be used by other methods.

    Would it make for "better code" if I declare them as class variables and instantiate them all in the class constructor - and be done with it?

    They can be used throughout the app, so they "go away" when the app exits.

    // add common configuration
    BT_Configuration_MainWindow *BTCMW = new BT_Configuration_MainWindow();
    BTCMW->show();

    MainWindow\_Bluetoothctl \*MWB = new MainWindow\_Bluetoothctl();
    MWB->show();
    
    MWB->UpdateText();
    
    BT\_Utility\_Library \*BTUL = new BT\_Utility\_Library();
    
    L 1 Reply Last reply
    0
    • L Lost User

      Currently I have all these objects as a function / method "local " variables.
      They can be used by other methods.

      Would it make for "better code" if I declare them as class variables and instantiate them all in the class constructor - and be done with it?

      They can be used throughout the app, so they "go away" when the app exits.

      // add common configuration
      BT_Configuration_MainWindow *BTCMW = new BT_Configuration_MainWindow();
      BTCMW->show();

      MainWindow\_Bluetoothctl \*MWB = new MainWindow\_Bluetoothctl();
      MWB->show();
      
      MWB->UpdateText();
      
      BT\_Utility\_Library \*BTUL = new BT\_Utility\_Library();
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      There is no simple answer to such a question as the lifetime of a variable will depend on its usage and requirements. You need to analyse your code to decide what is best in each specific case.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups