Article View: net.lang.c++
Article #136address of constructor
From: Bjarne Stroustrup
Date: Thu, 03 Apr 1986 03:34
Date: Thu, 03 Apr 1986 03:34
32 lines
1352 bytes
1352 bytes
> Subject: Address of constructor
> Path: ..!cecil!keith (keith gorlen @ NIH-CSL, Bethesda, MD)
>
> Is there a way to get the address of a constructor function in C++?
No. Why do you want it? To pass a function that constructs an object?
If so, this trick can be used:
class base { ... };
class x : public base { ... x(int); };
class y : public base { ... y(int); };
typedef base* (*PF)(int);
some_function(PF f) {
// ...
base* p = (*f)(2); // name a new x or y
// ...
}
base* xtor(int i) { return new x(i); }
base* ytor(int i) { return new y(i); }
another_function()
{
some_function(&xtor);
some_function(&ytor);
}
A slightly more elegant version would involve pointers to member functions.
Note that destructors can be virtual so that you don't have to play games
with pointers to functions to get proper cleanup.
Technical Details
Message-ID:
<5241@alice.uUCp>Path:
arc1-ca.pugleaf.net!utzoo!watmath!clyde!burl!ulysses!allegra!alice!bsShow Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site alice.UUCP Path: arc1-ca.pugleaf.net!utzoo!watmath!clyde!burl!ulysses!allegra!alice!bs From: bs@alice.UucP (Bjarne Stroustrup) Newsgroups: net.lang.c++ Subject: address of constructor Message-ID: <5241@alice.uUCp> Date: Wed, 2-Apr-86 22:34:51 EST Article-I.D.: alice.5241 Posted: Wed Apr 2 22:34:51 1986 Date-Received: Sat, 5-Apr-86 06:35:06 EST Organization: Bell Labs, Murray Hill Lines: 32