473,657 Members | 2,437 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Number of ways to create an object

A friend was asked "How many ways are there to create an object in C++?"
in an interview. Apparently, the "right" answer was eleven. Does
anyone know how the interviewer arrived at that number? Is this fact
well known (if true)?

Jul 22 '05 #1
22 7861
On Thu, 29 Jan 2004 10:40:44 -0800, Peter Ammon <pe*********@ro cketmail.com> wrote:
A friend was asked "How many ways are there to create an object in C++?"
in an interview.
The only meaningful answer is 1: by calling a constructor.

Lest idiots yet again object to that terminology: it's the one employed
by the Holy Standard.

Apparently, the "right" answer was eleven. Does
anyone know how the interviewer arrived at that number? Is this fact
well known (if true)?


Any number might be considered correct under suitable assumptions and
using suitable definitions, e.g. of "ways", "create", "object".

Jul 22 '05 #2

"Peter Ammon" <pe*********@ro cketmail.com> wrote in message
news:bv******** **@news.apple.c om...
A friend was asked "How many ways are there to create an object in C++?"
in an interview. Apparently, the "right" answer was eleven. Does
anyone know how the interviewer arrived at that number? Is this fact
well known (if true)?


That "fact" is not well known, if true. Sounds like one of those goofy
questions where the interviewer has a "one up" by already researching an
obscure problem ahead of time that you couldn't know. Maybe he just wanted
to see how you think about things and get the ball rolling, not come up with
"11". (Frankly, I'm reminded of This Is Spinal Tap at this moment.)

class A;

A a; // 1
A* pA = new A; // 2
f(A()); // 3
etc.
Jul 22 '05 #3

"Alf P. Steinbach" <al***@start.no > wrote in message news:40******** ********@News.C IS.DFN.DE...
On Thu, 29 Jan 2004 10:40:44 -0800, Peter Ammon <pe*********@ro cketmail.com> wrote:
A friend was asked "How many ways are there to create an object in C++?"
in an interview.
The only meaningful answer is 1: by calling a constructor.


It's wrong. You can't call the constructor. A constructor is called by the
implementation as a byproduct of object created.
Lest idiots yet again object to that terminology: it's the one employed
by the Holy Standard.


No it's not. Even with your quibbling over "is called" verses "calling"
your answer is WRONG. There are two DISTINCT steps in object
creation specifically listed in the standard:

1. First storage is obtained
2. Then construction occurs.

Your incorrect answer totally omits the first step.
Jul 22 '05 #4

"Peter Ammon" <pe*********@ro cketmail.com> wrote in message news:bv******** **@news.apple.c om...
A friend was asked "How many ways are there to create an object in C++?"
in an interview. Apparently, the "right" answer was eleven. Does
anyone know how the interviewer arrived at that number? Is this fact
well known (if true)?


Well without any better explanation of the problem, any answer might be
correct. Object creation is divided into two steps:

1. Obtaining storage.
2. Construction.

The first is divided into three main categories:

1. Static storage duration, which includes static parts of classes, static variables at namespace
scope, and static variables within functions.
2. Automatic storage duration (your non-static local variables inside functions as well as temporaries).
3. Dynamic storage duration: i.e., that obtained via new. Which includes regular single
object new expressions, array new expressions, each of which can have additional
"placement arguments"

The second phase can be divided into three groups:

1. Default initialization
2. Copy initialization
3. Direct initialization. .

I can easily come up with any number from 3 to over 24 different flavors of object creation
depending on what he means by "way".

Jul 22 '05 #5
On Thu, 29 Jan 2004 10:40:44 -0800 in comp.lang.c++, Peter Ammon
<pe*********@ro cketmail.com> was alleged to have written:
A friend was asked "How many ways are there to create an object in C++?"
in an interview. Apparently, the "right" answer was eleven. Does
anyone know how the interviewer arrived at that number? Is this fact
well known (if true)?


I guarantee that if you post his list of eleven "ways", people here will
come up with _both_ additional "ways" that are just as valid as some on
the list, and reasons why some of the eleven are really the "same thing"
and shouldn't be counted twice.

Jul 22 '05 #6
Ron Natalie wrote:
Alf P. Steinbach wrote:
The only meaningful answer is 1: by calling a constructor.


It's wrong. You can't call the constructor.
A constructor is called by the implementation
as a byproduct of object created.
Lest idiots yet again object to that terminology:
it's the one employed by the Holy Standard.


No it's not.
Even with your quibbling over "is called" verses "calling"
your answer is WRONG. There are two DISTINCT steps
in object creation specifically listed in the standard:

1. First storage is obtained
2. Then [initialization] occurs.

Your incorrect answer totally omits the first step.


Actually, these two steps define construction.
The constructor function is actually an initializer.
The compiler emits code to

1. allocate storage for the object then
2. "calls" the "constructo r" to initialize it.

You can think of a constructor for class C
as a class "friend" function:

C C(. . .);

that returns an object of type C
which can be used in an expression.

Jul 22 '05 #7
"David Harmon" <so****@netcom. com> wrote in message
news:40******** *******@news.we st.earthlink.ne t...
On Thu, 29 Jan 2004 10:40:44 -0800 in comp.lang.c++, Peter Ammon
<pe*********@ro cketmail.com> was alleged to have written:
A friend was asked "How many ways are there to create an object in C++?"
in an interview. Apparently, the "right" answer was eleven. Does
anyone know how the interviewer arrived at that number? Is this fact
well known (if true)?


I guarantee that if you post his list of eleven "ways", people here will
come up with _both_ additional "ways" that are just as valid as some on
the list, and reasons why some of the eleven are really the "same thing"
and shouldn't be counted twice.


Whatever the correct answer is (if there is any at all), who cares? I think
many skilled C++ can't answer that question, and for those who can it says
nothing about their C++ programming skills. You could question however the
C++ skills of the person who asked the question.

--
Peter van Merkerk
peter.van.merke rk(at)dse.nl


Jul 22 '05 #8
On Thu, 29 Jan 2004 14:34:35 -0500 in comp.lang.c++, "Ron Natalie"
<ro*@sensor.com > was alleged to have written:
I can easily come up with any number from 3 to over 24 different flavors of object creation
depending on what he means by "way".


Well, it would be pretty difficult to come up with eleven by your
method, since it is a prime number.

Jul 22 '05 #9
On Thu, 29 Jan 2004 14:28:20 -0500, "Ron Natalie" <ro*@sensor.com > wrote:

"Alf P. Steinbach" <al***@start.no > wrote in message news:40******** ********@News.C IS.DFN.DE...
On Thu, 29 Jan 2004 10:40:44 -0800, Peter Ammon <pe*********@ro cketmail.com> wrote:
>A friend was asked "How many ways are there to create an object in C++?"
>in an interview.
The only meaningful answer is 1: by calling a constructor.


It's wrong. You can't call the constructor.


No? See below.

A constructor is called by the implementation
The machine code that achieves that amazing feat is irrelevant.

Why do you keep on harking about the machine-code level?
as a byproduct of object created.
Lest idiots yet again object to that terminology: it's the one employed
by the Holy Standard.
No it's not.


It is, as you well know from earlier discussions. Have you forgotten? In
that case, allow me to attempt to refresh your failing (ailing?) memory:
§12.1/2 ... an explicit type conversion using the functional notation (5.2.3)
will cause a constructor to be CALLED to initialize an object.

§12.1/5 A default constructor for a class X is a constructor of class X that
can be CALLED without an argument.

§12.1/8 Default constructors are CALLED implicitly to create class objects
of automatic storage duration (3.7.1, 3.7.2) defined without an
initializer (8.5), are CALLED to create class objects of dynamic
storage duration (3.7.3) created by a new-expression in which the
new-initializer is omitted (5.3.4), or are CALLED when the explicit
type conversion syntax (5.2.3) is used.

--> Note especially the distinction here between CALLED IMPLICITLY
and simply CALLED. Perhaps you've been misled to think the
former is always the case, and that's what your apparent
hang-up about the machine code level is about?

--> Note also: "constructo rs" ... "are CALLED".
§12.1/9 [Note: 12.6.2 described the order in which constructors for base
classes and non-static data members CALLED and describes how arguments
can be specified for the CALLS to these constructors.]

§12.1/13 ... [Note: explicit constructor CALLS do not yield lvalues, see 3.10].
And so on.

Feel free to CALL it anything you like.

But don't feed claims about constructors being non-callable to the newsgroup.

Even with your quibbling over "is called" verses "calling"
your answer is WRONG. There are two DISTINCT steps in object
creation specifically listed in the standard:

1. First storage is obtained
2. Then construction occurs.

Your incorrect answer totally omits the first step.

"Create" can be subdivided, and further prerequisites added, so what?

I applaud the insight you've had that there must be some storage.

On the other hand, logic is otherwise totally absent in your reply; in
particular, I did not omit or include any details about creation other
than the need for a constructor call (in the cases of the only reasonable
interpretation of "object" in the question at hand).

Jul 22 '05 #10

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

Similar topics

9
32594
by: Rune Strand | last post by:
Hi, If I have a lot of integers and want do something with each digit as integer, what is the fastest way to get there? Eg. Make 12345 into an iterable object, like or "12345" (Btw: What is the English term for this process; itemize? tokenize? digitize? sequence?) Some examples:
5
3339
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible double value in the range could come up with equal probability). I'd also like to be able to seed this generator (e.g., via the clock) so that the same sequence of random values don't come up every time. Anybody have an easy and fast...
7
1989
by: J-T | last post by:
I can instantiate my object in my *ASP.NET* application in two ways: A) public sealed class RSSingleton { private static ReportingServiceProxy m_RsProxy=null; static RSSingleton() { m_RsProxy = new ReportingServiceProxy();
3
1271
by: GregO | last post by:
Hi, I would like to find out how many sessions are currently active for my web site. I can't add any counters to the session start event , is there a way to access the ASP.NET engine and find this information out? Or is there other ways? Kind regards Greg
5
4011
by: JSParker1 | last post by:
Summary: Maximum number of records per second that can be inserted into SQLServer 2000. I am trying to insert hundreds (preferably even thousands) of records per second in to SQLServer table (see below) but I am getting the following error in the Windows Event Viewer Application log file: "Insufficent Memory......" And very few records were inserted and no errors where sent back via
22
1698
by: joh12005 | last post by:
hello, i'm looking for a way to have a list of number grouped by consecutive interval, after a search, for example : => , , , ]
5
5481
by: BD | last post by:
I am coding with C# in Visual Studio 2005 for a database application residing on remote MS SQL Server 2005. What I want to do is open the same form but from 2 different places and only one instance. I currently have the form where it will open from a double click on datagridview from another form to the proper record and only one instance. However, doing this bypassed by databinding source for opening the form to all records. I still...
19
107938
Frinavale
by: Frinavale | last post by:
Filtering user input is extremely important for web programming. If input is left unfiltered users can input malicious code that can cripple your website. This article will explain how to make sure that the user only submits a number to your .NET web application and also demonstrate how to add JavaScript to your ASPX pages. Upon popular demand, I have added a section that also covers how to use a validator control to check if a text box...
6
4800
by: Smithers | last post by:
Just looking to compile a list of "all the ways to implement events". I'm NOT looking to get into the merits or mechanics of each in this thread... just want to identify them all - good, bad, and ugly. Here's what I have so far 1. Implement via the 'event' keyword (with associated delegate, etc) 2. Expose a private delegate via a public property (really a "roll yer own" version of the 'event' keyword method).
0
8395
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
8732
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
8503
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
8605
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
7330
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...
0
5632
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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 we have to send another system
2
1615
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.