Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

the new {} syntax

65 views
Skip to first unread message

Popping mad

unread,
Nov 20, 2016, 10:10:11 PM11/20/16
to
why doesn't this enter the second constructor?

18 #include<iostream>
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@flatbush max_path]$ g++ -Wall test.cpp
[ruben@flatbush max_path]$ ./a.out
null constructor
0
Segmentation fault

Louis Krupp

unread,
Nov 20, 2016, 11:06:38 PM11/20/16
to
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<iostream>
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

ruben safir

unread,
Nov 20, 2016, 11:33:49 PM11/20/16
to
Right but there seems to be no way, without calling new, to create an
anonymous object which is pointed to by parent * myvar

Juha Nieminen

unread,
Nov 21, 2016, 2:58:41 AM11/21/16
to
Louis Krupp <lkr...@nospam.pssw.com.invalid> wrote:
>> 39 parent * tmp = new parent;
>> 40 parent * tmp2{tmp};

> 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.

If you are not mistaken? Sheesh.

'tmp' is a pointer to something. 'tmp2' is another pointer of the same type.
It's assigned the pointer 'tmp'. Thus it will just point to the same object.
Only one instance of 'parent' is being created here (using 'new').

Paavo Helde

unread,
Nov 21, 2016, 3:10:31 AM11/21/16
to
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<iostream>

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;
}






ruben safir

unread,
Nov 21, 2016, 4:52:06 AM11/21/16
to
On 11/21/2016 02:58 AM, Juha Nieminen wrote:
> If you are not mistaken? Sheesh.

why bother typing after that? Nothing else is read. I know that you
think that one who is asking for information will certainly read past
this but frankly, in my old age, I don't feel the need.

/dev/null

there is more to life than the right answer for a programming language.
I can put the keyboard down and play with my grandchildren.

I hope I've made my point without being unnecessarily rude.

Reuvain

ruben safir

unread,
Nov 21, 2016, 4:55:43 AM11/21/16
to
thank you. You've clear up what I find is a subtle point which can
cause great bugs. I appreciate you taking the time to discuss this with me.

Reuvain

Alf P. Steinbach

unread,
Nov 21, 2016, 9:02:06 AM11/21/16
to
On 21.11.2016 10:51, ruben safir wrote:
> On 11/21/2016 02:58 AM, Juha Nieminen wrote:
>> If you are not mistaken? Sheesh.
>
> why bother typing after that? Nothing else is read. I know that you
> think that one who is asking for information will certainly read past
> this but frankly, in my old age, I don't feel the need.

You were introducing needless and misleading uncertainty.


> /dev/null
>
> there is more to life than the right answer for a programming language.
> I can put the keyboard down and play with my grandchildren.
>
> I hope I've made my point without being unnecessarily rude.

Juha was entirely correct in pointing out that the uncertainty you
expressed, about fundamental stuff, was unwarranted and misleading.

As he wrote, “Sheesh”.


Cheers & hth.,

- Alf ;-)

Alf P. Steinbach

unread,
Nov 21, 2016, 9:04:02 AM11/21/16
to
s/you/Louis/g

@Ruben: Your own fault for removing all context, heh.

ruben safir

unread,
Nov 21, 2016, 3:51:01 PM11/21/16
to
On 11/21/2016 08:59 AM, Alf P. Steinbach wrote:
>
> You were introducing needless and misleading uncertainty.

Alf, your no gem either. Reconsider what your saying because your way
off and if you were one of my children at the table, would be sent
upstairs to mull over things.

Reuvain

Alf P. Steinbach

unread,
Nov 22, 2016, 8:58:29 AM11/22/16
to
No need to get personal, the ad hominem.

Just take to heart that in your defense of one person who erred, you
effectively supported an incorrect and misleading view of the technical.

This is first and foremost a technical group (even though, unlike its
now defunct moderated sister group, it lacks a charter). If you want a
more social forum, one that actively discourages technical discussion
and encourages social up- and down-voting, there's Stack Overflow for
you, the greatest Herb Schildt zone on the net. Personally I treat SO as
a technical forum in the hopes of helping to preventg changes to the
worse, but there's many people there who, like you, seem to revel in the
opportunities to focus on social games: defending this person, attacking
that person, projecting a false image, just like you did now.


Cheers & hope you fuck off,

- Alf

ruben safir

unread,
Nov 22, 2016, 11:54:47 AM11/22/16
to
On 11/22/2016 08:55 AM, Alf P. Steinbach wrote:
> Just take to heart that in your defense of one person who erred, you
> effectively supported an incorrect and misleading view of the technical.


My lexer can't parse this. Not even with antlr

Louis Krupp

unread,
Nov 22, 2016, 12:41:52 PM11/22/16
to
It took me a while to figure out what you're getting at.

You're finding disagreement where there is none. "tmp2 is a copy of
tmp" means "tmp2 has the same value as tmp." Nobody said anything
about copies of "parent"; there's only one.

Louis

woodb...@gmail.com

unread,
Nov 22, 2016, 3:03:27 PM11/22/16
to
On Tuesday, November 22, 2016 at 7:58:29 AM UTC-6, Alf P. Steinbach wrote:

Alf, please don't swear here.

Have you seen this one?
http://www.city-journal.org/html/real-war-science-14782.html


Brian
Ebenezer Enterprises
http://webEbenezer.net

red floyd

unread,
Nov 22, 2016, 4:01:21 PM11/22/16
to
On 11/22/2016 12:03 PM, woodb...@gmail.com wrote:
> On Tuesday, November 22, 2016 at 7:58:29 AM UTC-6, Alf P. Steinbach wrote:
>
> Alf, please don't swear here.
>
Fuck off. Haven't you figured it out yet?


0 new messages