-1

I'm new to using class/objects. I'm trying to write something like this:

class C1{

    main ()
    {
         C2::foo();
    }
};

class C2:public C1
{
public:
    foo()
    {
    }
};

It compiler complains C2 is not a class or namespace when C2::foo() is called. If I move class C2 definition in front, then C1 is not defined. What can be done here?

Leigh Chao
  • 5
  • 1
  • 1

4 Answers4

2

You can create a class with a member function named main, but it will not be the main that gets invoked to start your program -- that needs to be a global function (outside any class or namespace). But yes, a member function named main is perfectly fine (ยง3.6.1/3):

The name main is not otherwise reserved. [Example: member functions, classes, and enumerations can be called main, as can entities in other namespaces. -- end example]

As far as how to arrange your code, you typically end up with something like this:

class C1 { 
    int main();
};

class C2 : public C1 { 
public:
    int foo();
    // or perhaps: static int foo();
};

int C1::main() {
    C2::foo(); // given `C2::static int foo();`
    // otherwise: C2 c; c.foo();
}
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
1

main should not be inside of a class at all.

matzahboy
  • 3,004
  • 20
  • 25
1

No, main cannot be inside a class. (Just to make it clear, you can have functions called main in a class, but they won't be the entry point of your program.)

(C++ Standard n3337, section 3.6.1)

A program shall contain a global function called main, which is the designated start of the program.

us2012
  • 16,083
  • 3
  • 46
  • 62
1

a main can be inside of a class. But its not the same as the entry point main, which must be global!

Here is an example:

#include <iostream>

using namespace std;

class C2 {
   public:
     void foo() {
        cout << "Foo!";
    }
};

class C1 {
   public:
     void main() { // not the same as the entry-point `main`
       cout << "What?";
       b.foo();
     }
  private: 
     B b;
};



int main() //actual entry point `main`
{
   A a;
   a.main();
   return 0;
}

Note however, your code is seriously flawed, for one - the member functions must have a return type.(void if you're not returning anything). Secondly, foo is an object function, which means it can only be invoked with an object of type C2.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
  • You are confusing readers by saying `Contrary to the answers, a main can be inside of a class.`. In your example, main *is not* inside class, there is only a method called main, what is much different from `main` we're talking about. Your `main` still *is not* inside class. A valid way of saying what you want to say is like Jerry Coffin is doing in next answer. โ€“ Spook Feb 26 '13 at 05:54
  • @Spook, yeah edited it out. I don't recall what I read that made me write "contrary to the answers" statement. But it was something to do with one of the answers posted at that time. โ€“ Aniket Inge Feb 26 '13 at 07:06
  • I'd still clear it out like: `You can write program in object manner by creating static function called main in some class. It still has to be called from global main function though`. โ€“ Spook Feb 26 '13 at 07:19