Pointing to a class

P

pauldepstein

If Fred is a class, I am often confused by the appearance of Fred()
instead of Fred

Here is an example from a well-established c++ website:

#include <new> // Must #include this to use "placement new"
#include "Fred.h" // Declaration of class Fred

void someCode()
{
char memory[sizeof(Fred)]; // Line #1
void* place = memory; // Line #2

Fred* f = new(place) Fred(); // Line #3 (see "DANGER" below)
// The pointers f and place will be equal

...
}

I do believe that I understand this code. However, I would write
Fred* f = new(place) Fred;

After all, for the more usual new, the format is pointer = new type;

It is _not_ pointer = new type();

The class Fred is called Fred not Fred(). What are these final
brackets doing?

Paul
 
R

Rolf Magnus

If Fred is a class, I am often confused by the appearance of Fred()
instead of Fred

Here is an example from a well-established c++ website:

#include <new> // Must #include this to use "placement new"
#include "Fred.h" // Declaration of class Fred

void someCode()
{
char memory[sizeof(Fred)]; // Line #1
void* place = memory; // Line #2

Fred* f = new(place) Fred(); // Line #3 (see "DANGER" below)
// The pointers f and place will be equal

...
}

I do believe that I understand this code. However, I would write
Fred* f = new(place) Fred;

Both are correct, and for non-POD, they do the same thing.
After all, for the more usual new, the format is pointer = new type;

It is _not_ pointer = new type();

Where did you get that from?
The class Fred is called Fred not Fred().

If Fred had a constructor that takes int, you could also write:

Fred* f = new(place) Fred(4);

even though the class is not called Fred(4). Would you still find it unusual
to write it like this?
What are these final brackets doing?

They explicitly say that the object is to be default initialized.
 
P

pauldepstein

Rolf said:
If Fred is a class, I am often confused by the appearance of Fred()
instead of Fred

Here is an example from a well-established c++ website:

#include <new> // Must #include this to use "placement new"
#include "Fred.h" // Declaration of class Fred

void someCode()
{
char memory[sizeof(Fred)]; // Line #1
void* place = memory; // Line #2

Fred* f = new(place) Fred(); // Line #3 (see "DANGER" below)
// The pointers f and place will be equal

...
}

I do believe that I understand this code. However, I would write
Fred* f = new(place) Fred;

Both are correct, and for non-POD, they do the same thing.
After all, for the more usual new, the format is pointer = new type;

It is _not_ pointer = new type();

Where did you get that from?
The class Fred is called Fred not Fred().

If Fred had a constructor that takes int, you could also write:

Fred* f = new(place) Fred(4);

even though the class is not called Fred(4). Would you still find it unusual
to write it like this?
What are these final brackets doing?

They explicitly say that the object is to be default initialized.

Thanks Rolf.

This is very helpful to me.

The website I quoted is:

http://www.parashift.com/c++-faq-lite/

Paul Epstein
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top