Is a constructor a function?

A

Artie Gold

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.]
 
J

Jerry Coffin

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.
 
D

Dave Rahardja

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.
 
J

John Dibling

"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
 
A

Artie Gold

John said:
"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
 
J

Jerry Coffin

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.
 
R

Rolf Magnus

John said:
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.
 
K

Karl Heinz Buchegger

Dave said:
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.
 
M

Mike Wahler

Rolf Magnus said:
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
 
E

E. Robert Tisdale

Artie said:
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."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,351
Latest member
LolaD32479

Latest Threads

Top