🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: net.lang.c++
Article #288

Re: Constructors -- a redefinition.

#288
From: keith gorlen
Date: Mon, 18 Aug 1986 16:12
52 lines
2428 bytes
>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
Technical Details
Message-ID: <75@cecil.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!mnetor!seismo!elsie!cecil!keith
References: <353@comp.lancs.ac.uk>

Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: arc1-ca.pugleaf.net!utzoo!mnetor!seismo!elsie!cecil!keith
From: keith@cecil.UUCP (keith gorlen)
Newsgroups: net.lang.c++
Subject: Re: Constructors -- a redefinition.
Message-ID: <75@cecil.UUCP>
Date: Mon, 18-Aug-86 12:12:02 EDT
Article-I.D.: cecil.75
Posted: Mon Aug 18 12:12:02 1986
Date-Received: Tue, 19-Aug-86 21:01:00 EDT
References: <353@comp.lancs.ac.uk>
Organization: NIH-CSL, Bethesda, MD
Lines: 52