#Returned values
1 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
Why would you even return a global variable from a member function?
Unless you mean non member functions
Even then it’s a global variable, so I don’t think you would ever need to return it
Does this mean that functions can return anything that they have access to?
Yep
No idea, just an example I thought of while going through reference returns on learn cpp
Most of the time I just return things that I created in the function but today I learned that you can actually return anything that the function has access to lol
A function can return any value that is convertible to it's return type
As long as it has access to it aswell
?
Well of course
You can't use an identifier that hasn't been declared at the point of use 🤷♂️
True lol
Just another question regarding classes. Typically I just have my classes in a header with the definition but then I put my method implementations into another source file. Technically I could just put my implementations into the header aswell right? It wouldn’t cause any errors unless different translation units see different definitions?
You could, but you shouldn't
Just curious, if things are implicitly inline, is there a keyword to make it not?
In theory extern, but that clashes with the immediate definition.
#includeing is just copy & paste, so if you have a all-in-header approach, then on every change of the implementation, all files that are including that header would change (because they copy & paste a different header text), which would need them to be recompiled.
If you have a split header and source file approach, and you decide to make a change to just the implementation (e.g. you optimize the internal process), then only that source file needs to be recompiled, but since you never changed the header, all other files including that header aren't changed and don't need to be recompiled.
For simple projects it's almost negligible, but especially in C++ compile times can quickly skyrocket.
Professional sniper you are, sir
Die, you must
why doesn’t a class implementation in a header file break the ODR after it’s #included multiple times?
guards
after it’s #included multiple times?
That's what you have include guards for, i.e. either the old fashioned:
#ifndef HEADER_SPECIFIC_TOKEN
#define HEADER_SPECIFIC_TOKEN
// code
#endif
```or the - technically non standard - simpler way:
```c
#pragma once
// code
C++20 then introduced modules, but tbf I haven't really looked into them, yet. Even though they have technically been introduced in C++20, many compilers needed quiet a long time to (fully) implement them, and I'm honestly not sure if all major compilers have already fully implemented them by now. In case you want to give it a read: https://en.cppreference.com/w/cpp/language/modules.html
i like guards more anyway
Oooh okay the include guards make sense, now I feel silly xD thank you. Also excited to give ur article on scanf a read right now. My friend has a C lang college class that Ive been trying to help him with since I know some cpp, and I’ve been kinda annoyed with scanf after being spoiled by cin, especially the fact that we have to add “get char” after every scanf statement xD
Also excited to give ur article on scanf a read right now
It's not mine, but it helped me massively when I started learning C
Well thanks so much for spreading awareness of it. Ive been meaning to read up on scanf for a while now and you saved me a search xP