help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: uninitializing constructor


From: david
Subject: Re: uninitializing constructor
Date: Sat, 7 Oct 2006 08:53:42 +0000 (UTC)

jinxidoru <jinxidoru@byu.net> wrote:
>
>> class A
>> {
>> private:
>>        int my_value;
>>        A (int init)      { my_value = init; }
>> public:
>>        A () {}
>>        A (const A& init) { my_value = init.my_value; }
>>        A& operator = (int scalar) { my_value = scalar << 8; }
>>        ...
>> };
>>
>> int main (void)
>> {
>>        int p, q;
>>        A x, y;
>>
>>        p = q;
>>        y = x;
>> }
>>
>> g++ complains about q beeing used but not initialized, is there a way for it
>> to complain about x too ?
>> I tried the options -O -Wall -Wextra.
>
> I appologize if I am misunderstanding what you are trying to do here.
> If so, let me know and I'll try to help better.  It seems that the best
> way would be to just not have a default constructor.  As long as you
> have other constructors, the default constructor will not be defined.
> The default constructor is only implicitly defined if no other
> constructors are defined either.
> 
> If you really need the default constructor internally, you could make
> it private or protected so that it can only be accessed from within the
> class.

I understand, but I'm questionning myself on the very example above, with
the constructors I wrote. "A(){}" must remain public so to allow the user to
declare a variable "without" initializing it.

Please consider that this is just a curiosity question: "I'd like the
compiler to complain if the user is using the variable without initializing
it after its declaration, like for any other basic types". This is not
critical for me. If it were, I'd initialize the varible using
"A(){my_value=0;}".

Anyway, thank you for your answer.

david


reply via email to

[Prev in Thread] Current Thread [Next in Thread]