473,480 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Explicitly calling constructor

Suppose I have class MyClass. Then which will be termed as explicit
call to constructor.

MyClass( );

or

MyClass object;
object.MyClass( );

Oct 29 '06 #1
3 4950
* 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?
Oct 29 '06 #2

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( );
Oct 29 '06 #3
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
Oct 29 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
27738
by: Giulio | last post by:
why definition of two constructors like these is not possible in c++??? ----------------------- date::date(const int d, const int m, const int y, const int ora, const int mi, const int se){...
21
17827
by: Philipp | last post by:
Hello, a very simple question: Ok I have a class MyClass with a constructor MyClass(int) (no constructor without argument defined) how can I make an array of pointers to objects of that class,...
1
1261
by: tsteinke | last post by:
Doe anyone know the way to explicitly call the varible initializers on an object? for instance Class A { private int a=123; }
5
1477
by: lallous | last post by:
Hello, Probably I am not seeing it now, but is it possible for constructor(int) to call constructor() ? class A { private: int x, y; public:
9
1312
by: Gonçalo Rodrigues | last post by:
Hi all, Is it possible to call a constructor of a class, call it Object, explicitely? e.g. suppose you have a void pointer ptr pointing to a block of memory big-enough to hold an Object. Is...
2
3839
by: Josef Meile | last post by:
Hi, I have this constructor: public CExcelDatabase(string host, string user, string password, string database, bool promptCredentials, int findExcelInstance, bool readOnly) { //Some code...
2
2378
by: a | last post by:
Hi Extremely easy question for someone who knows the answer I'm sure :-) (aren't they all) What exactly does LoadControl do? Whats the difference between calling this and just constructing a...
4
4937
by: Michael | last post by:
Hello, I want to use an object (LowCut) within another object (SampleRateConverter) like it is written as follows: class SampleRateConverter { public: SampleRateConverter( int...
2
3193
by: Studlyami | last post by:
After i create an object can i call the constructor again to reinitialize that objects variables? I know i can create a function to do this, but i was wonder if i can just call the constructor again....
0
6911
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
6966
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5344
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4787
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4488
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2999
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2988
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.