How can you access the function from another translation unit without including any files?
I was reading about static functions and it was said that if the function is static then you can use it only in the same file. After testing it, I realized it is not true because if you include the file with a static function you can still use that function in another file. Then I read a clarification that you can actually use static function only in the same translation unit. Ok, that makes sense because it means .cpp + includes, but even if the function is not static, you will still not be able to use it unless you include the file. So how is it even possible to access a function from another translation unit in the first place without including anything, what is the point of static functions?
Main.cpp
#include "Staticf.h"
void main()
{
visible();
}
Staticf.h
#pragma once
#include <iostream>
using namespace std;
static void visible()
{
cout << "Static function is visiblen";
}
This compiles alright. If I make that function non-static and remove #include "Staticf.h" I will not be able to use it in Main anyways. So why are static functions needed if you can't access non-static as well?
c++ static-functions
add a comment |
I was reading about static functions and it was said that if the function is static then you can use it only in the same file. After testing it, I realized it is not true because if you include the file with a static function you can still use that function in another file. Then I read a clarification that you can actually use static function only in the same translation unit. Ok, that makes sense because it means .cpp + includes, but even if the function is not static, you will still not be able to use it unless you include the file. So how is it even possible to access a function from another translation unit in the first place without including anything, what is the point of static functions?
Main.cpp
#include "Staticf.h"
void main()
{
visible();
}
Staticf.h
#pragma once
#include <iostream>
using namespace std;
static void visible()
{
cout << "Static function is visiblen";
}
This compiles alright. If I make that function non-static and remove #include "Staticf.h" I will not be able to use it in Main anyways. So why are static functions needed if you can't access non-static as well?
c++ static-functions
1
If you include something, the included file and the file that includes it are the same file.
– Neil Butterworth
Nov 20 '18 at 18:35
Yes, I know that, that was not the question
– Vegeta
Nov 20 '18 at 18:41
What is the point of calling someone that you don't know their address, name and phone number?
– Ostrich Groomer
Nov 20 '18 at 18:42
#include <iostream> using namespace std
; is the worst path you can take on in a header file.
– πάντα ῥεῖ
Nov 20 '18 at 18:42
FWIW, this is a hold over from C. In C++ if you want to hide a function you can use a namespace.
– NathanOliver
Nov 20 '18 at 18:43
add a comment |
I was reading about static functions and it was said that if the function is static then you can use it only in the same file. After testing it, I realized it is not true because if you include the file with a static function you can still use that function in another file. Then I read a clarification that you can actually use static function only in the same translation unit. Ok, that makes sense because it means .cpp + includes, but even if the function is not static, you will still not be able to use it unless you include the file. So how is it even possible to access a function from another translation unit in the first place without including anything, what is the point of static functions?
Main.cpp
#include "Staticf.h"
void main()
{
visible();
}
Staticf.h
#pragma once
#include <iostream>
using namespace std;
static void visible()
{
cout << "Static function is visiblen";
}
This compiles alright. If I make that function non-static and remove #include "Staticf.h" I will not be able to use it in Main anyways. So why are static functions needed if you can't access non-static as well?
c++ static-functions
I was reading about static functions and it was said that if the function is static then you can use it only in the same file. After testing it, I realized it is not true because if you include the file with a static function you can still use that function in another file. Then I read a clarification that you can actually use static function only in the same translation unit. Ok, that makes sense because it means .cpp + includes, but even if the function is not static, you will still not be able to use it unless you include the file. So how is it even possible to access a function from another translation unit in the first place without including anything, what is the point of static functions?
Main.cpp
#include "Staticf.h"
void main()
{
visible();
}
Staticf.h
#pragma once
#include <iostream>
using namespace std;
static void visible()
{
cout << "Static function is visiblen";
}
This compiles alright. If I make that function non-static and remove #include "Staticf.h" I will not be able to use it in Main anyways. So why are static functions needed if you can't access non-static as well?
c++ static-functions
c++ static-functions
edited Nov 20 '18 at 18:38
Vegeta
asked Nov 20 '18 at 18:33
VegetaVegeta
4616
4616
1
If you include something, the included file and the file that includes it are the same file.
– Neil Butterworth
Nov 20 '18 at 18:35
Yes, I know that, that was not the question
– Vegeta
Nov 20 '18 at 18:41
What is the point of calling someone that you don't know their address, name and phone number?
– Ostrich Groomer
Nov 20 '18 at 18:42
#include <iostream> using namespace std
; is the worst path you can take on in a header file.
– πάντα ῥεῖ
Nov 20 '18 at 18:42
FWIW, this is a hold over from C. In C++ if you want to hide a function you can use a namespace.
– NathanOliver
Nov 20 '18 at 18:43
add a comment |
1
If you include something, the included file and the file that includes it are the same file.
– Neil Butterworth
Nov 20 '18 at 18:35
Yes, I know that, that was not the question
– Vegeta
Nov 20 '18 at 18:41
What is the point of calling someone that you don't know their address, name and phone number?
– Ostrich Groomer
Nov 20 '18 at 18:42
#include <iostream> using namespace std
; is the worst path you can take on in a header file.
– πάντα ῥεῖ
Nov 20 '18 at 18:42
FWIW, this is a hold over from C. In C++ if you want to hide a function you can use a namespace.
– NathanOliver
Nov 20 '18 at 18:43
1
1
If you include something, the included file and the file that includes it are the same file.
– Neil Butterworth
Nov 20 '18 at 18:35
If you include something, the included file and the file that includes it are the same file.
– Neil Butterworth
Nov 20 '18 at 18:35
Yes, I know that, that was not the question
– Vegeta
Nov 20 '18 at 18:41
Yes, I know that, that was not the question
– Vegeta
Nov 20 '18 at 18:41
What is the point of calling someone that you don't know their address, name and phone number?
– Ostrich Groomer
Nov 20 '18 at 18:42
What is the point of calling someone that you don't know their address, name and phone number?
– Ostrich Groomer
Nov 20 '18 at 18:42
#include <iostream> using namespace std
; is the worst path you can take on in a header file.– πάντα ῥεῖ
Nov 20 '18 at 18:42
#include <iostream> using namespace std
; is the worst path you can take on in a header file.– πάντα ῥεῖ
Nov 20 '18 at 18:42
FWIW, this is a hold over from C. In C++ if you want to hide a function you can use a namespace.
– NathanOliver
Nov 20 '18 at 18:43
FWIW, this is a hold over from C. In C++ if you want to hide a function you can use a namespace.
– NathanOliver
Nov 20 '18 at 18:43
add a comment |
2 Answers
2
active
oldest
votes
You can access non-static functions from other translation units. You just need a forward declaration:
somemodule.cpp
void func()
{
// details
}
main.cpp
void func();
int main()
{
func();
}
It is, of course, a best practice to put the forward declaration in a header file.
So what use are static functions?
In modern C++, they actually aren't much use. static
is a feature C++ inherited from C. In C, declaring a helper function static
is actually pretty useful because C doesn't have namespaces. You can use static
to eliminate the chance of name collisions between your private helper functions and the private helper functions in other translation units. In C++, it's generally preferred to wrap your private helpers in a class or anonymous namespace.
You could mention OOP for your C++ section =)
– Chris Mc
Nov 20 '18 at 18:48
Thanks, I tested it and this answered my question. I didn't think about forward declaration
– Vegeta
Nov 20 '18 at 18:50
add a comment |
If the function is static (as if free static function), you cannot access it from another translation unit.
Remove the static
, and if you can't include a header (you probably can though... change the former static function's header), add a prototype in the file.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53399370%2fhow-can-you-access-the-function-from-another-translation-unit-without-including%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can access non-static functions from other translation units. You just need a forward declaration:
somemodule.cpp
void func()
{
// details
}
main.cpp
void func();
int main()
{
func();
}
It is, of course, a best practice to put the forward declaration in a header file.
So what use are static functions?
In modern C++, they actually aren't much use. static
is a feature C++ inherited from C. In C, declaring a helper function static
is actually pretty useful because C doesn't have namespaces. You can use static
to eliminate the chance of name collisions between your private helper functions and the private helper functions in other translation units. In C++, it's generally preferred to wrap your private helpers in a class or anonymous namespace.
You could mention OOP for your C++ section =)
– Chris Mc
Nov 20 '18 at 18:48
Thanks, I tested it and this answered my question. I didn't think about forward declaration
– Vegeta
Nov 20 '18 at 18:50
add a comment |
You can access non-static functions from other translation units. You just need a forward declaration:
somemodule.cpp
void func()
{
// details
}
main.cpp
void func();
int main()
{
func();
}
It is, of course, a best practice to put the forward declaration in a header file.
So what use are static functions?
In modern C++, they actually aren't much use. static
is a feature C++ inherited from C. In C, declaring a helper function static
is actually pretty useful because C doesn't have namespaces. You can use static
to eliminate the chance of name collisions between your private helper functions and the private helper functions in other translation units. In C++, it's generally preferred to wrap your private helpers in a class or anonymous namespace.
You could mention OOP for your C++ section =)
– Chris Mc
Nov 20 '18 at 18:48
Thanks, I tested it and this answered my question. I didn't think about forward declaration
– Vegeta
Nov 20 '18 at 18:50
add a comment |
You can access non-static functions from other translation units. You just need a forward declaration:
somemodule.cpp
void func()
{
// details
}
main.cpp
void func();
int main()
{
func();
}
It is, of course, a best practice to put the forward declaration in a header file.
So what use are static functions?
In modern C++, they actually aren't much use. static
is a feature C++ inherited from C. In C, declaring a helper function static
is actually pretty useful because C doesn't have namespaces. You can use static
to eliminate the chance of name collisions between your private helper functions and the private helper functions in other translation units. In C++, it's generally preferred to wrap your private helpers in a class or anonymous namespace.
You can access non-static functions from other translation units. You just need a forward declaration:
somemodule.cpp
void func()
{
// details
}
main.cpp
void func();
int main()
{
func();
}
It is, of course, a best practice to put the forward declaration in a header file.
So what use are static functions?
In modern C++, they actually aren't much use. static
is a feature C++ inherited from C. In C, declaring a helper function static
is actually pretty useful because C doesn't have namespaces. You can use static
to eliminate the chance of name collisions between your private helper functions and the private helper functions in other translation units. In C++, it's generally preferred to wrap your private helpers in a class or anonymous namespace.
answered Nov 20 '18 at 18:42


Peter RudermanPeter Ruderman
10.2k2352
10.2k2352
You could mention OOP for your C++ section =)
– Chris Mc
Nov 20 '18 at 18:48
Thanks, I tested it and this answered my question. I didn't think about forward declaration
– Vegeta
Nov 20 '18 at 18:50
add a comment |
You could mention OOP for your C++ section =)
– Chris Mc
Nov 20 '18 at 18:48
Thanks, I tested it and this answered my question. I didn't think about forward declaration
– Vegeta
Nov 20 '18 at 18:50
You could mention OOP for your C++ section =)
– Chris Mc
Nov 20 '18 at 18:48
You could mention OOP for your C++ section =)
– Chris Mc
Nov 20 '18 at 18:48
Thanks, I tested it and this answered my question. I didn't think about forward declaration
– Vegeta
Nov 20 '18 at 18:50
Thanks, I tested it and this answered my question. I didn't think about forward declaration
– Vegeta
Nov 20 '18 at 18:50
add a comment |
If the function is static (as if free static function), you cannot access it from another translation unit.
Remove the static
, and if you can't include a header (you probably can though... change the former static function's header), add a prototype in the file.
add a comment |
If the function is static (as if free static function), you cannot access it from another translation unit.
Remove the static
, and if you can't include a header (you probably can though... change the former static function's header), add a prototype in the file.
add a comment |
If the function is static (as if free static function), you cannot access it from another translation unit.
Remove the static
, and if you can't include a header (you probably can though... change the former static function's header), add a prototype in the file.
If the function is static (as if free static function), you cannot access it from another translation unit.
Remove the static
, and if you can't include a header (you probably can though... change the former static function's header), add a prototype in the file.
answered Nov 20 '18 at 18:35
Matthieu BrucherMatthieu Brucher
14.9k32140
14.9k32140
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53399370%2fhow-can-you-access-the-function-from-another-translation-unit-without-including%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
If you include something, the included file and the file that includes it are the same file.
– Neil Butterworth
Nov 20 '18 at 18:35
Yes, I know that, that was not the question
– Vegeta
Nov 20 '18 at 18:41
What is the point of calling someone that you don't know their address, name and phone number?
– Ostrich Groomer
Nov 20 '18 at 18:42
#include <iostream> using namespace std
; is the worst path you can take on in a header file.– πάντα ῥεῖ
Nov 20 '18 at 18:42
FWIW, this is a hold over from C. In C++ if you want to hide a function you can use a namespace.
– NathanOliver
Nov 20 '18 at 18:43