473,545 Members | 529 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling constructor explicitely

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
Jul 23 '05 #1
9 1321
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
Jul 23 '05 #2
* 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?
Jul 23 '05 #3
* 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?
Jul 23 '05 #4
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
Jul 23 '05 #5
* 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?
Jul 23 '05 #6
On Wed, 23 Feb 2005 17:38:07 -0500, Victor Bazarov
<v.********@com Acast.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
Jul 23 '05 #7
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.
Jul 23 '05 #8
* 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?
Jul 23 '05 #9
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).
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
5145
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) .
12
4731
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() {} };
4
1529
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 coding!!!!
11
1848
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 something does not work instead of immediately searching for work arounds) . I have the following code resulting in an ambiguity:...
2
2183
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 instantiate an array of structures; consider the following useless code : using System; struct MyPointS
8
1898
by: arun | last post by:
Hello Group, I have a class class Simple2 { publica: Simple2(); list<int> *l1; list<int> l2; };
18
4320
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 can use dlopen/whatever to convert the function name into a pointer to that function, but actually calling it, with the right number of parameters,...
9
1974
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 function deceleration rather than ctor call?
4
13805
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 call to default copy constructor. Won't it solve our purpose ? Why we have to define the default copy constructor explicitely ?? Thanks, Jeevan
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7416
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7752
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5969
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4944
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3449
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1878
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 we have to send another system
1
1013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
701
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.