Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Explicitly calling constructor

Reply
Thread Tools

Explicitly calling constructor

 
 
Kavya
Guest
Posts: n/a
 
      10-29-2006
Suppose I have class MyClass. Then which will be termed as explicit
call to constructor.

MyClass( );

or

MyClass object;
object.MyClass( );

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-29-2006
* Kavya:
> Suppose I have class MyClass. Then which will be termed as explicit
> call to constructor.
>
> MyClass( );
>
> or
>
> MyClass object;
> object.MyClass( );


The second line of the latter isn't valid code.

The first is an example of what the standard calls an explicit
constructor call, while the first line of the second example is a
declaration with an implicit constructor call.

Used as a stand-alone expression statement (some illuminating context
would be nice!) "MyClass();" creates a temporary of MyClass type and
calls the default constructor on that temporary.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
Osamede.Zhang
Guest
Posts: n/a
 
      10-29-2006

Kavya wrote:
> Suppose I have class MyClass. Then which will be termed as explicit
> call to constructor.
>
> MyClass( );
>
> or
>
> MyClass object;

When the object was declared,it was automatic initialized by calling
class's contructor.
> object.MyClass( );


 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      10-29-2006
Kavya:

> MyClass( );



That createless a nameless object which exists until the end of the
statement. The following two snippets are equivalent:

int main()
{
int i = MyClass().GetVal();
}

int main()
{
int i;

{
MyClass nameless_object;
i = nameless_object.GetVal();
}
}

If you would like to invoke a class's constructor, you need to supply it
with a memory address at which to construct an object of the class in
question. "Placement new" is used for this:

void *const pmem = malloc(512);

MyClass *const p = ::new((void*)pmem) MyClass();

p->~MyClass();

free(pmem);

--

Frederick Gotham
 
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
How to call templated constructor explicitly? Lawrence Spector C++ 3 07-16-2007 07:16 PM
How to call a constructor explicitly if we want to allocate memory using malloc ? shsingh C++ 13 10-21-2006 01:14 PM
Explicitly calling destructor engaarea@gmail.com C++ 7 09-08-2006 02:30 PM
Calling container constructor explicitly daveb C++ 6 07-17-2006 08:58 AM



Advertisments