Results 1 to 2 of 2

Thread: Is any memory reserved to store objects of a class that contains 0 data members?

  1. #1 Is any memory reserved to store objects of a class that contains 0 data members? 
    Forum Ph.D.
    Join Date
    Jan 2013
    Does the compiler allocate memory to store objects of a class that contains 0 data members, like CfileLoader, CmyMathFuncs, etc...?

    And how about memory usage when the functions in those classes are static, vs non-static? are there any difference, since there are 0 data members in each of them?

    Should i bother making those classes a singleton, since they already have 0 data members, and (why) should i bother if someone initializes multiple instances?

    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  


  3. #2  
    Forum Radioactive Isotope MagiMaster's Avatar
    Join Date
    Jul 2006
    I wouldn't bother making a singleton unless you specifically need a singleton.

    If there are no non-static data members, then there's no point in making non-static member functions and you should probably just make the whole class static which would avoid the question of making a singleton class.

    Memory usage in all of this is a micro-optimization and probably not worth worrying about. That said, as far as I know, C++ will still allocate at least a pointer for the object since you should still be able to make two of them and compare their pointers to show they're different. For a singleton class, you'll still get a pointer every time you make a new instance, but it'll be the same pointer every time. For a static class, you can't make an instance, so you won't have any pointers.

    Reply With Quote  

Similar Threads

  1. How do i initialize data members?
    By RamenNoodles in forum Computer Science
    Replies: 2
    Last Post: July 8th, 2014, 02:28 PM
  2. How much memory is reserved when a Boolean variable is declared?
    By RamenNoodles in forum Computer Science
    Replies: 21
    Last Post: May 11th, 2014, 07:14 PM
  3. Replies: 1
    Last Post: October 24th, 2013, 09:42 AM
  4. Calling Base Class Constructors from Derived Class c++
    By muhammadjaziem in forum Computer Science
    Replies: 1
    Last Post: March 31st, 2013, 04:39 AM
  5. tatooing crystals to store data?
    By Michael W. Bush in forum Physics
    Replies: 8
    Last Post: January 9th, 2007, 07:56 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts