Connecting Tech Pros Worldwide Forums | Help | Site Map

operator= and const data members

Jason Heyes
Guest
 
Posts: n/a
#1: Jul 22 '05
Will this class compile?

class C { const int a; }

Not sure because default operator= is memberwise assignment and member is
const. Thanks for help.



John Harrison
Guest
 
Posts: n/a
#2: Jul 22 '05

re: operator= and const data members



"Jason Heyes" <jasonheyes@optusnet.com.au> wrote in message
news:40739ad7$0$16964$afc38c87@news.optusnet.com.a u...[color=blue]
> Will this class compile?
>
> class C { const int a; }
>
> Not sure because default operator= is memberwise assignment and member is
> const. Thanks for help.
>[/color]

It will compile but if you write

C x;
C y;
x = y;

you will get an error message that says something like 'the default
assignment operator could not be generated'.

john


John Harrison
Guest
 
Posts: n/a
#3: Jul 22 '05

re: operator= and const data members



"Jason Heyes" <jasonheyes@optusnet.com.au> wrote in message
news:40739ad7$0$16964$afc38c87@news.optusnet.com.a u...[color=blue]
> Will this class compile?
>
> class C { const int a; }
>
> Not sure because default operator= is memberwise assignment and member is
> const. Thanks for help.
>[/color]

It will compile but if you write

C x;
C y;
x = y;

you will get an error message that says something like 'the default
assignment operator could not be generated'.

john


Closed Thread


Similar C / C++ bytes