473,320 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Is a constructor a function?

Hidden in another thread, the question has come up:

"Is a constructor a function?"

(My claim is "no", though some have disagreed.)

Many thanks,
--ag

[Yes, I know what they do and how they are invoked. I'm trying to get an
answer from a `language lawyer' perspective.]
--
Artie Gold -- Austin, Texas

Jul 19 '05 #1
12 2227
In article <3F**************@austin.rr.com>, ar*******@austin.rr.com
says...
Hidden in another thread, the question has come up:

"Is a constructor a function?"


Yes, a ctor is a function, but (despite some appearances to the
contrary) does not have a name. The relevant part of the standard is
section 12.1, in case you care to look at it in more detail.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #2
On Wed, 06 Aug 2003 13:05:26 GMT, Jerry Coffin <jc*****@taeus.com> wrote:
Yes, a ctor is a function, but (despite some appearances to the
contrary) does not have a name.


Well, that's not entirely true. The ctor can't be called explicitly except
during construction because it has no name visible to the code, but it does
have a name through name mangling. i.e. the constructors

class foo
{
public:
foo();
foo(int i);
};

are two different functions, and have distinctive names as far as the linker
is concerned.

Jul 19 '05 #3
On Wed, 06 Aug 2003 00:37:42 GMT, Artie Gold <ar*******@austin.rr.com>
wrote:
"Is a constructor a function?"

(My claim is "no", though some have disagreed.)

Yes, a constructor is a function. If a constructor weren't a
function, then what exactly would it be?

More to the point... 12:1: "The default constructor (12.1), copy
constructor and copy assignment operator (12.8), and destructor (12.4)
are special member functions."

In addition, 12.1:1: "Constructors do not have names" and 12.1:2:
"...Becasue constructors do not have names, they are never found
during name lookup...," meaning they can't be called explicitly. Code
such as:

class A { /* ... */ } ;

A();

is legal because it is allowed by 12.1:13: "A functional notation type
conversion can be used to create new objects of its type. [Note: The
syntax looks like an erxplicit call of the constructor]"
</dib>
John Dibling
Witty banter omitted for your protection
Jul 19 '05 #4
John Dibling wrote:
On Wed, 06 Aug 2003 00:37:42 GMT, Artie Gold <ar*******@austin.rr.com>
wrote:

"Is a constructor a function?"

(My claim is "no", though some have disagreed.)


Yes, a constructor is a function. If a constructor weren't a
function, then what exactly would it be?

More to the point... 12:1: "The default constructor (12.1), copy
constructor and copy assignment operator (12.8), and destructor (12.4)
are special member functions."

In addition, 12.1:1: "Constructors do not have names" and 12.1:2:
"...Becasue constructors do not have names, they are never found
during name lookup...," meaning they can't be called explicitly. Code
such as:

class A { /* ... */ } ;

A();

is legal because it is allowed by 12.1:13: "A functional notation type
conversion can be used to create new objects of its type. [Note: The
syntax looks like an erxplicit call of the constructor]"
</dib>
John Dibling
Witty banter omitted for your protection


Thanks all. I wanted chapter and verse. I got chapter and verse.

;-)

--ag

--
Artie Gold -- Austin, Texas

Jul 19 '05 #5
In article <9m********************************@4ax.com>, as*@me.com
says...
On Wed, 06 Aug 2003 13:05:26 GMT, Jerry Coffin <jc*****@taeus.com> wrote:
Yes, a ctor is a function, but (despite some appearances to the
contrary) does not have a name.
Well, that's not entirely true.


Yes, it is entirely true. The precise quote from the C++ standard
(section 12.1/1, sentence 1) is: "Constructors do not have names."

[ ... ]
foo();
foo(int i);
};

are two different functions, and have distinctive names as far as the linker
is concerned.


This has on relevance. When you compile and if/then/else statement, the
output of the compiler will typically include (at least) a couple of
named labels, but this doesn't mean that an if/then/else statement has a
name -- only that the compiler typically uses some names to implement
it. The same is true with ctors -- yes, the compiler typically
synthesizes names for the pieces of code that implement them, but this
means nothing about the ctors themselves.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #6
John Dibling wrote:
On Wed, 06 Aug 2003 00:37:42 GMT, Artie Gold <ar*******@austin.rr.com>
wrote:
"Is a constructor a function?"

(My claim is "no", though some have disagreed.)

Yes, a constructor is a function. If a constructor weren't a
function, then what exactly would it be?


It would be.... well, a constructor. If the C++ standard says that it
sees a constructor as a function, then there is not much to say against
it wrt. C++, but actually, that term isn't really correct, since a
constructor is missing an important element that a function must have:
a return type.

Jul 19 '05 #7


Dave Rahardja wrote:

On Wed, 06 Aug 2003 13:05:26 GMT, Jerry Coffin <jc*****@taeus.com> wrote:
Yes, a ctor is a function, but (despite some appearances to the
contrary) does not have a name.


Well, that's not entirely true. The ctor can't be called explicitly except
during construction because it has no name visible to the code, but it does
have a name through name mangling. i.e. the constructors

class foo
{
public:
foo();
foo(int i);
};

are two different functions, and have distinctive names as far as the linker
is concerned.


The linker is not part of standard C++.
In standard C++, a constructor has no name and thus cannot
be called.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #8
Artie Gold <ar*******@austin.rr.com> wrote in message
news:3F**************@austin.rr.com...

Thanks all. I wanted chapter and verse. I got chapter and verse.


Remember, you can get your own copy of the "bible"
for only 18 dollars. http://tinyurl.com/31yw

-Mike

Jul 19 '05 #9

Rolf Magnus <ra******@t-online.de> wrote in message
news:bg*************@news.t-online.com...
John Dibling wrote:
On Wed, 06 Aug 2003 00:37:42 GMT, Artie Gold <ar*******@austin.rr.com>
wrote:
"Is a constructor a function?"

(My claim is "no", though some have disagreed.)

Yes, a constructor is a function. If a constructor weren't a
function, then what exactly would it be?


It would be.... well, a constructor. If the C++ standard says that it
sees a constructor as a function, then there is not much to say against
it wrt. C++, but actually, that term isn't really correct, since a
constructor is missing an important element that a function must have:
a return type.


Here's how I see it:

While the syntax for a ctor declaration does not
include a formal 'return type', it does return the type
of object for which the ctor was defined.

class T
{
int member;
public:
T(int arg) : member(arg) {}
};

T obj(42);

'obj' is initialized with value "returned"
by 'T::T()', (not with 42, as the syntax
seems to indicate.)

-Mike

Jul 19 '05 #10
On Wed, 06 Aug 2003 17:39:43 +0200, Karl Heinz Buchegger <kb******@gascad.at>
wrote:
The linker is not part of standard C++.
In standard C++, a constructor has no name and thus cannot
be called.


Point taken!

Jul 19 '05 #11
Artie Gold wrote:
Hidden in another thread, the question has come up:

"Is a constructor a function?"

(My claim is "no", though some have disagreed.)


According to Bjarne Stroustrup,
"The C++ Programming Language: Third Edition",
Chapter2 A Tour of C++, Section 5 Data Abstraction,
Subsection 2 User-Defined Types, page 32:

"A member function with the same name as its class
is called a constructor. A constructor defines a way
to initialize an object of its class."

Subsection 3 Concrete Types, page 33:

"The constructor Stack(int) will be called
whenever an object of the class is created.
This takes care of initialization."

Jul 19 '05 #12
Artie Gold wrote:
Hidden in another thread, the question has come up:

"Is a constructor a function?"

(My claim is "no", though some have disagreed.)


According to Bjarne Stroustrup,
"The C++ Programming Language: Third Edition",
Chapter2 A Tour of C++, Section 5 Data Abstraction,
Subsection 2 User-Defined Types, page 32:

"A member function with the same name as its class
is called a constructor. A constructor defines a way
to initialize an object of its class."

Subsection 3 Concrete Types, page 33:

"The constructor Stack(int) will be called
whenever an object of the class is created.
This takes care of initialization."

Jul 19 '05 #13

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

Similar topics

3
by: Jun | last post by:
I have following script <script> var Animal = function(name){ this.name = name; } Animal.prototype.eat = function (food) {
34
by: Andy | last post by:
1) Is there any use of defining a class with a single constructor declared in private scope? I am not asking a about private copy constructors to always force pass/return by reference. 2) Is...
2
by: vakap | last post by:
function show() { var s = '' ; for (var i = 0; i<arguments.length; s += '\n'+arguments) ; typeof(window) != 'undefined' ? window.alert(s) : WScript.Echo(s) ; } function f(){}...
10
by: John Brock | last post by:
I have a base class with several derived classes (I'm writing in VB.NET). I want each derived class to have a unique class ID (a String), and I want the derived classes to inherit from the base...
19
by: Martin Oddman | last post by:
Hi, I have a compiling problem. Please take a look at the code below. I have an application that is built upon three tiers: one data tier (Foo.DataManager), one business tier (Foo.Kernel) and...
16
by: plmanikandan | last post by:
Hi, I have doubts reg virtual constructor what is virtual constructor? Is c++ supports virtual constructor? Can anybody explain me about virtual constructor? Regards, Mani
26
by: Patient Guy | last post by:
The code below shows the familiar way of restricting a function to be a method of a constructed object: function aConstructor(arg) { if (typeof(arg) == "undefined") return (null);...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
9
by: Morten Lemvigh | last post by:
Is it possible to pass a pointer to a constructor or a class definition as argument to a function? Maybe in a way similar to passing function pointers...? The function should construct a number...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.