Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > newbie question about constructor

Reply
Thread Tools

newbie question about constructor

 
 
sam
Guest
Posts: n/a
 
      12-29-2006
Hi,
Why declare constructors implicit or explicit ,whats the use of it?

 
Reply With Quote
 
 
 
 
wittempj@hotmail.com
Guest
Posts: n/a
 
      12-29-2006


On Dec 29, 4:52 pm, "sam" <(E-Mail Removed)> wrote:
> Hi,
> Why declare constructors implicit or explicit ,whats the use of it?


See http://www.parashift.com/c++-faq-lit....html#faq-10.1

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      12-29-2006
sam wrote:

> Hi,
> Why declare constructors implicit or explicit ,whats the use of it?


Constructors that can be called with one argument are conversion
constructors. If the constructor is not declared 'explicit', the compiler
will do implicit conversions using that constructor.

struct X{};
struct Y{};

class MyClass
{
public:
MyClass(X);
explicit MyClass(Y);
};

void myfunc(MyClass);

int main()
{
X x;
Y y;

myfunc(x); // ok, conversion constructor is implicitly used
myfunc(y); // error, constructor is explicit
myfunc(static_cast<MyClass>(y)); // ok, explicit conversion
}

Btw: What C++ book are you reading that doesn't explain this?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
NewBie - java constructor error can't find constructor psmith@mcwy.com Java 6 02-20-2007 04:29 AM
Constructor question, how does the call to the parent constructor work? marcwentink@hotmail.com C++ 6 05-09-2006 07:19 AM
calling a constructor within a constructor Brett Irving C++ 3 06-29-2003 10:43 AM
why it's not possible calling constructor from constructor? Giulio C++ 9 06-25-2003 03:56 PM



Advertisments