Reputation: 131
If I have a header file in which I want to declare a class:
class Entity
{
int a;
int b;
void do_something();
};
Then this is just a normal declaration, right?
class Entity
{
int a = 0;
int b;
void do_something();
};
But now does this turn it into a definition? If it does, what exactly happens when I try to include that header file into two .cpp files? I'm basically going to have two Entity classes defined in two files but is this okay? If it is okay, then why is it okay? What exactly is the linker going to do?
Upvotes: 3
Views: 98
Reputation:
Then this is just a normal declaration right?
class Entity
{
int a = 0;
int b;
void do_something();
};
Nope. this is a definition of the Entity
class, which includes a declaration of the do_something()
method.
I'm basically gonna have two Entity classes defined in two files but is this okay? And if it is okay why is it okay?
This is ok because the One Definition Rule allows for classes to be redefined in multiple translation units as long as the definitions are the same.
You can think of it as all class definitions having an implicit inline
.
Upvotes: 1