Hi,
I have some questions about structure,wanted to know that when we make a struct using strings, do we have to make it outside of main() or inside it?
I hope i am clear
Hi,
I have some questions about structure,wanted to know that when we make a struct using strings, do we have to make it outside of main() or inside it?
I hope i am clear
Normally when you define a structure you do it outside of any functions. However if the structure is only going to be used inside that particular function you can define it inside the structure.
Jim
I would only consider following that if programming in .NET. Otherwise just follow typical convention: Use a struct when it is a POD type and most things will be public.
I've seen several comments lately about getters and setters, without any hint of a thought about the fact that such things indicate a lack of encapsulation, and so tend to be solving the wrong problem anyway. Also YAGNI ... so don't over-engineer it.
student111: You can declare a struct wherever you like. Just be aware that if you declare it inside a function then you cant go any use it outside of that function.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Over-engineering is bad. But is it truly such a waste of time to set up proper getters and setters? It takes not much more time, and thus, it can seen as a precaution.
When you start accessing members of the struct at several places, it is typically time to set up getters and setters, but at that time, you've just screwed yourself over--now you must make (possibly) big changes to your code to make those getters and setters.
As a precaution, whenever making changes to a struct, it may be worth changing about encapsulating such a thing to a function. This may be taking it to the extreme, but it will protect yourself from code changes later.