how Should a class definition be put in the global or local scope?Ask Questions

 Posted on 08/18/2018

fellow programmers. I've seen it a common practice to put the class definition inside the global scope in a source file, above the main() function I'm not worrying about the details of the class definition for now.

class Dog { ... };

int main() {
    Dog dog1;
    ...
    return 0;
}

Why should we do this? Is it a rule or just a common convention? Can we also put the class definition inside the main() function? What are the advantages/disadvantages for doing this?

Consider the following two lines of code:

class Dog { ... } dog1;
int dog1;

To me it seems that class Dog { ... } is just a data type just like int is.

If I am going to use objects of some class inside the main() function only, why not put the class definition inside the main() function also? Is this bad programming practice? Or is this something that only C programmers do with structs? What is the reason to put the class definition in the global scope?

int main() {
    class Dog { ... };

    Dog d1;
    ...
    return 0;
 }
Share On: facebook gplus twitter
profile
Asked by on 08/18/2018 Score: 89 points
Add Comment:

Comments

1 Answers

0 Corrected Answers
Aproved Answers
0
Profile
Answered by hardik chaudhary on 9/6/2018 8:02:10 AM Score: 553 points

If you put the definition of Dog inside main, that definition is scoped just like other names.

That means a function outside main doesn't see that definition of Dog, so you can't define (for example) a function that takes a Dog as a parameter. So, this works:

class Dog {};

void foo(Dog const &d); // no problem

int main() { 
    Dog d;
    foo(d);
}

...but this doesn't:

void foo(Dog const &d);

int main() { 
    class Dog {} d;
    foo(d);
}

... and adding a forward declaration of Dog doesn't really work either.

Most types should be visible to essentially all the code, so they're typically defined outside any function.

A few types are intended only to be used by one other particular type, in which case they're typically defined inside that type:

class linked_list {
    class node {};
};

Now there's a type named linked_list::node, but you need the linked_list qualifier to find node (and, of course, node has to be public for anything outside linked_list to use it, whereas here it's shown as private).

Comments

Add Comment:

Post Your Answers

Existing Members

Sign in to your account
Email Address
Password
New Member?
Sign up and complete profile
Full Name
Email Address
I have read and agree to the Terms of Service and Privacy Policy
Please subscribe me to the StoodQ newsletters
Guideline to answer a question:

Useful tips to submit your answer
Please read below guidelines before you submit your answer for question.

  • Read and understand question for which you are submitting your answer.
  • Try to avoid grammatical and spell mistake while answering.
  • Do not post any irrelevant information in your answer.
  • Explain your answer with example or any reference link to help who posted question.
  • If you find irrelevant question, please report it to support. Click here to contact support.
  • You agree to the privacy policy and terms of use to submit any contents.

Note: StoodQ is online developers community which helps developer for their difficulty, lets help them with your value contribution.