Click here to Skip to main content
15,901,853 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What is the difference between define/declare function with in class and outside the class in c++....
ex:-
C++
class A
{
public:
void display()
{
cout<<"function inside class";
}
};

-------------------------------------
C++
class A
{
public:
void display();
};
void A::display()
{
cout<<"function outside class";
}
Posted
Updated 8-Aug-12 21:56pm
v2
Comments
Albert Holguin 9-Aug-12 23:43pm    
In addition to what others already said... well, it's simply a whole lot more cleaner to define your methods inside a cpp file instead of the header. Why? ...mostly because a lot of practical methods will likely have more code than your one line example. In that case, it's hard to see the "bounds" of your class with the clutter of the inner workings of the class.
Volynsky Alex 10-Aug-12 5:39am    
A declaration introduces an identifier and describes its type, be it a type, object, or function. A declaration is what the compiler needs to accept references to that identifier. These are declarations:

extern int bar;
extern int g(int, int);
double f(int, double); // extern can be omitted for function declarations
class foo; // no extern allowed for class declarations

A definition actually instantiates/implements this identifier. It's what the linker needs in order to link references to those entities. These are definitions corresponding to the above declarations:

int bar;
int g(int lhs, int rhs) {return lhs*rhs;}
double f(int i, double d) {return i+d;}
class foo {};

A definition can be used in the place of a declaration.

An identifier can be declared as often as you want. Thus, the following is legal in C and C++:

double f(int, double);
double f(int, double);
extern double f(int, double); // the same as the two above
extern double f(int, double);

However, it must be defined exactly once. If you forget to define something that's been declared and referenced somewhere, then the linker doesn't know what to link references to and complains about a missing symbols. If you define something more than once, then the linker doesn't know which of the definitions to link references to and complains about duplicated symbols.

There is not difference today. Sometimes you just have to put the function outside the body of your class declaration typically when you have two classes that use each other declaration: lets say we have class A and class B where A::display() calls B::some_method() and B::func() calls A::blahblah(). In this case you have to declare the method of the first class outside the class body after the declaration of the second class to be able to call the method of the second class. The second class of course can already see the functions of the class that was declared first.

C++
// This is needed to be able to declare a B pointer or reference inside class A
// but it isn't enough for more, for example to call one of its methods.
class B;

class A
{
public:
    void func1()
    {
        printf("%s\n", __FUNCTION__);
    }
    void func2();
private:
    B* m_B;
};

class B
{
public:
    void blahblah()
    {
        printf("%s\n", __FUNCTION__);
    }
    void blah()
    {
        m_A->func1();
    }
private:
    A* m_A;
};

void A::func2()
{
    m_B->blahblah();
}


Note that if you put A::func2() to the .cpp file instead of the header, the ordering of the class declarations and the A::func2() declaration remains the same if you preprocess the .cpp file. Visual studio can for example inline functions even if they are put into .cpp files with its link time code generation feature in return for some more link time.
 
Share this answer
 
v3
Comments
Volynsky Alex 10-Aug-12 13:30pm    
Good response
pasztorpisti 10-Aug-12 13:39pm    
Thanks!
Volynsky Alex 10-Aug-12 14:16pm    
You are welcome :)
In addition to what pasztorpisti said in his solution, there is one more important difference:

When defining a member function outside its class declaration and in a .cpp file, other parts of your software only get to see the class declaration in your .h file at compile time and not the code of the function.

This is a huge difference when implementing a library in a DLL. Should the compiler for instance decide to inline the code of your function, this code gets compiled into all other program parts using your library. Thus, you cannot change it easily at a later time. When the function code resides in .cpp file not seen by a library user, you just rebuild your DLL and you are done.

And of course, you can distribute a library in a DLL without disclosing the code in your .cpp files. But the code in your .h file, which go with the library, must be delivered to the library user and hence is visible.
 
Share this answer
 
Comments
pasztorpisti 9-Aug-12 6:08am    
+5 Good point! Would clarify the OP that an exported DLL function can not be inlined in any case but an exported function might return a pointer to an interface (my favorite method - I have a half finished article on this) that can have inline methods. This is very dangerous! This is why its a good practice to use interfaces - classes that have only pure virtual/abstract methods - to communicate with DLLs.
nothing, really ... it *used* to be that those defined within the class were automatically inline'd by the compiler - I'm talking early 1990's
 
Share this answer
 
The difference is that when definition and declaration are separate, the definition can be put in a different file (e.g. a .cpp file), or compilation unit.

This, in turn, means that the compiler can compile this once, into an object file, which the linker can link to.

If you have the function definition inside the class definition, as in your first example, this code will be compiled every time a file that includes the class definition is compiled.

In large projects, with thousands of classes, separating the definition from the declaration can speed up compilation/linking time significantly.
 
Share this answer
 
Short answer:

Don't put the implementation of your functions into the header files unless you want to inline them. And don't inline functions unless your profiler tells you it's needed.

Also check and understand this article about inlining and why not to do it[^] before using inline yourself.

P.S.: the article actually starts with the question of when to use inline, but when you read on you'll find that the answer is pretty much "never" ;-)
 
Share this answer
 
v2
define funtions inside the class:
the funtion is always with the object,memory for the function will only be alloted when you construct object.
define funtions outside the classL:
memory for funtion will be alloted as sonn as the process is running
(forgive my poor English)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900