Hi, slowly transitioning from C to C++, I decided to remodel a
struct/union (i.e. type identifier as first field, union of variant
types) as a class + subclasses. Switching functions are replaced by
virtual functions. So far so good.
Now what I used to do is have a struct, set its type and union member,
and return a pointer. I.e. I initialized the struct appropriately and
returned a reference. Now I'd like to do that in C++ (right now I
construct a new subclass with "new" and delete it after every call,
because the instance is only needed very shortly anyway; seems ugly to me).
I tried to define a union that contains all subclasses (that I will ever
use) of the base class, but C++ complains that it can't have objects
with constructors or destructors inside a union.
Is there any way to have a static piece of storage, and to return
references/objects(by value) or pointers to this piece of storage, while
also initializing it (i.e. myunion.objectB = ObjectB(); return
myunion.objecTB; or something like that)? In C I didn't need malloc()
in this case; I don't see why I should need new() now. 9 2126
Could you please give some supporting code?
I'm afraid I couldn't follow what you're saying (starting from
the 2nd paragraph), even though I read the thing at least 3
times ;-)
Ulrich Hobelmann wrote:
Hi, slowly transitioning from C to C++, I decided to remodel a
struct/union (i.e. type identifier as first field, union of variant
types) as a class + subclasses. Switching functions are replaced by
virtual functions. So far so good.
Now what I used to do is have a struct, set its type and union member,
and return a pointer. I.e. I initialized the struct appropriately and
returned a reference. Now I'd like to do that in C++ (right now I
construct a new subclass with "new" and delete it after every call,
because the instance is only needed very shortly anyway; seems ugly to me).
I tried to define a union that contains all subclasses (that I will ever
use) of the base class, but C++ complains that it can't have objects
with constructors or destructors inside a union.
Is there any way to have a static piece of storage, and to return
references/objects(by value) or pointers to this piece of storage, while
also initializing it (i.e. myunion.objectB = ObjectB(); return
myunion.objecTB; or something like that)? In C I didn't need malloc()
in this case; I don't see why I should need new() now.
In article <4g*************@individual.net>, u.*********@web.de
says...
[ ... ]
Is there any way to have a static piece of storage, and to return
references/objects(by value) or pointers to this piece of storage, while
also initializing it (i.e. myunion.objectB = ObjectB(); return
myunion.objecTB; or something like that)? In C I didn't need malloc()
in this case; I don't see why I should need new() now.
Yes -- it's called placement new. You do something like:
static char buffer[4096]; // assume that's big enough...
your_class *object = new(buffer) your_class;
When you're done with this object, you don't delete it -- instead,
you call its dtor directly:
object->~your_class();
As far as being anything like malloc goes: no, it's not. malloc does
one thing: allocates raw memory. The usual use of new does two
things: allocates some memory (about like malloc), and then creates
an object in that memory.
Placement new does only the _second_ part of that. It doesn't
allocate any memory -- it just creates an object in the memory you
designate. You're still creating a new object, so (at least to me) it
makes perfect sense to use the "new" keyword, even though you're not
doing anything that corresponds to what malloc does.
--
Later,
Jerry.
The universe is a figment of its own imagination.
Hi,
Yes, look up 'in place new' on parashift.
Here is a piece of my Variant type code:
// in the header
union {
Int8 Char;
Int64 Long;
UInt8 UChar;
UInt64 ULong;
double Double;
bool Bool;
char String [ sizeof( std::string ) ];
char Map [ sizeof( std::map<UVar*,UVar*, UFindVar) ];
char SRefPtr[ sizeof( MSRefPtr<ISerialize) ];
char WRefPtr[ sizeof( MWRefPtr<ISerialize) ];
char KeyStroke[ sizeof( MKey ) ];
};
And then in the implementation if the variant is for instance string: (from
a piece of the copy constructor passed variable Var)
case eString:
new( this->String ) string( *reinterpret_cast<string const *const>(
Var.String ) );
break;
--
Make sure to set the alignment options for your compiler otherwise maybe
stuff could get misaligned i.e. on char istead of four byte boundary.
Regards, Ron AF Greve http://moonlit.xs4all.nl
"Ulrich Hobelmann" <u.*********@web.dewrote in message
news:4g*************@individual.net...
Hi, slowly transitioning from C to C++, I decided to remodel a
struct/union (i.e. type identifier as first field, union of variant types)
as a class + subclasses. Switching functions are replaced by virtual
functions. So far so good.
Now what I used to do is have a struct, set its type and union member, and
return a pointer. I.e. I initialized the struct appropriately and
returned a reference. Now I'd like to do that in C++ (right now I
construct a new subclass with "new" and delete it after every call,
because the instance is only needed very shortly anyway; seems ugly to
me).
I tried to define a union that contains all subclasses (that I will ever
use) of the base class, but C++ complains that it can't have objects with
constructors or destructors inside a union.
Is there any way to have a static piece of storage, and to return
references/objects(by value) or pointers to this piece of storage, while
also initializing it (i.e. myunion.objectB = ObjectB(); return
myunion.objecTB; or something like that)? In C I didn't need malloc() in
this case; I don't see why I should need new() now.
Jerry Coffin posted:
static char buffer[4096]; // assume that's big enough...
That's not guaranteed to be suitably aligned.
--
Frederick Gotham st************@gmail.com wrote:
Could you please give some supporting code?
I have a function that will return one of many subclasses of a base
class (so it can't be call-by-value, because the subclasses can have
different sizes). I don't want to use new(), because the object is only
used for a very short time. In C I simply had a union of appropriate
types, initialized one of them, and returned a pointer. In C++ I'd like
to return a reference (ok, doesn't matter really), but the union can't
contain a list of my subclasses, due to init/destruction issues it seems.
Jerry and Moonlit, thanks for the pointers. I could use placement new
(though it remains open how best to declare the memory area; A union
would free me from the chore of having to determine how big my objects
actually are), but I was completely ignoring that objects could have
destructors (ok, these don't, but still the idea seems a bit ugly).
I guess I'll just continue to go with new/delete. The few cycles
shouldn't hurt.
Ulrich Hobelmann schrieb:
Hi, slowly transitioning from C to C++, I decided to remodel a
struct/union (i.e. type identifier as first field, union of variant
types) as a class + subclasses. Switching functions are replaced by
virtual functions. So far so good.
You could try the placement-new way, but others have done it before: http://www.boost.org/doc/html/variant.html
Thomas
In article <%e*******************@news.indigo.ie>, fg*******@SPAM.com
says...
Jerry Coffin posted:
static char buffer[4096]; // assume that's big enough...
That's not guaranteed to be suitably aligned.
True -- a bit of ugliness to attempt to keep the code as simple as
possible, and concentrate on the placement new part. You're right,
however, that I probably should have pointed out the limitations more
thoroughly. OTOH, hopefully the comment was enough to indicate that
this buffer was really only for demo, not real use...
--
Later,
Jerry.
The universe is a figment of its own imagination.
Jerry Coffin posted:
In article <%e*******************@news.indigo.ie>, fg*******@SPAM.com
says...
>Jerry Coffin posted:
static char buffer[4096]; // assume that's big enough...
That's not guaranteed to be suitably aligned.
True -- a bit of ugliness to attempt to keep the code as simple as
possible, and concentrate on the placement new part. You're right,
however, that I probably should have pointed out the limitations more
thoroughly. OTOH, hopefully the comment was enough to indicate that
this buffer was really only for demo, not real use...
Yes I see what you're getting at, try to keep the example as simple as
possible.
I myself like to throw in a little comment, maybe at that point in the
code, or perhaps afterwards down before my signature, something like:
static char buffer[64]; /* Let's pretend it's suitably aligned */
It's good to be pedantic.
--
Frederick Gotham
Thomas J. Gritzan wrote:
Ulrich Hobelmann schrieb:
>Hi, slowly transitioning from C to C++, I decided to remodel a struct/union (i.e. type identifier as first field, union of variant types) as a class + subclasses. Switching functions are replaced by virtual functions. So far so good.
You could try the placement-new way, but others have done it before:
http://www.boost.org/doc/html/variant.html
Thanks, but that feels a bit more heavyweight than I planned to use. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Curious Expatriate |
last post by:
Hi-
I'm completely stumped. I'm trying to write some code that will parse
a file and rewrite it with all URLs replaced by something else.
For example: if the file looks like this:
<b>click...
|
by: JS Bangs |
last post by:
I started using PHP's object-oriented stuff a little while ago, which has
mostly been a joy. However, I've noticed that they don't seem to echo as I
would like. Eg:
$this->field = 255;...
|
by: lawrence |
last post by:
I've waited 6 weeks for an answer to my other question and still no
luck, so let me rephrase the question. I know I can do this:
<form method="post" action="$self">
<input type="text"...
|
by: Ben Eisenberg |
last post by:
I'm trying to run a php script setuid. I've tried POSIX_setuid but you have to be root to run this. The files are located on a public access unix system and have me as the owner and nobody as the...
|
by: Felix |
last post by:
Hi,
I've a problem:
I want to have the result of my Mysql Query in a Table in my php file.
Now I've this:
<?
|
by: James |
last post by:
What is the best way to update a record in a MYSQL DB using a FORM and PHP
?
Where ID = $ID !
Any examples or URLS ?
Thanks
|
by: Patrick Schlaepfer |
last post by:
Why this code is not working on Solaris 2.8 host. Always
getting:
PHP Fatal error: swfaction() :
getURL('http://www.php.net'
^
Line 1: Reason: 'syntax error'
in /.../htdocs/ming2.php on...
|
by: phpkid |
last post by:
Howdy
I've been given conflicting answers about search engines picking up
urls like: http://mysite.com/index.php?var1=1&var2=2&var3=3
Do search engines pick up these urls?
I've been considering...
|
by: lawrence |
last post by:
What is the PHP equivalent of messaging, as in Java?
|
by: Quinten Carlson |
last post by:
Is there a way to conditionally define a function in php?
I'm trying to run a php page 10 times using the include statement, but I get
an error because my function is already defined. The docs...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |