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

guaranteeing const

29 views
Skip to first unread message

Popping mad

unread,
Nov 18, 2016, 1:39:20 PM11/18/16
to
How does one do this without losing the const


}; /* constructor */
Node ( const Node &other ){
parent(other.parent()) const;
right(other.right() const);
left(other.left() const);
value(other.value() const);
max(other.max()) const;
}; /* copy constructor */
~Node (){}; /*
destructor */


msort.h|41 col 25| error: passing ‘const maxpath::Node’ as ‘this’
argument discards qualifiers [-fpermissive]

bitrex

unread,
Nov 18, 2016, 3:06:27 PM11/18/16
to
Probably because your methods "parent", "right", etc. aren't declared
"const."

What compiler does this compile under? I'm surprised putting "const"
after a function call like that isn't a syntax error.

If you have to do it this way probably best to create a local non-const
copy of Node in the copy ctr and pass it in

bitrex

unread,
Nov 18, 2016, 3:13:57 PM11/18/16
to
Well, wait. That might be bad...

red floyd

unread,
Nov 18, 2016, 3:14:45 PM11/18/16
to
I *THINK* he's trying to use an initializer list. I've already told him
his syntax for that is wrong.

bitrex

unread,
Nov 18, 2016, 3:16:58 PM11/18/16
to

bitrex

unread,
Nov 18, 2016, 3:17:31 PM11/18/16
to
On 11/18/2016 03:14 PM, red floyd wrote:
Ah. I would be pretty surprised if the code as it is right there
actually compiled...

bitrex

unread,
Nov 18, 2016, 3:24:01 PM11/18/16
to
On 11/18/2016 03:14 PM, red floyd wrote:
if what he's shooting for is something like:

Node(const Node& other) : parent(other.parent()), right(other.right()), etc.

then AFAIK the "getters" parent, right etc. need to have "const" after
their function declaration or there's gonna be trouble.

Popping mad

unread,
Nov 18, 2016, 4:10:59 PM11/18/16
to
On Fri, 18 Nov 2016 12:14:35 -0800, red floyd wrote:

> *THINK* he's trying to use an initializer list. I've already told him
> his syntax for that is wrong.

no I am not. Nor was I doing that before. I'm building accessory
methods to private data. It happens to be a copy constructor and there
is no initializer list. I did wonder if I could, however, condense this
into an initializer list, in this case, because I am accessing a
parameter and then evaluating its value, which is a class instance, and
calling a member of the class which returns a value with is assigned to a
member of 'this'.

Alf P. Steinbach

unread,
Nov 18, 2016, 5:19:14 PM11/18/16
to
You'd better express that as an initializer list.

I.e.

Node( Node const& other )
: parent{ other.parent }
, right{ other.right }
, left{ other.left }
, value{ other.value }
, max{ other.max }
{}

But this is what the default copy constructor will do, and it will not
forget to copy over some item.

So really you should write

Node( Node const& other ) = default;

Or even better, just not declare any copy constructor at all.

Cheers!,

- Alf

ruben safir

unread,
Nov 19, 2016, 11:45:37 AM11/19/16
to
On 11/18/2016 03:06 PM, bitrex wrote:
> On 11/18/2016 01:39 PM, Popping mad wrote:
>> How does one do this without losing the const
>>
>>
>> }; /* constructor */
>> Node ( const Node &other ){
>> parent(other.parent()) const;
>> right(other.right() const);
>> left(other.left() const);
>> value(other.value() const);
>> max(other.max()) const;
>> }; /* copy constructor */
>> ~Node (){}; /*
>> destructor */
>>
>>
>> msort.h|41 col 25| error: passing ‘const maxpath::Node’ as ‘this’
>> argument discards qualifiers [-fpermissive]
>>
>
> Probably because your methods "parent", "right", etc. aren't declared
> "const."
>
> What compiler does this compile under? I'm surprised putting "const"
> after a function call like that isn't a syntax error.
>

gcc
0 new messages