Member function definition outside of class

后端 未结 3 1776
青春惊慌失措
青春惊慌失措 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:35

    You can define a method outside of your class

    // A.h
    #pragma once
    class A 
    {
    public:
        A (): foo (10) {}
        int bar();
    private:
        int foo;
    };
    
    // A.cpp
    int A::bar () 
    {
        return foo;
    }
    

    But you cannot declare a method outside of your class. The declaration must at least be within the class, even if the definition comes later. This is a common way to split up the declarations in *.h files and implementations in *.cpp files.

提交回复
热议问题