How can I define and declare a class and its functions at the same time? Is this posssible?
declaring and defining at the same time
Collapse
X
-
Tags: None
-
You don't define a class. You declare it.
The definition is the creation of an object of that class.
The class methods ar typically declared by having the function prototypes inside the class declaraion. You can put the function definition there (the code) but that makes the methods inline and that may not be what you want. -
Do you mean like this:?Originally posted by curious2007How can I define and declare a class and its functions at the same time? Is this posssible?
[code=cpp]
class Test
{
public:
void testrun() { cout << "BLAH BLAH" << endl; };
}
[/code]
The functions that are defined in the class will be inline.Comment
-
OK, thanks but what happens if a function is inline? Does this put restrictions on what I can do with it? Also, if I make this function declerations and definitions in one file, is this going to be an hpp or cpp?Comment
-
An inline functions means whenever that function is called the compiler puts the code from that function in that specific spot. Regular functions (not inline) are called and the code executes the function. That might cause some overhead. That's why if a function is small and is called many times, it might be good to make it inline. But, it is dangerous because your file will grow in size depending on the size of the function. If you want to make you functions inline in a class just declare in the class declaration them in the .hpp file.Originally posted by curious2007OK, thanks but what happens if a function is inline? Does this put restrictions on what I can do with it? Also, if I make this function declerations and definitions in one file, is this going to be an hpp or cpp?Comment
-
An inline function is not called. Instead, the definition is inserted in line. That means there is no function. That means there can be no function address. That means no virtual functions. That means no object-oriented programming using inline functions.Originally posted by curious2007OK, thanks but what happens if a function is inline? Does this put restrictions on what I can do with it? Also, if I make this function declerations and definitions in one file, is this going to be an hpp or cpp?
The compiler needs to see the function definition in oreder o insert it in line. That means the inline function definition goes in a header file.Comment
Comment