Conclusion

It's not a big deal and you may find it not nicely programmed in an OO way, but it is handy to know when you want to do something like this.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

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.

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

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

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?

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

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.