473,606 Members | 2,756 Online
Bytes | Software Development & Data Engineering Community
+ 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 1840
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
3794
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 a single argument? I'm sorry, this explanation is just atrocious, but I can't think of exactly how to word it. Maybe an example... Take for instance Function.apply. It takes 1-2 arguments, the first being the object to use as the context, and...
2
13398
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 template and by setting output to a type library (DLL). All the object does is return a string value. At this point I have not checked the option to register for COM interop in Visual Studio So I go into Excel (where I want to use the object). Go to VB...
6
4066
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 ..../adm_std_mo_descr.h:244: warning: non-static const member `const char *const std_mo::name' in class without a constructor The header file looks as the following: typedef struct std_tee {
7
9982
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 used to post to a public folder. The component works fine when instantiated from a Windows script, but when using it from COM Interop in an ASP.NET web application it errors when trying to execute oNs.GetNameSpace("MAPI"). The error it throws...
2
1656
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 have to create a class file to host these functions. And every time when I need to call these functions, I have to create a object.
4
7639
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: http://www.c-sharpcorner.com//1/PDFFileGeneratorPKS.asp The code was published by Pramod Kumar Singh. His code didn't use any component, but it lacks the detail of the syntax
3
2831
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 additionally pictures of the product are stored in an images subfolder and the database holds the file name of the relevant picture. The user can then click a button to display the picture in a pop-up window and also another button to email the potential...
10
1850
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 Visual Studio. I understand that VS does a lot of things for us, but I want to know what those things are - and do them manually - just for the didactic benefit. Any recommendations? Thanks!
4
1536
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 class. I can make the instance of this class MyClass to be able to access the instance test in two ways.
1
4498
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 we install this MSI the dll file will be placed into the GAC and when we activate the button placed in IE tool it calls the functions in the dll using activex object. Eventhough the dll is digitally signed when we call this we are getting the...
0
8045
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7981
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8467
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8462
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8127
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8320
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6803
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5994
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3952
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.