No call to default constructor

Discussion in 'C++' started by Tagore, Oct 26, 2009.

  1. Tagore

    Tagore Guest

    hi,
    consider following class:
    class A
    {
    int i;
    public:
    A(int k)
    { i=k;}
    };
    Now I can create an object of class in 2 ways:
    A a; //first method
    A a(10); //second method

    In first method, a default constructor implicitly defined is called.
    but value of i remains garbage. and instance is not consistent.
    How can I enforce that this default constructor is not called and
    first method generates an error.

    SECOND QUERY
    I have seen that I can also define constructor A as
    A(int k):i(k)
    { }
    How this method differs from one define earlier?
    Tagore, Oct 26, 2009
    #1
    1. Advertising

  2. Tagore

    Pavel Guest

    Paavo Helde wrote:
    > Tagore <> wrote in news:7700e2af-a00b-44bf-9d5a-
    > :
    >
    >> hi,
    >> consider following class:
    >> class A
    >> {
    >> int i;
    >> public:
    >> A(int k)
    >> { i=k;}
    >> };
    >> Now I can create an object of class in 2 ways:
    >> A a; //first method
    >> A a(10); //second method
    >>
    >> In first method, a default constructor implicitly defined is called.
    >> but value of i remains garbage. and instance is not consistent.
    >> How can I enforce that this default constructor is not called and
    >> first method generates an error.

    >
    > You have just done that, by defining another constructor A(int).


    Another
    > option would be to declare a private constructor A(), and not provide a
    > definition for it.

    As OP needs an error (compilation error, I guess), only this second
    method you mentioned will probably suit him. What he has done prevented
    the default constructor from being supplied by implementation but still
    allowed the definition without initializers, like "A a;" above to get
    compiled.


    >
    >
    >> SECOND QUERY
    >> I have seen that I can also define constructor A as
    >> A(int k):i(k)
    >> { }
    >> How this method differs from one define earlier?

    >
    > In the first approach, i is uninitialized in the beginning of the
    > constructor body. No practical difference here as it will be initialized
    > in the first statement. AFAIK a good style is to initialize all data in
    > the initialization list though, if not by other reasons then just for
    > being explicit about what is initialization and what is extra work done
    > by the constructor.
    >
    > Also, for most non-POD types the first approach would mean duplicate
    > initialization, first by the default constructor, then assigned the
    > actually needed value later. For some classes (like your A, when embedded
    > in another class) this would not even compile as they can't be default-
    > constructed.
    >
    > hth
    > Paavo
    Pavel, Oct 27, 2009
    #2
    1. Advertising

  3. Pavel wrote:
    > Paavo Helde wrote:
    >> Tagore <> wrote in news:7700e2af-a00b-44bf-9d5a-
    >> :
    >>
    >>> hi,
    >>> consider following class:
    >>> class A
    >>> {
    >>> int i;
    >>> public:
    >>> A(int k)
    >>> { i=k;}
    >>> };
    >>> Now I can create an object of class in 2 ways:
    >>> A a; //first method
    >>> A a(10); //second method
    >>>
    >>> In first method, a default constructor implicitly defined is called.
    >>> but value of i remains garbage. and instance is not consistent.
    >>> How can I enforce that this default constructor is not called and
    >>> first method generates an error.

    >>
    >> You have just done that, by defining another constructor A(int).

    >
    > Another
    >> option would be to declare a private constructor A(), and not provide
    >> a definition for it.

    > As OP needs an error (compilation error, I guess), only this second
    > method you mentioned will probably suit him. What he has done prevented
    > the default constructor from being supplied by implementation but still
    > allowed the definition without initializers, like "A a;" above to get
    > compiled.
    >


    Trying to compile (using g++ 4.3.0) this:
    class A
    {
    int i;
    public:
    A(int k)
    { i=k;}
    };

    int main()
    {
    A a; //first method
    A b(10); //second method
    }

    I am getting this:
    g++ gve.cpp
    gve.cpp: In function ‘int main()’:
    gve.cpp:12: error: no matching function for call to ‘A::A()’
    gve.cpp:6: note: candidates are: A::A(int)
    gve.cpp:3: note: A::A(const A&)


    So, how are you compiling to prevent that happening??


    --
    Bolje je ziveti sto godina kao bogatun, nego jedan dan kao siromah!
    Vladimir Jovic, Oct 27, 2009
    #3
  4. Tagore

    Fred Zwarts Guest

    Tagore wrote:
    > hi,
    > consider following class:
    > class A
    > {
    > int i;
    > public:
    > A(int k)
    > { i=k;}
    > };
    > Now I can create an object of class in 2 ways:
    > A a; //first method


    This will not work in your case. Since you defined a constructor,
    the compiler does not supply a default constructor.

    > A a(10); //second method
    >
    > In first method, a default constructor implicitly defined is called.
    > but value of i remains garbage. and instance is not consistent.


    Therefore, the compiler will generate an error if you try to use this
    option when another constructor is defined in your class.

    > How can I enforce that this default constructor is not called and
    > first method generates an error.


    You did that already.

    >
    > SECOND QUERY
    > I have seen that I can also define constructor A as
    > A(int k):i(k)
    > { }
    > How this method differs from one define earlier?


    In the example at the top of the message,
    you do not realy initialize i. The variable i is initialized
    by the compiler with a default value and then you assign a value (k) to it.
    In the example at the bottom, you properly initialize the value.
    The difference may be clearer if a "const int i" was used in the class.
    Than only the second option (initialization) is possible.
    Assignement, the first option, is not possible for const values.

    It is the same difference as in a function body, where you can either use

    int i;
    i = 10;

    or

    int i = 10;

    At the end it will have the same effect, but formally only the second option
    is a proper initialization (which can also be used for a const int).
    Fred Zwarts, Oct 27, 2009
    #4
  5. Vladimir Jovic wrote:

    > Pavel wrote:
    >> Paavo Helde wrote:
    >>> Tagore <> wrote in news:7700e2af-a00b-44bf-9d5a-
    >>> :
    >>>
    >>>> hi,
    >>>> consider following class:
    >>>> class A
    >>>> {
    >>>> int i;
    >>>> public:
    >>>> A(int k)
    >>>> { i=k;}
    >>>> };
    >>>> Now I can create an object of class in 2 ways:
    >>>> A a; //first method
    >>>> A a(10); //second method
    >>>>
    >>>> In first method, a default constructor implicitly defined is called.
    >>>> but value of i remains garbage. and instance is not consistent.
    >>>> How can I enforce that this default constructor is not called and
    >>>> first method generates an error.
    >>>
    >>> You have just done that, by defining another constructor A(int).

    >>
    >> Another
    >>> option would be to declare a private constructor A(), and not provide
    >>> a definition for it.

    >> As OP needs an error (compilation error, I guess), only this second
    >> method you mentioned will probably suit him. What he has done prevented
    >> the default constructor from being supplied by implementation but still
    >> allowed the definition without initializers, like "A a;" above to get
    >> compiled.
    >>

    >
    > Trying to compile (using g++ 4.3.0) this:
    > class A
    > {
    > int i;
    > public:
    > A(int k)
    > { i=k;}
    > };
    >
    > int main()
    > {
    > A a; //first method
    > A b(10); //second method
    > }
    >
    > I am getting this:
    > g++ gve.cpp
    > gve.cpp: In function ‘int main()’:
    > gve.cpp:12: error: no matching function for call to ‘A::A()’
    > gve.cpp:6: note: candidates are: A::A(int)
    > gve.cpp:3: note: A::A(const A&)
    >
    >
    > So, how are you compiling to prevent that happening??
    >

    struct B {
    A a;
    };
    Johannes Schaub (litb), Oct 27, 2009
    #5
  6. Tagore

    Pavel Guest

    Vladimir Jovic wrote:
    > Pavel wrote:
    >> Paavo Helde wrote:
    >>> Tagore <> wrote in news:7700e2af-a00b-44bf-9d5a-
    >>> :
    >>>
    >>>> hi,
    >>>> consider following class:
    >>>> class A
    >>>> {
    >>>> int i;
    >>>> public:
    >>>> A(int k)
    >>>> { i=k;}
    >>>> };
    >>>> Now I can create an object of class in 2 ways:
    >>>> A a; //first method
    >>>> A a(10); //second method
    >>>>
    >>>> In first method, a default constructor implicitly defined is called.
    >>>> but value of i remains garbage. and instance is not consistent.
    >>>> How can I enforce that this default constructor is not called and
    >>>> first method generates an error.
    >>>
    >>> You have just done that, by defining another constructor A(int).

    >>
    >> Another
    >>> option would be to declare a private constructor A(), and not provide
    >>> a definition for it.

    >> As OP needs an error (compilation error, I guess), only this second
    >> method you mentioned will probably suit him. What he has done
    >> prevented the default constructor from being supplied by
    >> implementation but still allowed the definition without initializers,
    >> like "A a;" above to get compiled.
    >>

    >
    > Trying to compile (using g++ 4.3.0) this:
    > class A
    > {
    > int i;
    > public:
    > A(int k)
    > { i=k;}
    > };
    >
    > int main()
    > {
    > A a; //first method
    > A b(10); //second method
    > }
    >
    > I am getting this:
    > g++ gve.cpp
    > gve.cpp: In function ‘int main()’:
    > gve.cpp:12: error: no matching function for call to ‘A::A()’
    > gve.cpp:6: note: candidates are: A::A(int)
    > gve.cpp:3: note: A::A(const A&)
    >
    >
    > So, how are you compiling to prevent that happening??
    >
    >

    You are right, this must not compile -- sorry for the confusion.
    -Pavel
    Pavel, Oct 31, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aire
    Replies:
    3
    Views:
    453
    Mike Wahler
    Jan 25, 2004
  2. Replies:
    9
    Views:
    942
    Alf P. Steinbach
    Mar 6, 2006
  3. Replies:
    4
    Views:
    403
    Salt_Peter
    Oct 12, 2006
  4. desktop
    Replies:
    2
    Views:
    446
    Victor Bazarov
    Jun 6, 2007
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,197
Loading...

Share This Page