Hi all,
Is it possible to call a constructor of a class, call it Object,
explicitely? e.g. suppose you have a void pointer ptr pointing to a
block of memory big-enough to hold an Object. Is there a way to
initialize the region pointed to by ptr by somehow calling one of
Object's constructors?
If it's not possible or a "don't do that" it's no big deal. It is more
of a curiosity question -- it would make a piece of my code simpler --
than an actual specific need.
With my best regards,
G. Rodrigues 9 1310
Gonçalo Rodrigues wrote: Is it possible to call a constructor of a class, call it Object, explicitely?
No.
e.g. suppose you have a void pointer ptr pointing to a block of memory big-enough to hold an Object. Is there a way to initialize the region pointed to by ptr by somehow calling one of Object's constructors?
Yes, it's called "placement new". Please read about it.
[..]
V
* Gonçalo Rodrigues: Is it possible to call a constructor of a class, call it Object, explicitely?
Yes, but that is _not_ what you then clarify you're asking for.
e.g. suppose you have a void pointer ptr pointing to a block of memory big-enough to hold an Object. Is there a way to initialize the region pointed to by ptr by somehow calling one of Object's constructors?
Yes, there are two ways.
If you're not picky about having that pointer in the first place, but
any region of memory is okay, then you can use std::vector's "safe"
functionality for this, namely the default value argument which invokes
your object's copy constructor.
If you absolutely insist on construction in *ptr then you can
include <new>, I think it was, and then write
::new(ptr) Object;
which in common-speak is called "placement new".
If it's not possible or a "don't do that" it's no big deal.
It's a "don't do that".
There are numerous pitfalls.
Even experts get it wrong.
It is more of a curiosity question -- it would make a piece of my code simpler -- than an actual specific need.
Describe your problem and/or post your code; it's very likely that there
is at least one coding or design level solution that's infinitely better!
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
* Victor Bazarov: Gonçalo Rodrigues wrote: Is it possible to call a constructor of a class, call it Object, explicitely?
No.
I especially like the standard's phrasing, "explicit constructor
calls do not yield lvalues": it must be true, if they don't exist.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Alf P. Steinbach wrote: If you absolutely insist on construction in *ptr then you can include <new>, I think it was, and then write
::new(ptr) Object;
Why are you using the scope resolution operator here? Placement new is
not hidden by another new in some local scope.
--
Ioannis Vranos http://www23.brinkster.com/noicys
* Ioannis Vranos: Alf P. Steinbach wrote:
If you absolutely insist on construction in *ptr then you can include <new>, I think it was, and then write
::new(ptr) Object;
Why are you using the scope resolution operator here? Placement new is not hidden by another new in some local scope.
Well I'm not absolutely sure about that, but what I was thinking of was
the possibility for class Object to define a void* placement new operator; it
can do that because only the global one is forbidden fruit.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
On Wed, 23 Feb 2005 17:38:07 -0500, Victor Bazarov
<v.********@comAcast.net> wrote: Gonçalo Rodrigues wrote: Is it possible to call a constructor of a class, call it Object, explicitely?
No.
e.g. suppose you have a void pointer ptr pointing to a block of memory big-enough to hold an Object. Is there a way to initialize the region pointed to by ptr by somehow calling one of Object's constructors?
Yes, it's called "placement new". Please read about it.
Thanks, it's exactly what I was looking for. I just had a "Duh!"
moment.
Best regards,
G. Rodrigues
Alf P. Steinbach wrote: * Gonçalo Rodrigues:
Is it possible to call a constructor of a class, call it Object, explicitely?
Yes, but that is _not_ what you then clarify you're asking for.
Actually, it is NOT possible to call the constructor explicitly
at all, either for the this or any other purpose. Constructors
are called for you by the implementation as part of object creation.
They don't participate in name lookup, you can't get a pointer, to
them, there's no syntax to call them.
* Ron Natalie: Alf P. Steinbach wrote: * Gonçalo Rodrigues:
Is it possible to call a constructor of a class, call it Object, explicitely?
Yes, but that is _not_ what you then clarify you're asking for.
Actually, it is NOT possible to call the constructor explicitly at all, either for the this or any other purpose. Constructors are called for you by the implementation as part of object creation. They don't participate in name lookup, you can't get a pointer, to them, there's no syntax to call them.
I'm happy with non-religious terminology, and it doesn't really matter
much to me that the standard also employs it (see reply to Victor);
now please stop confusing readers of this ng with religious stuff.
Cheers,
- Alf (nothing personal, just business)
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Alf P. Steinbach wrote: I'm happy with non-religious terminology, and it doesn't really matter much to me that the standard also employs it (see reply to Victor); now please stop confusing readers of this ng with religious stuff.
Cheers,
Cheers. It's not "religious terminology", it is being
correct. And it is confusing to users to imply that they can call
the constructor directly (as this user already seemed to think that
it was possible). This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Fabian Müller |
last post by:
Hi all,
my question is as follows:
If have a class X and a class Y derived from X.
Constructor of X is X(param1, param2) .
Constructor of Y is Y(param1, ..., param4) .
|
by: Marcelo Pinto |
last post by:
Hi all,
In practice, what is the diference between a default constructor and
an explicit default constructor?
class Ai
{
public:
Ai() {}
};
|
by: Girish Shetty |
last post by:
> Hi All,
>
> Some Strange thing with C++ Constructer !!
> I never knew that we can initialize an object / a variable more than once
> using C++ Constructer till I started doing some RnD...
|
by: Alexander Stippler |
last post by:
Hi
I have already posted and discussed the following problems once, but
despite really helpful hints I did not get any further with my problem
(I at least learned, first to exactly consider why...
|
by: david |
last post by:
Well, as a matter of fact I_HAD_MISSED a basic thing or two, anyway,
although Ollie's answer makes perfectly sense when dealing with
classes, it doesn't seem to me to apply as well if you have to...
| |
by: arun |
last post by:
Hello Group,
I have a class
class Simple2 {
publica:
Simple2();
list<int> *l1;
list<int> l2;
};
|
by: John Friedland |
last post by:
My problem: I need to call (from C code) an arbitrary C library
function, but I don't know until runtime what the function name is,
how many parameters are required, and what the parameters are. I...
|
by: toton |
last post by:
Hi,
Which one is the correct syntax for constructor for static memory
allocation
Object obj("param");
or
Object obj = Object("param");
1) For the first one, how compiler checks it as not a...
|
by: Jeevang |
last post by:
Hi,
We need copy constructor when we are passing the object to a function or if the function returns any object.
Suppose if we haven't defined the user defined copy constructor, it will give a...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |