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. Constructor vs. Initialization problem

Constructor vs. Initialization problem

Scheduled Pinned Locked Moved C / C++ / MFC
questionvisual-studiohelptutorialannouncement
3 Posts 3 Posters 0 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.
  • K Offline
    K Offline
    kialmur
    wrote on last edited by
    #1

    I have a somewhat obscure question about constructors, which I have not been able to find an appropriate answer for. I will preface my question by describing the situation: I have a class representing a board that requires initialization. I know that the constructor should deal with storing addresses and other information about the board. However, should it also perform the initialization? In other words, can the constructor do more than just initialize member variables and perform an action, or is this considered bad programming practice? If you can do initializations in the constructor, how do you return the result of that initialization? I need to know whether the physical board was actually located and configured. Can I pass a pointer (for example, initResultPtr) to the constructor such that it can update a variable that can be used to verify the result of the initialization? Or should my class have a constructor that initializes all the member variables and an init() function that performs the physical board initialization and returns the result of that operation? What it is the standard practice for this problem? If you have an opinion or can refer me to an accepted answer to this problem, I would be very grateful. Kiernan

    T R 2 Replies Last reply
    0
    • K kialmur

      I have a somewhat obscure question about constructors, which I have not been able to find an appropriate answer for. I will preface my question by describing the situation: I have a class representing a board that requires initialization. I know that the constructor should deal with storing addresses and other information about the board. However, should it also perform the initialization? In other words, can the constructor do more than just initialize member variables and perform an action, or is this considered bad programming practice? If you can do initializations in the constructor, how do you return the result of that initialization? I need to know whether the physical board was actually located and configured. Can I pass a pointer (for example, initResultPtr) to the constructor such that it can update a variable that can be used to verify the result of the initialization? Or should my class have a constructor that initializes all the member variables and an init() function that performs the physical board initialization and returns the result of that operation? What it is the standard practice for this problem? If you have an opinion or can refer me to an accepted answer to this problem, I would be very grateful. Kiernan

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      the constructor can perform whatever actions, but if it becomes very far from the purpose of initializating its instance, you can consider it is a bad pratice to do so. however, if you really need to do so, then write this code in another function, and call this function inside the constructor. of course, be careful that, if the function handles some uninitialized data members, you could go into trouble !

      1 Reply Last reply
      0
      • K kialmur

        I have a somewhat obscure question about constructors, which I have not been able to find an appropriate answer for. I will preface my question by describing the situation: I have a class representing a board that requires initialization. I know that the constructor should deal with storing addresses and other information about the board. However, should it also perform the initialization? In other words, can the constructor do more than just initialize member variables and perform an action, or is this considered bad programming practice? If you can do initializations in the constructor, how do you return the result of that initialization? I need to know whether the physical board was actually located and configured. Can I pass a pointer (for example, initResultPtr) to the constructor such that it can update a variable that can be used to verify the result of the initialization? Or should my class have a constructor that initializes all the member variables and an init() function that performs the physical board initialization and returns the result of that operation? What it is the standard practice for this problem? If you have an opinion or can refer me to an accepted answer to this problem, I would be very grateful. Kiernan

        R Offline
        R Offline
        Rick York
        wrote on last edited by
        #3

        Generally speaking, I think constructors should be rather lightweight and only initialize variables. This is mostly because constructors can not return a status. Customary practice dictates that an exception is thrown on failure and that may not always be the prefered option. I prefer that any 'heavy lifting' be done in specific methods. Regarding constructors failing : http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.2[^] BTW - not that they're the final authority or anything, I just found them first in my search. :)

        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