Click to See Complete Forum and Search --> : Question about using constructor to do type conversion.
George2
April 15th, 2003, 02:09 AM
Hi, everyone!
I have a sample about using constructor to do type conversion.
In my sample, I change an int to type "A". I just want to learn
more deeply into this topic -- using constructor to do type
conversion. Are there some online materials that I can make a
reference?
BTW: Is the topic in Bjarne Stroustrup's "The C++ Programming language
special edition"? I can not find that topic in this book.
Here is my example:
--------
#include <iostream.h>
class A {
private:
int a;
public:
A (int a)
{
this->a = a;
}
void output()
{
cout << "a is: " << this->a << endl;
}
};
void functionA (A a)
{
a.output();
}
int main()
{
functionA (10);
return 1;
}
--------
Thanks in advance,
George
Gabriel Fleseriu
April 15th, 2003, 02:24 AM
Yes, a constructor like that will be used for implicit conversions unless it is declared explicit.
class A
{
public:
A():i_(0){}
A(int i):i_(i){}
private:
int i_;
};
class B
{
public:
B():i_(0){}
explicit B(int i):i_(i){}
private:
int i_;
};
int main()
{
A a = 1; // ok (1)
B b = 1; // error (2)
return 0;
}
On the line marked (1) the integer 1 is implicitely converted to a temporary A using A's second ctor. Then the implicit copy ctor of A is used to initialize a.
On the line marked (2) a temporary B object cannot be created, because the ctor is declared explicit, thus the error.
Take care when using ctors to do implicit conversions (and also when writing cast operators) because they can lead to subtle bugs.
George2
April 15th, 2003, 02:27 AM
Thanks, Gabriel buddie!
George
Originally posted by Gabriel Fleseriu
Yes, a constructor like that will be used for implicit conversions unless it is declared explicit.
class A
{
public:
A():i_(0){}
A(int i):i_(i){}
private:
int i_;
};
class B
{
public:
B():i_(0){}
explicit B(int i):i_(i){}
private:
int i_;
};
int main()
{
A a = 1; // ok (1)
B b = 1; // error (2)
return 0;
}
On the line marked (1) the integer 1 is implicitely converted to a temporary A using A's second ctor. Then the implicit copy ctor of A is used to initialize a.
On the line marked (2) a temporary B object cannot be created, because the ctor is declared explicit, thus the error.
Take care when using ctors to do implicit conversions (and also when writing cast operators) because they can lead to subtle bugs.
Graham
April 15th, 2003, 03:04 AM
To expand on Gabriel's answer:
The point about using "explicit" on a ctor is just that - if you want the type conversion, then you have to be explicit about it:
B b = B(1); // OK
or:
void f(A a);
void g(B b);
int main()
{
f(1); // OK, but a potential bug
g(B(1)); // Explicit type conversion, so less likely to cause a bug.
}
Note that "explicit" is only of value if the ctor can be called with a single argument. If it has no arguments, or it needs two or more, then it can't be involved in implicit type conversion.
class foo
{
public:
foo();
explicit foo(int);
explicit foo(char c = 'a', int i); // could be called with single char arg.
foo(int, int);
};
Gabriel Fleseriu
April 15th, 2003, 03:08 AM
Originally posted by Graham
class foo
{
public:
foo();
explicit foo(int);
explicit foo(char c = 'a', int i); // could be called with single char arg.
foo(int, int);
};
Am I wrong assuming that the parameters that have a default value must come last (or, the other way around, those that don't have default values first)?
George2
April 15th, 2003, 03:23 AM
Thanks, Graham buddie!
Your experience is valuable to me!
regards,
George
Originally posted by Graham
To expand on Gabriel's answer:
The point about using "explicit" on a ctor is just that - if you want the type conversion, then you have to be explicit about it:
B b = B(1); // OK
or:
void f(A a);
void g(B b);
int main()
{
f(1); // OK, but a potential bug
g(B(1)); // Explicit type conversion, so less likely to cause a bug.
}
Note that "explicit" is only of value if the ctor can be called with a single argument. If it has no arguments, or it needs two or more, then it can't be involved in implicit type conversion.
class foo
{
public:
foo();
explicit foo(int);
explicit foo(char c = 'a', int i); // could be called with single char arg.
foo(int, int);
};
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.