[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: uninitializing constructor
From: |
jinxidoru |
Subject: |
Re: uninitializing constructor |
Date: |
7 Oct 2006 16:41:34 -0700 |
User-agent: |
G2/1.0 |
If you just want a warning whenever someone calls the default
constructor, you could also declare the constructor as deprecated (I
think) using a function attribute.
david wrote:
> 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