MESSAGE
DATE | 2016-11-21 |
FROM | ruben safir
|
SUBJECT | Subject: [Learn] Fwd: Re: the new {} syntax
|
From learn-bounces-at-nylxs.com Mon Nov 21 04:56:20 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 BAA2B161316; Mon, 21 Nov 2016 04:56:19 -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 1596F161312 for ; Mon, 21 Nov 2016 04:56:17 -0500 (EST) References: <0oKdnYecPfRxNq_FnZ2dnUU78eXNnZ2d-at-giganews.com> To: learn-at-nylxs.com From: ruben safir X-Forwarded-Message-Id: <0oKdnYecPfRxNq_FnZ2dnUU78eXNnZ2d-at-giganews.com> Message-ID: <3334bcfd-38f0-1da7-c630-3bd0af693d7f-at-mrbrklyn.com> Date: Mon, 21 Nov 2016 04:56:16 -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: <0oKdnYecPfRxNq_FnZ2dnUU78eXNnZ2d-at-giganews.com> Subject: [Learn] Fwd: Re: the new {} syntax 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: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: learn-bounces-at-nylxs.com Sender: "Learn"
-------- Forwarded Message -------- Subject: Re: the new {} syntax Date: Mon, 21 Nov 2016 10:10:23 +0200 From: Paavo Helde Newsgroups: comp.lang.c++ References:
On 21.11.2016 6:33, ruben safir wrote: > On 11/20/2016 11:06 PM, Louis Krupp wrote: >> On Mon, 21 Nov 2016 03:10:01 +0000 (UTC), Popping mad >> wrote: >> >>> why doesn't this enter the second constructor? >>> >>> 18 #include >>> 19 >>> 20 class parent{ >>> 21 public: >>> 22 parent():_something{nullptr}, _val{0} { >>> 23 std::cout << "null constructor\n"; >>> 24 }; >>> 25 parent(parent *in):_something{in}, _val{10} { >>> 26 std::cout << "here\n"; >>> 27 _something->_val = 0; >>> 28 }; >>> 29 parent * _something; >>> 30 int _val; >>> 31 }; >>> 32 void discover(parent * in){ >>> 33 std::cout<< in->_val << std::endl; >>> 34 std::cout<< in->_something->_val << std::endl; >>> 35 } >>> 36 >>> 37 int main(int argc, char **argv) >>> 38 { >>> 39 parent * tmp = new parent; >>> 40 parent * tmp2{tmp}; >>> 41 //parent * tmp2 = new parent(tmp); >>> 42 discover(tmp2); >>> 43 >>> 44 >>> 45 >>> 46 return EXIT_SUCCESS; >>> 47 } >>> [ruben-at-flatbush max_path]$ g++ -Wall test.cpp >>> [ruben-at-flatbush max_path]$ ./a.out >>> null constructor >>> 0 >>> Segmentation fault >> >> Because "parent * tmp2{tmp}" isn't constructing a new parent object; >> if I'm not mistaken, tmp2 is going to be of type parent * and a copy >> of tmp. >> >> Try this: >> >> 1 #include >> 2 >> 3 class parent{ >> 4 public: >> 5 parent():_something{nullptr}, _val{0} { >> 6 std::cout << "null constructor\n"; >> 7 }; >> 8 parent(parent *in):_something{in}, _val{10} { >> 9 std::cout << "here\n"; >> 10 _something->_val = 0; >> 11 }; >> 12 parent * _something; >> 13 int _val; >> 14 }; >> 15 void discover(parent * in){ >> 16 std::cout<< in->_val << std::endl; >> 17 std::cout<< in->_something->_val << std::endl; >> 18 } >> 19 >> 20 int main() >> 21 { >> 22 parent * tmp = new parent; >> 23 parent tmp2{tmp}; >> 24 //parent * tmp2 = new parent(tmp); >> 25 discover(&tmp2); >> 26 >> 27 return 0; >> 28 } >> >> Louis >> > Right but there seems to be no way, without calling new, to create an > anonymous object which is pointed to by parent * myvar
You are about right, if I understand correctly what you mean by "anonymous", and if you want the object to stay alive for longer term.
This is because in C++ the object lifetimes are deterministic. Either you create the object on stack (automatic storage duration) and then it needs a named object or at least a const reference to keep it alive, or alternatively, you create an object via new or equivalent (dynamic storage duration) and then you need a pointer for accessing and finally destroying the object.
The nearest you can get to "a non-newed object and pointer" is "a non-newed object and a const reference". There is a special rule that a const reference keeps a temporary object alive during the lifetime of the reference.
#include
class parent{ public: parent():_something{nullptr}, _val{0} { std::cout << "null constructor\n"; }; parent(parent *in):_something{in}, _val{10} { std::cout << "here\n"; _something->_val = 0; }; parent * _something; int _val; }; void discover(const parent * in){ std::cout<< in->_val << std::endl; std::cout<< in->_something->_val << std::endl; }
int main() { parent * tmp = new parent; const parent& tmp2 = parent {tmp}; discover(&tmp2);
return 0; }
_______________________________________________ Learn mailing list Learn-at-nylxs.com http://lists.mrbrklyn.com/mailman/listinfo/learn
|
|