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

孤街浪徒 提交于 2019-11-30 10:47:50

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.

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.

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();
}

(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.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!