 |
|
 |
I guess you already knew it, but just in case you were not aware of it: the proposed solution is wasting performance because of unneccessary copies.
A better solution would pass the default value as a constant reference:
static const MyStruct gs_myDefault = { 100, 100 };
foo( const MyStruct& st = gs_myDefault )
{
cout << st.a << " " << st.b << endl;
}
|
|
|
|
 |
|
 |
#include "stdafx.h" #include <iostream.h> struct MyStruct { MyStruct(int x = 100, int y = 200) { a = x; b = y; } int a; int b; }; void foo(MyStruct st) { cout << st.a << " " << st.b << endl; } void main() { MyStruct st; foo(st); foo(MyStruct(300,400)); }
|
|
|
|
 |
|
 |
With the default parameter you can do
foo();
and the parameters passed to foo will have the default.
|
|
|
|
 |
|
 |
void foo(int a, MyStruct st=MyStruct()) should work.
John
|
|
|
|
 |
|
 |
John M. Drescher wrote:
void foo(int a, MyStruct st=MyStruct()) should work.
This still does not accomplish what the original article set out to achieve. The original article had the function determining the nature of the initialisation of its default argument. You are having the structure determine it.
Suppose you had three functions, foo, goo and moo, and wanted a default argument to each consisting of a MyStruct object initialised with {100,100}, {200,200} and {300,300} respectively.
John Carson
|
|
|
|
 |
|
 |
Yes this is the C++ solution. I have been using C++ so long I did not remember that you could do the solution that the author proposed.
John
|
|
|
|
 |
|
 |
Adding a construct to an otherwise POD struct may have disadvantages, depending on what you need to do with it.
You can't use it in a union.
You can't initialize an array of them with = { ... }
The code may be less transparent to the optimizer.
(I have a feeling there's something else, but I don't remember what...)
That's not really a big deal though, since you can easily:
struct MyStruct2 : public MyStruct
{
MyStruct2(int x=100, int y=200) { a=x; b=y; }
};
Then you can use MyStruct2(300,400) for a function that takes MyStruct.
Similarly, you could define a free function to set the values:
MyStruct MakeMyStruct(int x,int y)
{
MyStruct m = {x, y};
return m;
}
|
|
|
|
 |
|
 |
Correct me if I'm wrong, but
static const stDefValue = { 100, 100 };
is not C++. Therefore it should not be in the C++ section.
|
|
|
|
 |
|
 |
Sarcasm apart, to my knowledge using default arguments is not even C, so what is this?
just kidding...
Paolo
------
Why spend 2 minutes doing it by hand when you can spend all night plus most of the following day writing a system to do it for you? - (Chris Maunder)
|
|
|
|
 |
|
|
 |
|
 |
Jörgen Sigvardsson wrote:
You don't have to be sarcastic about it
Yes I do.
There is no point to these articles that show people the wrong way to do things. These people work out how to do some really simple thing and then decide to share it with the world. Feel free to defend this article if you can
Whatever happened to RTFM?
|
|
|
|
 |
|
 |
Well, seeing that you have contributed the whopping sum of 0 articles, I don't see how you can be complaining. :P
The thing is that the author wasn't rude to you in any way. He made a typo, and his article was perhaps not the most advanced article on CP.
I've seen posts by you before, and the impression I have of you is somewhere between arrogant and a-hole. It's not like your defending your own views or honor or anything, then why be so darn hostile?
--
20 eyes in my head, they're all the same![^]
|
|
|
|
 |
|
 |
Jörgen Sigvardsson wrote:
you have contributed the whopping sum of 0 articles
That is a silly thing to say, but I can see you are probably joking. If you think only allowing article authours to comments is a good idea, you should start up your own site and see how well it does... Frankly, it would become a back-slapping contest (obviously).
Jörgen Sigvardsson wrote:
somewhere between arrogant and a-hole
I aim to please Thanks for the insult, but this article is a 0/5, and you know it. You have no reason to defend it
Jörgen Sigvardsson wrote:
why be so darn hostile
What I said was legitimate. The C++ way of doing default parameters is well known and was helpfully provided by someone else's comment (above). I believe there should only have proper C++ in the C++ section. I think you might have misunderstood - I wasn't actually trying to be hostile (honest). Perhaps I should use more smilies?
|
|
|
|
 |
|
 |
dog_spawn wrote:
The C++ way of doing default parameters is well known and was helpfully provided by someone else's comment (above).
Incorrectly as it turned out. See my comment above.
dog_spawn wrote:
I believe there should only have proper C++ in the C++ section.
If you are referring to the typo which produced
static const stDefValue = { 100, 100 };
when it should have been
static const MyStruct stDefValue = { 100, 100 };
then you are trivially correct.
If you are claiming that, after correcting the typo, the code is not legal C++, then you need to better acquaint yourself with the language.
If you are arguing that such constructions are legal but poor style, then you are entitled to your opinion but suggesting that they be banned from the C++ section is ridiculous. If Stroustrup includes this method of struct initialisation in his C++ text, who are you to say it should never be used?
John Carson
|
|
|
|
 |
|
 |
John Carson wrote:
Incorrectly as it turned out. See my comment above
Obviously I was refering to the original post including the refinements made.
John Carson wrote:
If you are claiming that, after correcting the typo, the code is not legal C++,
Nope, I never said that
John Carson wrote:
but suggesting that they be banned from the C++ section is ridiculous
I didn't say that either
My comments obviously didn't make sense without a smily face...
If you want to know my actual opinion: I am against articles that cover super-trivial topics. They seem to be done for the sake of it rather than to actually help anyone. Now I am not saying the authour was trying to waste anyone's time, but unfortunately , that was the way it turned out.
I think people should think twice about posting an article and then again, just to make sure. I will do the same with my comments from now on
|
|
|
|
 |
|
 |
dog_spawn wrote:
Obviously I was refering to the original post including the refinements made.
Even with the refinements, the suggested approach does not accomplish what the original article does --- i.e., make the initialisation function-specific.
dog_spawn wrote:
If you want to know my actual opinion: I am against articles that cover super-trivial topics.
I am against poor quality articles but not necessarily against short pieces that give tips on relatively simple matters. I have seen the subject of this article raised on newsgroups more than once, and I expect some people benefited from reading the proposed solutions. The solutions are not hard to figure out (I actually tried to solve the problem after reading the author's introduction and came up with the same two solutions as the author), but I cannot recall ever having seen them in a textbook.
John Carson
|
|
|
|
 |
|
 |
I found this article to be great...short and simple. I've never needed to do anything like this, but if I do in the future, I'll already know how!
When I do come across articles that I find "super-trivial" topics, I just don't read them, and keep on living.
Just my 3 cents.
|
|
|
|
 |
|
 |
It's not "wrong", just extraneous. const defaults to internal linkage in C++ (not in C) and to add "static" extraneously tells the compiler not to allow external linkage.
Just as
void function1()
{
}
Is not invalid C++ because functions default to returning void.
-- Peter
|
|
|
|
 |