No call to default constructor

T

Tagore

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?
 
P

Pavel

Paavo said:
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.
 
V

Vladimir Jovic

Pavel said:
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??
 
F

Fred Zwarts

Tagore said:
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).
 
J

Johannes Schaub (litb)

Vladimir said:
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;
};
 
P

Pavel

Vladimir said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top