Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Number of ways to create an object

Reply
Thread Tools

Number of ways to create an object

 
 
Peter Ammon
Guest
Posts: n/a
 
      01-29-2004
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)?

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-29-2004
On Thu, 29 Jan 2004 10:40:44 -0800, Peter Ammon <(E-Mail Removed)> 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".

 
Reply With Quote
 
 
 
 
jeffc
Guest
Posts: n/a
 
      01-29-2004

"Peter Ammon" <(E-Mail Removed)> wrote in message
news:bvbk3i$r1t$(E-Mail Removed)...
> 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.


 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      01-29-2004

"Alf P. Steinbach" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> On Thu, 29 Jan 2004 10:40:44 -0800, Peter Ammon <(E-Mail Removed)> 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.


 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      01-29-2004

"Peter Ammon" <(E-Mail Removed)> wrote in message news:bvbk3i$r1t$(E-Mail Removed)...
> 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".

 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      01-29-2004
On Thu, 29 Jan 2004 10:40:44 -0800 in comp.lang.c++, Peter Ammon
<(E-Mail Removed)> 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.

 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      01-29-2004
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 "constructor" 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.



 
Reply With Quote
 
Peter van Merkerk
Guest
Posts: n/a
 
      01-29-2004
"David Harmon" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Thu, 29 Jan 2004 10:40:44 -0800 in comp.lang.c++, Peter Ammon
> <(E-Mail Removed)> 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.merkerk(at)dse.nl




 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      01-29-2004
On Thu, 29 Jan 2004 14:34:35 -0500 in comp.lang.c++, "Ron Natalie"
<(E-Mail Removed)> 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.

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-29-2004
On Thu, 29 Jan 2004 14:28:20 -0500, "Ron Natalie" <(E-Mail Removed)> wrote:

>
>"Alf P. Steinbach" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
>> On Thu, 29 Jan 2004 10:40:44 -0800, Peter Ammon <(E-Mail Removed)> 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: "constructors" ... "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).

 
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
Elegant ways to convert '' or 'number' to a number Tim McDaniel Perl Misc 21 02-18-2012 02:52 AM
Lolling at programmers, how many ways are there to create a bitmask ? ;) :) Skybuck Flying C Programming 62 06-29-2010 08:50 PM
Ways to create a variable multi-tap delay line; and if/generate usage Marty Ryba VHDL 6 08-19-2008 03:13 AM
OT: Number Nine, Number Nine, Number Nine Frisbee MCSE 37 09-26-2005 04:06 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM



Advertisments