Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

c++ learning...

65 views
Skip to first unread message

Pvr Pasupuleti

unread,
Mar 18, 2018, 10:14:25 AM3/18/18
to

Mr Flibble

unread,
Mar 18, 2018, 10:20:36 AM3/18/18
to
On 18/03/2018 14:14, Pvr Pasupuleti wrote:
> https://unacademy.com/lesson/types-of-functions-2/3EZZALNC

1) Don't use "using namespace std"
2) 'result' would have been a better variable name than 'add' in
function 'add' as it is confusing giving the variable the same name as
the function name.

/Flibble


Bonita Montero

unread,
Mar 19, 2018, 2:46:09 PM3/19/18
to
> 1) Don't use "using namespace std"

Nonsense.

Öö Tiib

unread,
Mar 19, 2018, 4:37:06 PM3/19/18
to
On Monday, 19 March 2018 20:46:09 UTC+2, Bonita Montero wrote:
> > 1) Don't use "using namespace std"
>
> Nonsense.

When you use exclusively Spanish names in your code then clashes
with names in std will be less likely but then you should say
something like "disparates" not "nonsense".

Vir Campestris

unread,
Mar 19, 2018, 7:08:00 PM3/19/18
to
On 19/03/2018 18:45, Bonita Montero wrote:
>> 1) Don't use "using namespace std"
>
> Nonsense.

He's right. Don't do it.

If you see std::vector you _know_ it's the normal one. If you just see
vector you don't. Especially in old code that pre-dates the STL.

Heck, I've been fixing some code today that has
typedef std::shared_ptr<someclass> buffer;
or such for half-a-dozen types - and that keeps me having to refer back
to check what they are.

Andy

Mr Flibble

unread,
Mar 20, 2018, 5:45:37 AM3/20/18
to
Vir Campestris <vir.cam...@invalid.invalid> wrote:
> On 19/03/2018 18:45, Bonita Montero wrote:
>>> 1) Don't use "using namespace std"
>>
>> Nonsense.
>
> He's right. Don't do it.
>
> If you see std::vector you _know_ it's the normal one. If you just see
> vector you don't. Especially in old code that pre-dates the STL.

Agree.

>
> Heck, I've been fixing some code today that has
> typedef std::shared_ptr<someclass> buffer;
> or such for half-a-dozen types - and that keeps me having to refer back
> to check what they are.

Disagree. Using typedef to give otherwise non-descript generic types
meaningful names is a good idea which I employ a lot. C++ has also
introduced the 'auto' keyword which hides the original type.

/Flibble


Juha Nieminen

unread,
Mar 21, 2018, 8:47:41 AM3/21/18
to
Mr Flibble <flibbleREM...@i42.co.uk> wrote:
> C++ has also
> introduced the 'auto' keyword which hides the original type.

Using 'auto' can be a double-edged sword.

When you explicitly specify the type, it catches mistakes more easily.
For example, if you write:

std::vector<Type> vec = someFunction();

you are explicitly expressing that you expecting that function to
return a vector of that type. If you make a mistake and the function
returns something else, the error message will be immediate and clear.
However,

auto vec = someFunction();

is less readable because it doesn't express what you are expecting
that return value type to be, and if you make a mistake, the error
will happen later in the code, and can sometimes be very obscure.
In a few instances the program might even compile, and then
malfunction at runtime.

That being said, in generic code 'auto' can be very useful.
Perhaps also in non-generic code where 'auto' is used as a shortcut
to substitute a very long type, and it's very clear from the context
what that type is supposed to be.

Mr Flibble

unread,
Mar 21, 2018, 1:52:57 PM3/21/18
to
I used to think like you regarding auto but then I changed my mind and
wrote this:

https://leighjohnston.wordpress.com/2014/10/13/new-c-auto-keyword-condsidered-awesome-not-harmful/

/Flibble

--
"Suppose it’s all true, and you walk up to the pearly gates, and are
confronted by God," Bryne asked on his show The Meaning of Life. "What
will Stephen Fry say to him, her, or it?"
"I’d say, bone cancer in children? What’s that about?" Fry replied.
"How dare you? How dare you create a world to which there is such misery
that is not our fault. It’s not right, it’s utterly, utterly evil."
"Why should I respect a capricious, mean-minded, stupid God who creates
a world that is so full of injustice and pain. That’s what I would say."
0 new messages