ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
there will be no symbol named test2, so you can't call it even if you declare it. (yes, you can copy the code or something else, but test2 from test, will be accesible - will have the scope just in test.
And Java does pretty much the same. (of course, you need to specify classpath, but when you compile the linker must also know all the paths to the object files).
You do not restrict the scope at all in our solution.
What exactly do you mean? I don't know what you're talking about, but what I'm saying is that the way I wrote it, it's perfectly legal C. Are you saying that you can't go to yet another source file and use the same function in OP's solution? That's not scope restriction, that's symbol visibility, which can't be controlled using standard C except with static and inline. If you're worried about it showing up in a library's symbol table, just use __attribute__ ((visibility("internal"))) in the function definition.
ta0kira
And the thing with inner functions is that there will be no symbol named as the inner function. You wouldn't be able to use test2 not even in the same file - except for the function where it is defined. The attribute thing is doing probably more or less the same thing (it is a C extension to restrict the visibility).
@neioo: you almost answered yourself. The restricted access is a big plus if the project is big and many people have to mantain it. Also, inheritance will help reuse parts of the code. I would say also that the type system is a bit better and allows you to catch more errors at compile time. Then again, you can mess up completly C++, but a bad programmer manages to do that even in C.
Last edited by vladmihaisima; 08-04-2008 at 01:03 PM.
Reason: reply to neioo
I thought that's what the OP wanted... An inner function for scoping purposes.
No point in using invalid C when it isn't necessary. Honestly, is it really that important to protect your own code from yourself to that level? Don't tell yourself this, but you could also just copy and paste the same embedded function into another function, so by placing it in a different file, you'd have to open that file to copy and paste it.
The only real purpose I can see for this is to protect a library, which can easily be controlled by visibility or inline.
ta0kira
@neioo: you almost answered yourself. The restricted access is a big plus if the project is big and many people have to mantain it. Also, inheritance will help reuse parts of the code. I would say also that the type system is a bit better and allows you to catch more errors at compile time. Then again, you can mess up completly C++, but a bad programmer manages to do that even in C.
I'm sorry, it was hard to me to understand the existence of the Object oriented languages. I allways though that OO programming was simply to make programmers work harder: take care about classes, its visibility, its inheritance...
Now I see that the main of object oriented languages is to make easy to maintain a huge project with a lot of functionalities and with a lot of people working on it, although it has its cost in the performance. Because in huge projects, where hundreds of people are working together, is more important and abstraction of the problem, where a lot of little changes don't not affect the whole project than the performance.
Because I work in little projects, I have never taken this point of view. But I think that a well structured program written in C can also be maintained by a lot of people, if not, how can exists Linux kernel or GNU C library (for example)?
I'm sorry, it was hard to me to understand the existence of the Object oriented languages. I allways though that OO programming was simply to make programmers work harder: take care about classes, its visibility, its inheritance...
Wrong place for this discussion, but I have to disagree.
ta0kira
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.