What do they call this kind of constructor?

J

Jason Heyes

Is there a special name for a constructor that does struct-like
initialisation? Example:

class DefaultCtor
{
std::string class_name;
bool compiler_generated;

public:
DefaultCtor(std::string class_name, bool compiler_generated);

};

DefaultCtor::DefaultCtor(std::string class_name_, bool compiler_generated_)
: class_name(class_name_), compiler_generated(compiler_generated_)
{
}

Thanks.
 
V

Victor Bazarov

Jason said:
Is there a special name for a constructor that does struct-like
initialisation?

What's a "struct-like initialisation"?
Example:

class DefaultCtor
{
std::string class_name;
bool compiler_generated;

public:
DefaultCtor(std::string class_name, bool compiler_generated);

};

DefaultCtor::DefaultCtor(std::string class_name_, bool compiler_generated_)
: class_name(class_name_), compiler_generated(compiler_generated_)
{
}

What you have here is a normal _parameterized_ constructor. It is
*definitely* not a 'default' one, so there is a misnomer.

Also, as a side note, you shouldn't be passing 'std::string' by value,
there is no value in that (pun intended). You should pass it by a ref
to const:

SomeCtor(std::string const& class_name, ...

V
 
J

Jason Heyes

Victor Bazarov said:
What's a "struct-like initialisation"?


What you have here is a normal _parameterized_ constructor. It is
*definitely* not a 'default' one, so there is a misnomer.

Also, as a side note, you shouldn't be passing 'std::string' by value,
there is no value in that (pun intended). You should pass it by a ref
to const:

SomeCtor(std::string const& class_name, ...

V

I will call it a "default constructor that takes arguments" or a "default
parameterized constructor." How does that sound?

I don't pass std::string by const reference when I know it won't affect
performance.
 
D

Dan Cernat

Jason said:
I will call it a "default constructor that takes arguments" or a "default
parameterized constructor." How does that sound?

You provided it, therefore it is NOT a default constructor.

In your opinion, how a non-default parametrized constructor would look
like?

Thanks,
Dan
 
H

Howard

Jason Heyes said:
I will call it a "default constructor that takes arguments" or a "default
parameterized constructor." How does that sound?

A default constructor takes no arguments. Yours has arguments, so it's not
default. Why do you want to use the term "default" with it? It's simply a
(parameterized) constructor. Leave off the "default".

(By the way, who's going to hear/see you call it that? Is this for purposes
of discussion here, or for internal documentation purposes, or...?)

-Howard
 
V

Victor Bazarov

Howard said:
A default constructor takes no arguments.

Nit-pick: a default constructor is one that does not need arguments
specified to construct an object (IOW, it may have default argument
values). Both classes:

struct A {
A();
};

struct B {
B(int = 0);
};

have _default_ constructors.
Yours has arguments, so it's not
default. Why do you want to use the term "default" with it? It's simply a
(parameterized) constructor. Leave off the "default".

(By the way, who's going to hear/see you call it that? Is this for purposes
of discussion here, or for internal documentation purposes, or...?)

V
 
J

Jason Heyes

Howard said:
A default constructor takes no arguments. Yours has arguments, so it's
not default. Why do you want to use the term "default" with it? It's
simply a (parameterized) constructor. Leave off the "default".

(By the way, who's going to hear/see you call it that? Is this for
purposes of discussion here, or for internal documentation purposes,
or...?)

I want the phrase "default parameterized" to mean "all arguments." This does
not affect the phrase "default" on its own, which means "no arguments."

I need to name various kinds of constructors. I want to write classes to
represent them. The classes must be given names.
 
J

Jason Heyes

Dan Cernat said:
You provided it, therefore it is NOT a default constructor.

Aren't you talking about compiler-generated constructors? Default
constructors take no arguments and do not have to be generated automatically
by a compiler.
 
V

vish

BTW, Dont the default and non default constructors receive a pointer
("this") implicitly??
 
V

Victor Bazarov

vish said:
BTW, Dont the default and non default constructors receive a pointer
("this") implicitly??

Every non-static member function does. What does it have to do with
the problem at hand?
 
P

Peter Koch Larsen

Victor Bazarov said:
Every non-static member function does. What does it have to do with
the problem at hand?

Technically, I would say no. A constructor does not receive a this-pointer -
it "creates" one. But this is nitpicking, of course.

/Peter
 
S

Shezan Baig

Peter said:
Technically, I would say no. A constructor does not receive a this-pointer -
it "creates" one. But this is nitpicking, of course.

/Peter


Really? The 'this' pointer exists even before the constructor starts
executing (e.g, you can use it in the initializer list), so I would
have guessed that it is passed to the constructor.

-shez-
 
V

Victor Bazarov

Peter said:
Technically, I would say no. A constructor does not receive a this-pointer -
it "creates" one. But this is nitpicking, of course.

"Creates"? What does that mean? 'this' _pointer_ is valid before the
initialiser list is reached (although only as a pointer, the object has
not be constructed). Some other code allocates memory and therefore
"creates" the pointer, which is inserted into the expression any time
you use the 'this' keyword. That falls under "receive", not "create".

V
 
O

Old Wolf

Jason said:
I want the phrase "default parameterized" to mean "all arguments."
This does not affect the phrase "default" on its own, which means
"no arguments."

I need to name various kinds of constructors.

This is like calling a green apple "red variant".
IOW, "default" means "no arguments", as you say. (NB. not to be
confused with "no parameters"). That is the essence of the term
"default". It does not make much sense to use "default ....." for
something with arguments.
 
R

Richard Herring

Jason Heyes said:
I don't pass std::string by const reference when I know it won't affect
performance.
That would be "never", since you don't know how it's implemented.
 
P

Peter Koch Larsen

Victor Bazarov said:
"Creates"? What does that mean? 'this' _pointer_ is valid before the
initialiser list is reached (although only as a pointer, the object has
not be constructed). Some other code allocates memory and therefore
"creates" the pointer, which is inserted into the expression any time
you use the 'this' keyword. That falls under "receive", not "create".

V
I agree. What I meant to say was that while there is a this pointer, it does
not hold an object before the constructor completes. The this pointer
contains "raw" memory until that point.

/Peter
 
P

Peter Koch Larsen

Shezan Baig said:
Really? The 'this' pointer exists even before the constructor starts
executing (e.g, you can use it in the initializer list), so I would
have guessed that it is passed to the constructor.

-shez-
You are correct. What I meant was that "this" does not point to an object in
the constructor. It points to raw memory which gets created while the
constructor runs.

/Peter
 
R

Richard Herring

Jason Heyes said:
Have you considered how a std::string's size affects performance?

No. What does the standard say about it, and what aspect of that do you
think would make it better to pass by value than reference?
 

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,301
Messages
2,571,549
Members
48,295
Latest member
JayKillian
Top