default params to clsas member functions

P

puzzlecracker

not sure of the stardard: which one is right, if both which one is
preferable?

class FOO{
void foo(const string &, const string &username="true");
.....

}

void FOO::foo(const string & cycle, const string &username="true")
{


........


};



OR

class FOO{
void foo(const string &, const string &);
.....

};

void FOO::foo(const string & cycle, const string &username="true")
{


........


}



thx
 
V

Victor Bazarov

puzzlecracker said:
not sure of the stardard: which one is right, if both which one is
preferable?

class FOO{
void foo(const string &, const string &username="true");
....

}

void FOO::foo(const string & cycle, const string &username="true")

Redefinition of a default argument value is prohibited.
{


.......


};



OR

class FOO{
void foo(const string &, const string &);
....

};

void FOO::foo(const string & cycle, const string &username="true")
{


.......


}

Preferable to have the default argument value in the declaration, i.e.
in your case, in the class definition. The reason is that the default
argument value is used by the users of the class. The implementation
of that particular member function usually can't care less if the value
passed to it was because it was omitted in the code or because it was
specified to the exact same value as the default. And the user of the
class normally can't see the implementation. So, how is the default
value going to be available to him?

V
 
R

Ron Natalie

puzzlecracker said:
not sure of the stardard: which one is right, if both which one is
preferable?

You have to put the defaulted argument at the point of declaration that
the caller sees. This almost certainly means that it's defined
in the class definition.

You can't specify it again in another declaration or definition.

Niehter of your examples are right. This is the right way

class FOO {
void foo(const string&, const string& username="true"); // HERE
};

// not here
void FOO::foo(const string& cycle, const string& username) {
 
P

puzzlecracker

You have to put the defaulted argument at the point of declaration that
the caller sees. This almost certainly means that it's defined
in the class definition.
You can't specify it again in another declaration or definition.


you CANNOT or SHOULDNOT?
 
P

Peter Koch Larsen

puzzlecracker said:
not sure of the stardard: which one is right, if both which one is
preferable?

class FOO{
void foo(const string &, const string &username="true");
....

}
[snip]

Others have explained the correct way, but best is not to use default
parameters if you can. Thus prefer

class FOO{
void foo(const string &, const string &username="true");
void foo(const string & s) { foo(s"true"); }
.....

}
 
V

Victor Bazarov

Peter said:
not sure of the stardard: which one is right, if both which one is
preferable?

class FOO{
void foo(const string &, const string &username="true");
....

}

[snip]

Others have explained the correct way, but best is not to use default
parameters if you can. Thus prefer

class FOO{
void foo(const string &, const string &username="true");
void foo(const string & s) { foo(s"true"); }

What's up with your posts today? The two lines above are just wrong.
They should be

void foo(const string &, const string & username); // no default
void foo(const string & s) { foo(s, "true"); } // calling the buddy

V
 
R

Rolf Magnus

Peter said:
Others have explained the correct way, but best is not to use default
parameters if you can. Thus prefer

class FOO{
void foo(const string &, const string &username="true");
void foo(const string & s) { foo(s"true"); }
....

}

Why? It's more to type, more to maintain, more to read in the interface
documentation, and to me, it seems to offer no benefit.
 

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

Forum statistics

Threads
474,298
Messages
2,571,542
Members
48,284
Latest member
RedaBruno6

Latest Threads

Top