Galaxy
Galaxy

Reputation: 2481

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

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;
 }

Upvotes: 0

Views: 553

Answers (2)

John Kugelman
John Kugelman

Reputation: 361595

It's rare that a local class will be useful. Take your example: what can you do with the Dog object? You can't pass it to any other function. No other code can do anything with it. What's the point of encapsulating its data in an object that has such limited scope?

Would all the machinery of a class benefit something with such limited usage? Constructors, destructors, member functions, operator overloading, etc. Not really useful. Would you have private variables? Probably not. What's the point; there's nobody to hide them from.

Sometimes it's useful to create a local struct, particularly a plain old data (POD) structure. A PODs with only public variables and no member functions could be useful for quickly grouping a couple of related values. Even still, such usage is rare.

Upvotes: 1

Jerry Coffin
Jerry Coffin

Reputation: 490108

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

Upvotes: 1

Related Questions