delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/01/18/10:04:27

From: Hans-Bernhard Broeker <broeker AT physik DOT rwth-aachen DOT de>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Using new
Date: 18 Jan 2001 13:10:40 GMT
Organization: Aachen University of Technology (RWTH)
Lines: 27
Message-ID: <946q0g$k2n$1@nets3.rz.RWTH-Aachen.DE>
References: <944rio$k8r$1 AT info DOT cyf-kr DOT edu DOT pl>
NNTP-Posting-Host: acp3bf.physik.rwth-aachen.de
X-Trace: nets3.rz.RWTH-Aachen.DE 979823440 20567 137.226.32.75 (18 Jan 2001 13:10:40 GMT)
X-Complaints-To: abuse AT rwth-aachen DOT de
NNTP-Posting-Date: 18 Jan 2001 13:10:40 GMT
Originator: broeker@
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Rafal Maj <maj_rafal AT poczta DOT onet DOT pl> wrote:
> Hi,
> how to create object of my class "Class" using new, so this object will be
> placed in some allocated before memory area ?

You don't. Or, at least: you shouldn't be trying to do that.

Having two pointers of different type point to the same object will
most likely cause nothing but all kinds of headaches, in the long run.

If you really think you absolutely have to do this: don't allocate the
other pointer before you allocate the object. Then you can cast the
object pointer to the first one's type and assign it:

	char *chunk = (char *) object_ptr;

or something more C++ish (C++ has other cast operators that are
claimed to be preferrable over the classic C style cast).

The problem with this idea is that it's almost completely undefined
what the contents of string 'chunk' will be, now. Just about
everything you do with it will cause undefined behaviour of your
program, or make invalid assumptions that will bite you, later.
	
-- 
Hans-Bernhard Broeker (broeker AT physik DOT rwth-aachen DOT de)
Even if all the snow were burnt, ashes would remain.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019