Re: Default ctor, etc generated by compiler for structs?
"BobR" <removeBadBobR@worldnet.att.net> wrote in message
news:2lZci.174251$p47.59454@bgtnsc04-news.ops.worldnet.att.net...
JohnQ wrote in message...
"Ian Collins" <ian-news@hotmail.com> wrote in message...
Apply the same rules as you apply to your classes. If you need to
initialise members on creation or copy, you need constructors.
I was pondering the technique of restricting default constructor, copy
constructor, assignment operator though and its validity for structs as
well
as classes:
class A{
A(); // disallow def construction
A(const A&); // disallow copy construction
const A& operator=(const A&); // disallow assignment
public
int data_member;
A(int arg) : data_member(arg) {}
};
struct B{
private: // to be consistant with the class A
B(); // disallow def construction
B(const B&); // disallow copy construction
const B& operator=(const B&); // disallow assignment
public:
int data_member; // public by default
B(int arg) : data_member( arg ){}
};
If I disallow the stuff in struct B, it acts less like a C struct, yes?
John
A 'C++' struct is NOT a 'C' struct.
I'm trying to figure out when/if I can rely on a C++ to be "C-struct-like".
(Else I'll use the wrapper style I also posted... which may be easier than
trying to figure out what goes on behind the scenes).
It can act like a 'C' struct if that's the way you design it.
I thought all the above thingsare like a C struct. I'm just "worried" about
the layout and size for bitwise copying and such.
With the 'private:' and 'public:' I added to 'B', 'A' and 'B' are the
same.
I realize that. But you made struct B "class-like". I'd like some ensurance
the other way: making classes struct-like (or rather, knowing when they are
such).
Try using 'A' and 'B', then swap the 'class' and 'struct', try it again.
Can
you see any difference?
Of course not, i wouldn't expect to.
You can even do:
struct AA{ virtual ~AA(){} };
class BB : public AA{/*stuff*/};
BB bb;
class CC{ public: virtual ~CC(){} };
struct DD : public CC{/*stuff*/};
DD dd;
Use 'public/private/protected' for inheritance as needed, as well as
inside
the class/struct.
Try it!
Why? It's not what I'm concerned about. I'm "worried" about what the thing
looks like in memory, not elementary access specifier stuff (unless it
affects the layout making it different that a struct).
John