Two Questions!

Pages: 12
I am very confused right now.

My first question is as to why Inline Functions must be included in a header file and why cpp files cannot be included, and must be compiled?

My second question is when I add a class I separate it into two Pieces. The header file and the cpp file. I know why you must separate them, for organizational purposes and to protect the implementation of your class. But I do not include the cpp file in the header file, I only included the header file in the cpp file. So how does the class in the header access the definitions and implementation of the member functions?

Also when I use my class I only include the header file and the header file has no access to the cpp file so how does everything work?

I would appreciate a explanation to my questions, please! Thanks guys!

I am just a noob who started objective oriented programming.
inline. "There is no function here."
int c = a + b;
The above is same as this:
1
2
3
inline int foo( int x, int y ) { return x+y; }
..
int c = foo( a, b );

The compiler could not replace foo( a, b ) with the body of foo, if it could not see the body of foo.


Compiling a program has two stages. First, compiler generates an object file for each compilation unit. Second, a linker forms the executable binary from the object files and necessary libraries.

Lets look at a simple program:
1
2
3
4
#include <iostream>
void main() {
  std::cout << "Hello Dolly!\n";
}

It does refer to a global object std::cout and calls function operator<<. Both are implemented in some other compilation unit (within a library). The header file "iostream" declares those two. Those declarations are needed by the compiler to ensure that the linking will have in the main's object file calls that match symbols within library.

A header just says: "By the way, there exists function int foo( int, int) somewhere". It is a declaration. It does not use anything. A class does not "exist" in header; it is just a concept.
But then how does it get the definitions and implementation for the class, if only the header is included? Because don't you need to included it with the include preprocessor?

I am very confused lol.

I kinda understand my first question as to what inline means. No function just replace the body code with the function call. Which makes sense. Is it because the compiler can only see one cpp file at a time?
Last edited on
Sorry if I ask any stupid questions, I am very big noob.
1
2
3
4
void foo()
{
  Bar x;
}

What does the compiler need to know about type Bar in this code?
Last edited on
The declaration of bar is needed and the definition.
But how does it see the cpp file from the header?
Like your only including the header file so how does it know which cpp file is contains the definitions?

Or wait is it the linker that does this when everything is conbined?
And for inline its just that since it can only compile once cpp at a time the header needs to be included in order to get the declaration of the class so the compiler knows that the function exists?
Last edited on
Is that correct?
The real use of the inline keyword is just to specify linkage type so that there are no violations of the ODR when the function definition is included in multiple translation units, (and as noted it is rather hard for the compiler to inline a function that it can't see, but the compiler will inline the functions it sees fit to without much regard for what you've marked as inline.)

.cpp files are compiled into object files. Given that, the following may prove interesting.

http://blogs.msdn.com/b/oldnewthing/archive/2013/01/07/10382714.aspx
@cire: That is a really nice article.

Anmol444 wrote:
The declaration of bar is needed and the definition.

That does not quite answer my question.

x is a local variable. Local variables are stored in stack memory. The compiler must reserve correct amount of memory from stack for the x. For that, the compiler has to know how many bytes does one instance of Bar require. For that, the compiler needs to know the amount of memory used by the member variables of Bar, and possible alignment padding.

The compiler has to include two needed symbols as well; the signatures of ctor and dtor of Bar (unless Bar is a typedef for basic type, like int). No code for the methods is required. It is assumed that the linker will resolve that later.


Another quiz:
1
2
3
class Foo {
  void magic( Bar & );
};

So, what does the compiler need to know about type Bar in this code?
@cire
What do you mean ODR, multiplte translation units? Can you define some of the vocabulary you used, I dont understand it fully.

Also thanks for that article very nice :D

@keskiverto
what do you mean ctor and dtor?

The compiler needs to know how much space bar will take up. I dont understand the symbols part though.

Exactly what is a symbol?

Thanks guys!

This is the reason I love this forum, so helpful and nice xD
Can you define some of the vocabulary you used, I dont understand it fully.

http://stackoverflow.com/questions/1106149/what-is-a-translation-unit-in-c
http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=386


Exactly what is a symbol?

In the context of the linker article, you can think of a symbol as the name of a variable or function.
Oh ok thanks :D

Also what do you mean here:

"specify linkage type so that there are no violations of the ODR"
Oh so it allows for more then one definition of the function?
So basically you mean that if its included in multiple source files it doesnt violate ODR?

Cant you just use pragma_once for that?
Also what do you mean here:
"specify linkage type so that there are no violations of the ODR"

Explained in the second link above.

Cant you just use pragma_once for that?
pragma_once and inclusion guards work to prevent multiple inclusions of the same header file into a single translation unit, in accordance with the ODR. It does not address what happens when definitions occur (or prevent that occurrence) in multiple translation units.
Oh ok thanks :D
Are you sure that its only for one translation unit? Cause wouldnt you get linker errors if it is also included in another file cause it would violate ODR?

Or is there something I am missing lol.
Pages: 12