|
-
April 15th, 2003, 02:09 AM
#1
Question about using constructor to do type conversion.
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
-
April 15th, 2003, 02:24 AM
#2
Yes, a constructor like that will be used for implicit conversions unless it is declared explicit.
Code:
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.
-
April 15th, 2003, 02:27 AM
#3
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.
Code:
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.
-
April 15th, 2003, 03:04 AM
#4
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:
or:
Code:
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.
Code:
class foo
{
public:
foo();
explicit foo(int);
explicit foo(char c = 'a', int i); // could be called with single char arg.
foo(int, int);
};
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
April 15th, 2003, 03:08 AM
#5
Originally posted by Graham
Code:
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)?
-
April 15th, 2003, 03:23 AM
#6
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:
or:
Code:
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.
Code:
class foo
{
public:
foo();
explicit foo(int);
explicit foo(char c = 'a', int i); // could be called with single char arg.
foo(int, int);
};
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|