Member function definition outside of class

后端 未结 3 1737
青春惊慌失措
青春惊慌失措 2021-01-19 14:54

Is it possible to define function or method outside class declaration? Such as:

class A 
{
    int foo;
    A (): foo (10) {}
}

int A::bar () 
{
    return          


        
3条回答
  •  臣服心动
    2021-01-19 15:43

    Yes, but you have to declare it first in the class, then you can define it elsewhere (typically the source file):

    // Header file
    class A 
    {
        int foo = 10;
        int bar(); // Declaration of bar
    };
    
    // Source file
    int A::bar() // Definition of bar 
    {
        return foo;
    } 
    

提交回复
热议问题