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?

Advertisements

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

Advertisements

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.
>

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).

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!