#Returned values

1 messages · Page 1 of 1 (latest)

shut dagger
#

So in class methods I can return things like the class members. Does this mean that functions can return anything that they have access to? Ie. Global variables?

vale oysterBOT
#

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.

serene geode
#

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

twin surge
shut dagger
#

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

vague roost
shut dagger
#

?

vague roost
#

You can't use an identifier that hasn't been declared at the point of use 🤷‍♂️

shut dagger
#

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?

shut dagger
#

Just curious, if things are implicitly inline, is there a keyword to make it not?

tidal lark
#

In theory extern, but that clashes with the immediate definition.

hybrid turret
# shut dagger Just another question regarding classes. Typically I just have my classes in a h...

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

vague roost
#

Die, you must

hazy spindle
hybrid turret
# hazy spindle why doesn’t a class implementation in a header file break the ODR after it’s #in...

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

rough crest
#

i like guards more anyway

hazy spindle
hybrid turret
hazy spindle