[comp.lang.c++] Address of constructor?

kenny@uiucdcsb.cs.uiuc.edu (12/05/86)

In working over the material of the previous posting, removing any
references to overloaded functions, I discovered that I couldn't find
a syntax that cfront would accept for taking the address of a
constructor.  I want to do this because I am in a situation where I
have allocated memory by another means that I want to initialize as an
instance of a class, so I want to pass it to the constructor as
``this''.  The reason why is complicated; suffice it to say that I am
dealing with a situation where I can't rewrite the constructor and
have to use a memory allocator other than the standard _new operator.

Kevin Kenny			UUCP: {ihnp4,pur-ee,convex}!uiucdcs!kenny
Department of Computer Science	ARPA: kenny@B.CS.UIUC.EDU (kenny@UIUC.ARPA)
University of Illinois		CSNET: kenny@UIUC.CSNET
1304 W. Springfield Ave.
Urbana, Illinois, 61801		Voice: (217) 333-8740

bs@alice.UUCP (12/08/86)

Kevin Kenny writes

> In working over the material of the previous posting, removing any
> references to overloaded functions, I discovered that I couldn't find
> a syntax that cfront would accept for taking the address of a
> constructor.  I want to do this because I am in a situation where I
> have allocated memory by another means that I want to initialize as an
> instance of a class, so I want to pass it to the constructor as
> ``this''.  The reason why is complicated; suffice it to say that I am
> dealing with a situation where I can't rewrite the constructor and
> have to use a memory allocator other than the standard _new operator.

That sounds messy - and no, there is no way of taking the address of a
constructor, if you must play games with allocation and initialization
that separates the two you will have to do something like providing a
separate ``init()'' function and pass the address of it. If you cannot
even do that I think you will have to avoid user-defined allocation.

Similarly for destructors.

shopiro@alice.UUCP (12/08/86)

Try writing a public derived class with a constructor and destructor
and putting your allocation there.
-- 
		Jonathan Shopiro
		AT&T Bell Laboratories, Murray Hill, NJ
		research!shopiro   (201) 582-4179

mikem@otc.OZ (Michael Mowbray) (12/09/86)

In article <165700002@uiucdcsb>, kenny@uiucdcsb.cs.uiuc.edu writes:

> I discovered that I couldn't find a syntax that cfront would accept for
> taking the address of a constructor.  I want to do this because I am in
> a situation where I have allocated memory by another means that I want
> to initialize as an instance of a class, so I want to pass it to the
> constructor as ``this''.

Sounds contrary to C++ philosophy to me. There have been times when
I would have liked to do this, but I eventually realised that it just
reflected bad design. (Of course, I don't know the constraints applying
in this particular case.)

> The reason why is complicated; suffice it to say that I am
> dealing with a situation where I can't rewrite the constructor and
> have to use a memory allocator other than the standard _new operator.

You could possibly do the following, but I'd try to re-structure the design
in preference.


class Given {  // The class you're interested in.
	int i;
    public:
        Given(int ii); // - can't be re-written, say.
};
 
struct Aux : public Given { // Auxiliary class
        Aux(void* buf, int ii) : (ii)  // buf must have correct size & alignment
        {          
            this = (this==0) ? (Aux*)buf : this;         
        }
};
 
main()   
{
    int buffer[sizeof(Given)/sizeof(int)]; // Or use space which the
					   // non-standard allocator gave.
 
    Given *g = (Given*)(new Aux(buffer,1));  // we want a Given with i=1, say.
}


- not really recommended.