473,480 Members | 3,069 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Without using constructor, we can create object in following way?

1 New Member
Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3.        .....
  4.        void getdata(int x, int y)
  5.        {
  6.            ..........
  7.        }
  8.        .....
  9.        .....
  10. }
  11. main()
  12. {
  13.        A a1.getdata(10,20), a2.getdata(20,40);
  14.        .....
  15. }
Dec 18 '12 #1
2 1834
Banfa
9,065 Recognized Expert Moderator Expert
The only way to create an object without calling a constructor for it would be to allocate an array of memory large enough and then point a pointer to the object at the array, however that is extremely in advisable.

Expand|Select|Wrap|Line Numbers
  1. char *buffer = reinterpret_cast<char *>(malloc(sizeof A));
  2. A* pa = reinterpret_cast<A *>(buffer);
  3.  
Even this, due to object alignment may not work without further fiddling to ensure the object alignment was correct.

This syntax

Expand|Select|Wrap|Line Numbers
  1.     A a.getdata(0, 1);
  2.  
is not valid and even if it were valid it would call the default constructor of A first. The closest you could get to this syntax is

Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3. public:
  4.     A& getdata(int, int)
  5.   {
  6.         return *this;
  7.   }    
  8.  
  9. };
  10.  
  11. int main()
  12. {
  13.     A a = A().getdata(0, 1);
  14.  
  15.     return 0;
  16. }
  17.  
which calls both the default and copy constructors of A
Dec 18 '12 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
Constructors do not create objects.

Only the compiler can create objects. Either on its own or by your command.

After the compiler allocates the memory, the constructors for each data member are called to initialize the memory.

In this example. it's odd that A::getdata does not get the data since the return is void.
Dec 18 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

9
3780
by: Matt Eberts | last post by:
Sorry, bad title. Anyway, is there a way to pass the arguments to an object instantiated via a constructor using the arguments object and have it expanded, so to speak, so that it doesn't appear as...
2
13364
by: brazilnut52 | last post by:
I am going to outline the steps I go through to produce the problem. Hopefully this will help you understand the problem better I have created a simple COM DLL in .NET by using the COM class...
6
4049
by: zhixin_han | last post by:
Following warnings are found in my application: ..../adm_std_mo_descr.h:240: warning: non-static const member `const struct std_tee_t std_mo::tee' in class without a constructor...
7
9973
by: Munsifali Rashid | last post by:
Hi, I've built a webservice in C# that can add public folders to Exchange. There's also a component built in VB6 which references the Outlook 10 (XP) DLL, so I can change the default form being...
2
1646
by: Henry | last post by:
Hi guys, I want to write some global functions which can be called from different asp.net page. In Visual Basic, there is a global module which allow me to do that. In Visual Basic .net, I...
4
7612
by: Hongbo | last post by:
Hi, I'd like to create PDF file with tables in ASP.NET application in C# without using any third party component. I did some search online and only found the following link:...
3
2816
by: Siv | last post by:
Hi, A little while ago I wrote a small program that allowed the user to view products from a database. The database holds the details of the products which can be viewed via a form and...
10
1841
by: Frankie | last post by:
I am looking for tutorials, articles, etc that can help to enlighten me on what's "really going on" or what's required to create a .NET application (particularly Windows Forms) WITHOUT the use of...
4
1528
by: Tony Johansson | last post by:
Hello! I have a class definition called MyClass see below. I create an instance of this class MyClass I also want this instance to be able to modify the test instance that exist in this...
1
4492
by: kishorealla | last post by:
Hello, We have digitally signed one of our activex component (dll) using thawte code signing certificate for microsoft autheticode. We packaged this dll in a MSI which is an IE browser tool. When...
0
7055
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7060
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7022
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...
1
4799
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
4501
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
1311
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 ...
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
206
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.