C++ what to code if i put a class after main() function

前端 未结 5 923
滥情空心
滥情空心 2020-12-30 17:26

I\'m watching some video tutorials on C++ and i know you must define a function / class before it is used or called. But I like having my main() function at the top, and eve

相关标签:
5条回答
  • 2020-12-30 17:47

    This is why header files are normally used in C++. When you're saying ClassOne one, the compiler needs to know what the class looks like to create an object of that type. It's not enough to know that the class exists somewhere (that is enough if all you want is a pointer). So the compiler needs to already have read the definition of the class.

    Your class has to be defined before it is first used. Without putting it explicitly before main, the usual way is to create a header file. So you create ClassOne.h with the class declaration, and you have #include "ClassOne.h at the top of your file. In this situation the actual methods of the class would normally be in another source file, ClassOne.cpp.

    0 讨论(0)
  • 2020-12-30 17:51

    One scenario where the class definition after the main() function makes sense:

    #include <iostream>
    using namespace std;
    
    void f();
    
    int main()
    {
        f();
        return 0;
    }
    
    class ClassOne
    {
        public:
            void coolSaying()
            {
                cout << "Cool stuff yo!" << endl;
            }
    };
    
    void f()
    {
        ClassOne one;
        one.coolSaying();
    }
    
    0 讨论(0)
  • 2020-12-30 17:53

    You cannot create an actual instance of the type (variable, value member) until the type is fully defined, as its size is not known. There is no way around that, but there is a lot you can already do with a pointer to an incomplete type.

    0 讨论(0)
  • 2020-12-30 17:57

    A class MUST be "complete" when you create an instance of it. So there is no way you can use the class before you have defined the whole content of the class.

    It is possible to do something like this:

    class ClassOne;
    ClassOne* make_class_one();
    void use_class(ClassOne *x);
    
    int main()
    {
        ClassOne* one = make_class_one();
    
        use_class(one);
        return 0;
    }
    
    
    class ClassOne
    {
        public:
            void coolSaying()
            {
                cout << "Cool stuff yo!" << endl;
            }
    };
    
    ClassOne* make_class_one()
    {
         return new ClassOne;   // Bad idea, use uniqe_ptr, but I'm lazy.
    }
    
    void use_class(ClassOne *x)
    {
        x->coolSaying();
    }
    

    But in general, we don't want to do that.

    0 讨论(0)
  • 2020-12-30 17:59

    (note: all other answers are correct, but you may find this useful)

    I discovered this idiom to invert the order of main and secondary function classes. I use to share small code with colleagues, everybody expects the core of the code (i.e. main) to be on top so they can edit it quickly. It works with classes and functions (without need of declaration) of course. Usually I can leave the preamble (first #includes) because those have include guards in most cases.

    #include <iostream>
    using namespace std;
    
    #ifdef please_see_definitions_below_main
    int main()
    {
        ClassOne one;
        one.coolSaying();
        return 0;
    }
    #else
    class ClassOne
    {
        public:
            void coolSaying()
            {
                cout << "Cool stuff yo!" << endl;
            }
    };
    #define please_see_definitions_below_main
    #include __FILE__
    #endif
    

    I use the tag please_see_definitions_below_main so it serves as comment also, but if you don't like it you can use something shorter, like AFTER.

    0 讨论(0)
提交回复
热议问题