Empty classes

This is a discussion on Empty classes within the C++ Programming forums, part of the General Programming Boards category; Code: class Comma { }; This code works fine. My question is, even if I created 3 million Comma objects, ...

  1. #1
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493

    Empty classes

    Code:
    class Comma {
    
    };
    This code works fine. My question is, even if I created 3 million Comma objects, would time or space be wasted on this class?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Code:
    #include <iostream>
    using namespace std;
    
    class Comma
    {
    
    };
    
    int main()
    {
    	cout<<sizeof(Comma)<<endl;
    
    	return 0;
    }
    Last edited by 7stud; 08-10-2003 at 04:20 PM.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    The standard says:
    Complete objects and member subobjects of class type shall have nonzero size.
    Why would you want 3 million objects that do nothing but take up space?
    My best code is written with the delete key.

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    On a related topic, shouldn't:
    Code:
    class Comma;
    work the same way? Or is it compiler-specific?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >work the same way? Or is it compiler-specific?
    It wouldn't work and is well defined by the standard.
    Code:
    class Comma;
    is an incomplete type, so you can't take its size.
    My best code is written with the delete key.

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Since each instance is unique, they will each take up some space. Seeing as each instance of the class is the same, however, why don't you make it a singleton, and just point to the singleton instance where you need this class.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Thats odd considering in the book I'm reading entitled "The C++ Standard Library a Tutorial and Reference" the author uses that syntax quite a few times like:

    Code:
    class Error;
    
    void f()
    {
    ...
    throw Error();
    ...
    }
    He also states that his code examples compiled fine with the compiler he was using...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    9
    What you have there is a forward class declaration, which is a lot like a function prototype for classes. In the posts before, s/he was trying to create an actual empty class.

    Hope that makes sense,
    Chris

  9. #9
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    Originally posted by Prelude
    Why would you want 3 million objects that do nothing but take up space?
    Good point... I was just wondering what was stored if anything in an empty class. Here's my pseudo-real-world problem:
    Code:
    //Base also doesn't have a data member
    class Func_Def : public Base {
    public:
      //... other functions like these
      static void initial();
      virtual std::string token() const { return "="; }
      virtual void evaluate(const Expression&) const;
    };
    I have a Set class which contains some amount of data (in the form of a std::string for a name, a vector array, etc.). It also contains a pointer to a Func_Def object, which I initialize to NULL.

    For instance, for a Set of Expression objects, I could have a Func_Def which makes the Set an Addition set, or a Multiplication Set, or whatever else I wanted.

    Instead of initializing my pointer to NULL, I want to initialize it to a new copy of a Comma object. Comma is derived from Func_Def, and also doesn't contain data members. I just want to know if this is a wise choice or not.

  10. #10
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Why not just initialize all of the 'Comma' pointers to a singleton instance, since you don't need any instance specific data?
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I was just wondering what was stored if anything in an empty class.
    Objects of an empty class can be constructed, destroyed, assigned and copied. So you have at the very least, a default constructor, a copy constructor, an assignment operator, and a destructor. Then there's all of the hidden equipment that comes with every object to glue everything together and keep it working.
    My best code is written with the delete key.

  12. #12
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Originally posted by Prelude
    >I was just wondering what was stored if anything in an empty class.
    Objects of an empty class can be constructed, destroyed, assigned and copied. So you have at the very least, a default constructor, a copy constructor, an assignment operator, and a destructor. Then there's all of the hidden equipment that comes with every object to glue everything together and keep it working.
    non-virtual functions don't have pointers stored in the class.

    edit:
    My test, shows sizeof(EmptyClass) returns 1. So one byte is kept. No doubt it is bogus data. Plus, struct does the same thing.
    Last edited by FillYourBrain; 08-11-2003 at 12:38 PM.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with binary file c++
    By lucky_mutani in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2009, 10:24 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  5. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 08:26 PM

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