stephen@comp.lancs.ac.uk (Stephen J. Muir) (08/15/86)
I do not like the way constructors are handled at the moment. Presently, they must be declared in the public part of a class, but they cannot be invoked explicitly by the user: class x { int y; x () { y = 0; } public: //... }; x z; // ILLEGAL: x::x() is private class a { int b; public: a () { b = 0; } }; a c; main () { c.a(); //ILLEGAL: a is undefined } I think that a constructor should be allowed either in the public or private part of a class definition and, if in the public part, the program may explicitly call it. Version 1.1, 4.2 BSD, VAX-11/750. -- EMAIL: stephen@comp.lancs.ac.uk | Post: University of Lancaster, UUCP: ...!mcvax!ukc!dcl-cs!stephen | Department of Computing, Phone: +44 524 65201 Ext. 4120 | Bailrigg, Lancaster, UK. Project:Alvey ECLIPSE Distribution | LA1 4YR
keith@cecil.UUCP (keith gorlen) (08/18/86)
>I do not like the way constructors are handled at the moment. Presently, they >must be declared in the public part of a class, but they cannot be invoked >explicitly by the user: Not true. Constructors can be declared in the private part of a class, in which case objects of the class may only be created by members and friends of the class (unless a public constructor is also defined). Very handy. >I think that a constructor should be allowed either in the public or private >part of a class definition and, if in the public part, the program may >explicitly call it. This seems like an unusual thing to want to do. You're saying that you have an object (c in your example), and you want to "re-construct" it by explicitly calling one of its class's constructors on it a second time; after all, a constructor had to be called to declare it initially. I have run into this on rare occasion. In one instance I wrote a separate member function to do that portion of the construction that needed to be done explicitly, and called it both explicitly and from the constructor. This is clean and probably solves the problem in most situations. If you really need to explicitly invoke a constructor on an object, then define a constructor that takes a reference or pointer to an object of that class as an argument, assign the argument's address to "this" in the constructor, and call the constructor explicitly via new: class a { int b; public: a() { b = 0; } a(a& x) { this = &x; b = 0; } }; a c; main() { new a(c); } It isn't pretty, but it works; and, as I said before seems unusual. Perhaps you can give a specific example where this is needed frequently. -- --- Keith Gorlen Building 12A, Room 2017 Computer Systems Laboratory Division of Computer Research and Technology National Institutes of Health Bethesda, MD 20892 phone: (301) 496-5363 uucp: {decvax!}seismo!elsie!cecil!keith