This code works fine. My question is, even if I created 3 million Comma objects, would time or space be wasted on this class?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?Code:class Comma { };
Code:#include <iostream> using namespace std; class Comma { }; int main() { cout<<sizeof(Comma)<<endl; return 0; }
Last edited by 7stud; 08-10-2003 at 03:20 PM.
The standard says:
Why would you want 3 million objects that do nothing but take up space?Complete objects and member subobjects of class type shall have nonzero size.
My best code is written with the delete key.
On a related topic, shouldn't:
work the same way? Or is it compiler-specific?Code:class Comma;
"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
>work the same way? Or is it compiler-specific?
It wouldn't work and is well defined by the standard.
is an incomplete type, so you can't take its size.Code:class Comma;
My best code is written with the delete key.
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.
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:
He also states that his code examples compiled fine with the compiler he was using...Code:class Error; void f() { ... throw Error(); ... }
"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
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
Good point... I was just wondering what was stored if anything in an empty class. Here's my pseudo-real-world problem:Originally posted by Prelude
Why would you want 3 million objects that do nothing but take up space?
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.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; };
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.
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.
>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.
non-virtual functions don't have pointers stored in the class.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.
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 11:38 AM.
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter