Re: Default ctor, etc generated by compiler for structs?
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. It can act like a 'C' struct if that's
the way you design it.
With the 'private:' and 'public:' I added to 'B', 'A' and 'B' are the same.
Try using 'A' and 'B', then swap the 'class' and 'struct', try it again. Can
you see any difference?
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!
--
Bob R
POVrookie