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?
> 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.
>
> 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
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!
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).
> 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;
};
So? This is a legal definition of a B struct. At this point no A
constuctor is called, neither default nor custom. The constructors come
into a play when instantiating a B object, for example by:
B b = {10};
Note that A(int) is called here for initializing the A subobject.
Paavo