MESSAGE
DATE | 2016-11-21 |
FROM | ruben safir
|
SUBJECT | Subject: [Learn] Fwd: when is the constructor called for an object
|
From learn-bounces-at-nylxs.com Mon Nov 21 16:01:35 2016 Return-Path: X-Original-To: archive-at-mrbrklyn.com Delivered-To: archive-at-mrbrklyn.com Received: from www.mrbrklyn.com (www.mrbrklyn.com [96.57.23.82]) by mrbrklyn.com (Postfix) with ESMTP id DED5D161314; Mon, 21 Nov 2016 16:01:34 -0500 (EST) X-Original-To: learn-at-nylxs.com Delivered-To: learn-at-nylxs.com Received: from [10.0.0.62] (flatbush.mrbrklyn.com [10.0.0.62]) by mrbrklyn.com (Postfix) with ESMTP id C8CDE161312 for ; Mon, 21 Nov 2016 16:01:31 -0500 (EST) References: <874m31w0ci.fsf-at-bsb.me.uk> <3e863ctf0kfs5qffd4124kmn57btautnqi-at-4ax.com> To: learn-at-nylxs.com From: ruben safir X-Forwarded-Message-Id: <874m31w0ci.fsf-at-bsb.me.uk> <3e863ctf0kfs5qffd4124kmn57btautnqi-at-4ax.com> Message-ID: <1c21337c-9bec-c188-2cd9-803e4d740c31-at-mrbrklyn.com> Date: Mon, 21 Nov 2016 16:01:31 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/mixed; boundary="------------DA79ADC34FD754BC6EF493DA" Subject: [Learn] Fwd: when is the constructor called for an object X-BeenThere: learn-at-nylxs.com X-Mailman-Version: 2.1.17 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: learn-bounces-at-nylxs.com Sender: "Learn"
This is a multi-part message in MIME format. --------------DA79ADC34FD754BC6EF493DA Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit
Great review of constructors
--------------DA79ADC34FD754BC6EF493DA Content-Type: message/rfc822; name="when is the constructor called for an object.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="when is the constructor called for an object.eml"
Path: reader2.panix.com!panix!not-for-mail From: Popping mad Newsgroups: alt.comp.lang.learn.c-c++ Subject: when is the constructor called for an object Date: Mon, 21 Nov 2016 02:24:54 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Message-ID: NNTP-Posting-Host: www.mrbrklyn.com Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: reader2.panix.com 1479695094 17604 96.57.23.82 (21 Nov 2016 02:24:54 GMT) X-Complaints-To: abuse-at-panix.com NNTP-Posting-Date: Mon, 21 Nov 2016 02:24:54 +0000 (UTC) To: learn-at-nylxs.com User-Agent: Pan/0.140 (Chocolate Salty Balls; GIT b8fc14e git.gnome.org/git/pan2) Xref: panix alt.comp.lang.learn.c-c++:246544
I have a class class{ public: Node(Node *){}; Node(){}; Node* left() { return _left; } left(Node *in) { _left=in; }
private: Node * _left; Node * _right;
and in another class I call
Node * tmp;
and later I want to allocate its memory (without using new)
I use
tmp = nullptr;
I can't seem to use tmp{nullptr};
and when I try tmp(nullptr){};
if says tmp is not a function
when is the constructor called?
--------------DA79ADC34FD754BC6EF493DA Content-Type: message/rfc822; name="Re: when is the constructor called for an object.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Re: when is the constructor called for an object.eml"
Path: reader2.panix.com!panix!goblin2!goblin1!goblin.stu.neva.ru!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: "Alf P. Steinbach" Newsgroups: alt.comp.lang.learn.c-c++ Subject: Re: when is the constructor called for an object Date: Mon, 21 Nov 2016 04:04:51 +0100 Organization: A noiseless patient Spider Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Mon, 21 Nov 2016 03:06:58 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="ee88a5408c13d9946ce9d677130fa1a8"; logging-data="21598"; mail-complaints-to="abuse-at-eternal-september.org"; posting-account="U2FsdGVkX19uj1jY5bNFgOjNSfrEoaSY" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.5.0 In-Reply-To: Cancel-Lock: sha1:eBTMgnEy5fpozoSWVm4a35JZYio= Xref: panix alt.comp.lang.learn.c-c++:246545
On 21.11.2016 03:24, Popping mad wrote: > > I have a class > class{ > public: > Node(Node *){}; > Node(){}; > Node* left() > { > return _left; > } > left(Node *in) > { > _left=in; > } > > private: > Node * _left; > Node * _right;
You forgotten to provide a name for the class.
You have not provided any way to set the left and right pointers of an instance.
Leading underscore is an ungood convention because that convention is used to distinguish global namespace names reserved to the implementation. E.g. the Boost library code generally uses trailing underscore.
A simple data carrier class like this is best expressed as a POD struct, maybe with some helper functions:
struct Node { Node* left_; Node* right_; };
> and in another class I call > > Node * tmp;
That's not a call, that's a declaration.
> and later I want to allocate its memory (without using new) > > I use > > tmp = nullptr;
That does not allocate, that assigns a nullpointer value to `tmp`.
> I can't seem to use > tmp{nullptr};
That's not valid syntax. It could be valid as part of a declaration.
> and when I try > tmp(nullptr){}; > > if says tmp is not a function
Because it guessed that you were trying to express a function call.
This is again not valid syntax.
> when is the constructor called?
It's not.
But if you had created some `Node` instance, then a constructor would have been called. However, the two constructors you have defined do absolutely nothing. So regardless of which one you'd asked to be executed, it wouldn't do anything.
Cheers & hth.,
- Alf
--------------DA79ADC34FD754BC6EF493DA Content-Type: message/rfc822; name="Re: when is the constructor called for an object.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Re: when is the constructor called for an object.eml"
Path: reader2.panix.com!panix!not-for-mail From: Popping mad Newsgroups: alt.comp.lang.learn.c-c++ Subject: Re: when is the constructor called for an object Date: Mon, 21 Nov 2016 03:17:07 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Message-ID: References: NNTP-Posting-Host: www.mrbrklyn.com Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: reader2.panix.com 1479698227 23311 96.57.23.82 (21 Nov 2016 03:17:07 GMT) X-Complaints-To: abuse-at-panix.com NNTP-Posting-Date: Mon, 21 Nov 2016 03:17:07 +0000 (UTC) User-Agent: Pan/0.140 (Chocolate Salty Balls; GIT b8fc14e git.gnome.org/git/pan2) Xref: panix alt.comp.lang.learn.c-c++:246546
On Mon, 21 Nov 2016 04:04:51 +0100, Alf P. Steinbach wrote:
> It's not. > > But if you had created some `Node` instance, then a constructor would > have been called. However, the two constructors you have defined do > absolutely nothing. So regardless of which one you'd asked to be > executed, it wouldn't do anything. > > > Cheers & hth., > > - Alf
it is sample code Alf .. the whole thing does nothing ;)
class WOODY{ public: WOODY(WOODY *){}; WOODY(){}; WOODY* left() { return _left; } left(Node *in) { _left=in; }
private: WOODY * _left; WOODY * _right;
and in another class I call
WOODY * tmp;
and later I want to allocate its memory (without using new)
I use
tmp = nullptr;
I can't seem to use tmp{nullptr}; <<== that is only good in declarations?
and when I try tmp(nullptr){}; <<== that doesn't call the constructor?
if says tmp is not a function
when is the constructor called?
--------------DA79ADC34FD754BC6EF493DA Content-Type: message/rfc822; name="Re: when is the constructor called for an object.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Re: when is the constructor called for an object.eml"
Path: reader2.panix.com!panix!goblin3!goblin.stu.neva.ru!news.roellig-ltd.de!open-news-network.org!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: alt.comp.lang.learn.c-c++ Subject: Re: when is the constructor called for an object Date: Mon, 21 Nov 2016 11:15:09 +0000 Organization: A noiseless patient Spider Message-ID: <874m31w0ci.fsf-at-bsb.me.uk> References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="f53b1ba90493d820ad651d62f9faff7c"; logging-data="3410"; mail-complaints-to="abuse-at-eternal-september.org"; posting-account="U2FsdGVkX1+rvPoFZo+L8AEV5GqsOWah7YG9c4mLM7c=" Cancel-Lock: sha1:CzE9vuDpMzum1m7lEKtic8FON/A= sha1:rWfuaW/L75yBRpELoW1EZZKdRII= X-BSB-Auth: 1.34131ab01208c4adb4d0.20161121111509GMT.874m31w0ci.fsf-at-bsb.me.uk Xref: panix alt.comp.lang.learn.c-c++:246554
I'll just add a couple of things...
Popping mad writes:
> and in another class I call > > WOODY * tmp;
That's not a function call (it's a declaration) so the verb "call" is very confusing here. The best verb to use here is just "write": "in another class I write..."
> and later I want to allocate its memory (without using new) > > I use > > tmp = nullptr;
This is an assignment. A lot of confusion has been caused over the years by the fact that C and C++ both use the same symbol for two very different things:
Thing x = 1; // initialisation x = 2; // assignment
> I can't seem to use tmp{nullptr}; <<== that is only good in > declarations?
Pretty much. The relatively recent {} syntax is only good in initialisations. Declarations are where most things are initialised, but you can use it in other contexts (for example where a temporary object is created).
One reason to use {} is to make the distinction between assignment and initialisation syntactically distinct.
-- Ben.
--------------DA79ADC34FD754BC6EF493DA Content-Type: message/rfc822; name="Re: when is the constructor called for an object.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Re: when is the constructor called for an object.eml"
Path: reader2.panix.com!panix!goblin3!goblin1!goblin.stu.neva.ru!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Barry Schwarz Newsgroups: alt.comp.lang.learn.c-c++ Subject: Re: when is the constructor called for an object Date: Sun, 20 Nov 2016 20:39:15 -0800 Organization: A noiseless patient Spider Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Injection-Info: mx02.eternal-september.org; posting-host="b1ffb49c3b67adc12128617a730c3b5c"; logging-data="5804"; mail-complaints-to="abuse-at-eternal-september.org"; posting-account="U2FsdGVkX1/ZGmp3atqTvLrrdjqPw+wTr3I8zyFJuZc=" X-Newsreader: Forte Agent 4.2/32.1118 Cancel-Lock: sha1:1kuwhxcYbjxLfZbF8cMHLiVnIuw= Xref: panix alt.comp.lang.learn.c-c++:246547
On Mon, 21 Nov 2016 02:24:54 +0000 (UTC), Popping mad wrote:
> > >I have a class >class{ >public: > Node(Node *){}; > Node(){};
Where is Node defined? Based on the two constructors you have defined, is the first line of the class declaration actually class Node{ and not an unnamed class as your message text implies?
> Node* left() > { > return _left; > } > left(Node *in) > { > _left=in; > } > >private: > Node * _left; > Node * _right; > > >and in another class I call > >Node * tmp;
tmp is a pointer to a class.
>and later I want to allocate its memory (without using new)
What do you actually mean by allocate if you won't use new? There are only three possible values for tmp: It can be indeterminate (the result of the above definition that does not contain any initialization). It can be a null value (the result of the assignment you coded below). It can point to an object of type Node. This will only happen if you assign it the address of an existing object or the address of an object you allocate using new or an equivalent process.
>I use > >tmp = nullptr; > >I can't seem to use >tmp{nullptr};
What syntax do you think the above statement is an example of? It is not initialization since it is not the definition of an object
>and when I try >tmp(nullptr){};
What syntax do you think this is an example of? It cannot be a function definition since there is no return type, tmp is not a function name, and nullptr is not a type.
>if says tmp is not a function
Quite so. tmp is a pointer to an object of type Node. Currently there is no object of that type and tmp contains a null value which documents that it does not currently point to an object.
>when is the constructor called?
The constructor is called when an object of that type is created. This normally happens when an object is defined or when new allocates space for an object of that type. It is not called when a pointer to an object is defined unless initialization causes allocation.
-- Remove del for email
--------------DA79ADC34FD754BC6EF493DA Content-Type: message/rfc822; name="Re: when is the constructor called for an object.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Re: when is the constructor called for an object.eml"
Path: reader2.panix.com!panix!not-for-mail From: Popping mad Newsgroups: alt.comp.lang.learn.c-c++ Subject: Re: when is the constructor called for an object Date: Mon, 21 Nov 2016 05:59:16 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Message-ID: References: NNTP-Posting-Host: www.mrbrklyn.com Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: reader2.panix.com 1479707956 23311 96.57.23.82 (21 Nov 2016 05:59:16 GMT) X-Complaints-To: abuse-at-panix.com NNTP-Posting-Date: Mon, 21 Nov 2016 05:59:16 +0000 (UTC) User-Agent: Pan/0.140 (Chocolate Salty Balls; GIT b8fc14e git.gnome.org/git/pan2) Xref: panix alt.comp.lang.learn.c-c++:246548
On Sun, 20 Nov 2016 20:39:15 -0800, Barry Schwarz wrote:
> What syntax do you think this is an example of? It cannot be a function > definition since there is no return type, tmp is not a function name, > and nullptr is not a type.
it would be an attempt to use the constructor of the object.
--------------DA79ADC34FD754BC6EF493DA Content-Type: message/rfc822; name="Re: when is the constructor called for an object.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Re: when is the constructor called for an object.eml"
Path: reader2.panix.com!panix!goblin2!goblin1!goblin.stu.neva.ru!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Barry Schwarz Newsgroups: alt.comp.lang.learn.c-c++ Subject: Re: when is the constructor called for an object Date: Mon, 21 Nov 2016 08:36:32 -0800 Organization: A noiseless patient Spider Message-ID: <3e863ctf0kfs5qffd4124kmn57btautnqi-at-4ax.com> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Injection-Info: mx02.eternal-september.org; posting-host="75134bb09f65065dd5f371d984aa9bd1"; logging-data="11596"; mail-complaints-to="abuse-at-eternal-september.org"; posting-account="U2FsdGVkX1/Ew7ANREjo4XvntNDqjZapcj/D8vZdICk=" X-Newsreader: Forte Agent 4.2/32.1118 Cancel-Lock: sha1:muDqfUtRm96zwCDBXbUclbvDrKU= Xref: panix alt.comp.lang.learn.c-c++:246555
On Mon, 21 Nov 2016 05:59:16 +0000 (UTC), Popping mad wrote:
>On Sun, 20 Nov 2016 20:39:15 -0800, Barry Schwarz wrote: > >> What syntax do you think this is an example of? It cannot be a function >> definition since there is no return type, tmp is not a function name, >> and nullptr is not a type. > >it would be an attempt to use the constructor of the object.
You don't call a constructor of an object. The appropriate constructor is called for you automatically when the object is being created by one of the methods described in my previous message.
For you to call a member function of a class, you normally need to have an object of that class. You don't have any objects of your class.
-- Remove del for email
--------------DA79ADC34FD754BC6EF493DA Content-Type: message/rfc822; name="Re: when is the constructor called for an object.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Re: when is the constructor called for an object.eml"
Path: reader2.panix.com!panix!goblin3!goblin.stu.neva.ru!nntp-feed.chiark.greenend.org.uk!ewrotcd!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Ike Naar Newsgroups: alt.comp.lang.learn.c-c++ Subject: Re: when is the constructor called for an object Date: Mon, 21 Nov 2016 08:31:22 -0000 (UTC) Organization: A noiseless patient Spider Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Injection-Date: Mon, 21 Nov 2016 08:31:22 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="cae1db1297ffc0cc56488ebcd6981047"; logging-data="1739"; mail-complaints-to="abuse-at-eternal-september.org"; posting-account="U2FsdGVkX1/YdQB+sX/HGZcSl8pJnWKF" User-Agent: slrn/0.9.9p1 (NetBSD) Cancel-Lock: sha1:mtHO+S5Us0WODepW9vX9YZersrs= Xref: panix alt.comp.lang.learn.c-c++:246549
On 2016-11-21, Popping mad wrote: > I have a class
> class{
You probably meant
class Node {
> public: > Node(Node *){}; > Node(){}; > Node* left() > { > return _left; > } > left(Node *in)
You probably meant
void left(Node *in)
> { > _left=in; > } > > private: > Node * _left; > Node * _right;
};
> and in another class I call > > Node * tmp; > > and later I want to allocate its memory (without using new)
Why can't you use new?
> I use > > tmp = nullptr; > > I can't seem to use > tmp{nullptr}; > > and when I try > tmp(nullptr){}; > > if says tmp is not a function > > when is the constructor called?
The constructur is called when an instance of class Node is created. E.g. like this:
Node *px = new Node; /* constructor Node() is called */ Node *py = new Node(px); /* constructor Node(Node*) is called */ Node z; /* constructor Node() is called */ Node *pz = &z; Node w(z); /* constructor Node(Node*) is called */ Node *pw = &w; Node *
--------------DA79ADC34FD754BC6EF493DA Content-Type: message/rfc822; name="Re: when is the constructor called for an object.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Re: when is the constructor called for an object.eml"
Path: reader2.panix.com!panix!not-for-mail From: ruben safir Newsgroups: alt.comp.lang.learn.c-c++ Subject: Re: when is the constructor called for an object Date: Mon, 21 Nov 2016 03:37:52 -0500 Organization: PANIX Public Access Internet and UNIX, NYC Message-ID: References: NNTP-Posting-Host: www.mrbrklyn.com Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit X-Trace: reader2.panix.com 1479717472 9651 96.57.23.82 (21 Nov 2016 08:37:52 GMT) X-Complaints-To: abuse-at-panix.com NNTP-Posting-Date: Mon, 21 Nov 2016 08:37:52 +0000 (UTC) User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 In-Reply-To: Xref: panix alt.comp.lang.learn.c-c++:246550
On 11/21/2016 03:31 AM, Ike Naar wrote: > On 2016-11-21, Popping mad wrote: >> I have a class > >> class{ > > You probably meant > > class Node { > >> public: >> Node(Node *){}; >> Node(){}; >> Node* left() >> { >> return _left; >> } >> left(Node *in) > > You probably meant > > void left(Node *in) > >> { >> _left=in; >> } >> >> private: >> Node * _left; >> Node * _right; > > }; > >> and in another class I call >> >> Node * tmp; >> >> and later I want to allocate its memory (without using new) > > Why can't you use new? > >> I use >> >> tmp = nullptr; >> >> I can't seem to use >> tmp{nullptr}; >> >> and when I try >> tmp(nullptr){}; >> >> if says tmp is not a function >> >> when is the constructor called? > > The constructur is called when an instance of class Node is created. > E.g. like this: > > Node *px = new Node; /* constructor Node() is called */ > Node *py = new Node(px); /* constructor Node(Node*) is called */ > Node z; /* constructor Node() is called */ > Node *pz = &z; > Node w(z); /* constructor Node(Node*) is called */ > Node *pw = &w; > Node * > constructor confusion
The real problem here is that then new Strousrup text seems to say that use of {} can be used in lew of "= new" nomenclature, but it doesn't seem to work like he suggests, which means I'm not understanding what he wrote, or what he wrote wasn't clear ;)
--------------DA79ADC34FD754BC6EF493DA Content-Type: message/rfc822; name="Re: when is the constructor called for an object.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Re: when is the constructor called for an object.eml"
Path: reader2.panix.com!panix!goblin2!goblin1!goblin.stu.neva.ru!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Ike Naar Newsgroups: alt.comp.lang.learn.c-c++ Subject: Re: when is the constructor called for an object Date: Mon, 21 Nov 2016 09:14:36 -0000 (UTC) Organization: A noiseless patient Spider Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Injection-Date: Mon, 21 Nov 2016 09:14:36 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="cae1db1297ffc0cc56488ebcd6981047"; logging-data="1739"; mail-complaints-to="abuse-at-eternal-september.org"; posting-account="U2FsdGVkX1/uYylGkVnCDobNqQSadh7m" User-Agent: slrn/0.9.9p1 (NetBSD) Cancel-Lock: sha1:wSesB8y9CW1422TM54MNZWQPCdw= Xref: panix alt.comp.lang.learn.c-c++:246551
On 2016-11-21, ruben safir wrote: > On 11/21/2016 03:31 AM, Ike Naar wrote: >> The constructur is called when an instance of class Node is created. >> E.g. like this: >> >> Node *px = new Node; /* constructor Node() is called */ >> Node *py = new Node(px); /* constructor Node(Node*) is called */ >> Node z; /* constructor Node() is called */ >> Node *pz = &z; >> Node w(z); /* constructor Node(Node*) is called */ >> Node *pw = &w; >> Node * >> > constructor confusion > > The real problem here is that then new Strousrup text seems to say that > use of {} can be used in lew of "= new" nomenclature, but it doesn't > seem to work like he suggests, which means I'm not understanding what he > wrote, or what he wrote wasn't clear ;)
Not sure what you mean. Of course one could rerwite the above as
Node *x(new Node); /* constructor Node() is called */ Node *y(new Node(x)); /* constructor Node(Node*) is called */ Node z; /* constructor Node() is called */ Node w(&z); /* constructor Node(Node*) is called */ Node *tmp(&w);
which uses () instead of = for the initializations.
--------------DA79ADC34FD754BC6EF493DA Content-Type: message/rfc822; name="Re: when is the constructor called for an object.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Re: when is the constructor called for an object.eml"
Path: reader2.panix.com!panix!not-for-mail From: ruben safir Newsgroups: alt.comp.lang.learn.c-c++ Subject: Re: when is the constructor called for an object Date: Mon, 21 Nov 2016 04:46:18 -0500 Organization: PANIX Public Access Internet and UNIX, NYC Message-ID: References: NNTP-Posting-Host: www.mrbrklyn.com Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit X-Trace: reader2.panix.com 1479721578 9190 96.57.23.82 (21 Nov 2016 09:46:18 GMT) X-Complaints-To: abuse-at-panix.com NNTP-Posting-Date: Mon, 21 Nov 2016 09:46:18 +0000 (UTC) User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 In-Reply-To: Xref: panix alt.comp.lang.learn.c-c++:246552
On 11/21/2016 04:14 AM, Ike Naar wrote: > Not sure what you mean. > Of course one could rerwite the above as > > Node *x(new Node); /* constructor Node() is called */ > Node *y(new Node(x)); /* constructor Node(Node*) is called */ > Node z; /* constructor Node() is called */ > Node w(&z); /* constructor Node(Node*) is called */ > Node *tmp(&w); > > which uses () instead of = for the initializations.
for one thing I think he is saying that
Node *x{new Node}; works
--------------DA79ADC34FD754BC6EF493DA Content-Type: message/rfc822; name="Re: when is the constructor called for an object.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Re: when is the constructor called for an object.eml"
Path: reader2.panix.com!panix!goblin2!goblin1!goblin.stu.neva.ru!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Ike Naar Newsgroups: alt.comp.lang.learn.c-c++ Subject: Re: when is the constructor called for an object Date: Mon, 21 Nov 2016 10:09:51 -0000 (UTC) Organization: A noiseless patient Spider Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Injection-Date: Mon, 21 Nov 2016 10:09:51 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="cae1db1297ffc0cc56488ebcd6981047"; logging-data="1739"; mail-complaints-to="abuse-at-eternal-september.org"; posting-account="U2FsdGVkX1/R0X/xqc9rsMHvgcb/07wo" User-Agent: slrn/0.9.9p1 (NetBSD) Cancel-Lock: sha1:BrIe/WhRWQ7JIlqXPz1+FoM/1jg= Xref: panix alt.comp.lang.learn.c-c++:246553
On 2016-11-21, ruben safir wrote: > On 11/21/2016 04:14 AM, Ike Naar wrote: >> Not sure what you mean. >> Of course one could rerwite the above as >> >> Node *x(new Node); /* constructor Node() is called */ >> Node *y(new Node(x)); /* constructor Node(Node*) is called */ >> Node z; /* constructor Node() is called */ >> Node w(&z); /* constructor Node(Node*) is called */ >> Node *tmp(&w); >> >> which uses () instead of = for the initializations. > > for one thing I think he is saying that > > Node *x{new Node}; works
Yes, that works, too.
One could rewrite the above as:
Node *x{new Node}; /* constructor Node() is called */ Node *y{new Node{x}}; /* constructor Node(Node*) is called */ Node z; /* constructor Node() is called */ Node w{&z}; /* constructor Node(Node*) is called */ Node *tmp{&w};
--------------DA79ADC34FD754BC6EF493DA Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline
_______________________________________________ Learn mailing list Learn-at-nylxs.com http://lists.mrbrklyn.com/mailman/listinfo/learn
--------------DA79ADC34FD754BC6EF493DA--
|
|