Calling functions of class and outside with same name!

Hey friends... Long time didnt use forum... But now i got a fairly interesting thing to get solved.
Just a few moments ago i was just doing foolish things in c++ and discovered something new. Though some of you might have known this, for those who dont know, take a look at the follwing 2 small programs,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream.h>
#include <conio.h>
void main();
void loop()
{
  cout<<"calling main..."<<endl;
  getch();
  main();
}
void main()
{
  cout<<"calling loop..."<<endl;
  getch();
  loop();
}

here you can see, i declared main 'cause have to call it in loop and actually can call main from another function... though it is wierd, it works and pretty logical... like having a mirror in front of a mirror... now take a look at this...
1
2
3
4
5
6
7
8
#include <iostream.h>
#include <conio.h>
void main()
{
  cout<<"calling main()"<<endl;
  getch();
  main();
}

here you can see that, in general, a function can call itself during its run and main() is no exception...
Now having given some unusual intro, let me give my exact problem now...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream.h>
#include <conio.h>
class vector
{
  int i,j,k;
  public:
  void main();
}
void vector::main()
{
  cout<<"main of class..."<<endl;
  getch();
  main();
}
void main()
{
  vector A;
  cout<<"main as usual..."<<endl;
  getch();
  A.main();
}

so here is my problem. i think u wud have figured out what m trying to do above. am actually calling the main() of the class and from there, i want to call the usual main... the problem is, during A.main()'s run, if i refer to main(); , that call represents itself, that it, it is like it calls itself. How on earth can i call the outside main?? And please explain ur answers, because am a beginner. thanx in advance! :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

using namespace std;

int main();
int (*fptr)() = main;

class veld
{
  int i,j,k;
  public:
  void main();
};

void veld::main()
{
  cout<<"main of class..."<<endl;
  getchar();
  fptr();
}

int main()
{
  veld A;
  cout<<"main as usual..."<<endl;
  getchar();
  A.main();

  return 0;
}


There's an implicit this-> within the class' methods, so calling main will call itself. You'll need an alies for the main function, you can do this with a function pointer.
oh well... can u xplain it? like i told, am a total newbie to classes...
what xactly does int (*fptr)() = main; and fptr(); do?
Last edited on
Sorry.

The fptr is a function pointer. Its syntax may be a little weird at first sight, I can explain how it works:

int (*fptr)();

int is the return type, this function pointer will point to a function that returns an integer.

(*fptr) is basically the "declaration of a pointer", fptr is the name, you can replace it by anything.

() the end of the declaration, this contains what function arguments will be given. In your case, it's just int main();, so we do not put anything inside.

An example where there are arguments:

int (*another_fptr)(int, char);

fptr(); simply calls the function that fptr holds! we assign main to the fptr, so when we call fptr();, we actually call main() (which is not the veld::main() function) :D
now i understand... its just like a reference for a function! so i can use this anywhere? is there any special name to this?
so now all these things are fine... and no compiler errors... but is that legal? i mean a program cant have more than one main function right? or can it? :/
1
2
> void main()
> {


main() must return int


> here you can see, i declared main 'cause have to call it in loop
> and actually can call main from another function...

No. The global function main() can't be called from within the program.


> How can I call functions of class and outside with same name?

Use the scope resolution operator.
http://msdn.microsoft.com/en-us/library/b451xz31(v=vs.110).aspx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

void foo()
{
    std::cout << "::foo\n" ;
}

struct veld
{
    void foo()
    {
        std::cout << "veld::foo\n" ;
        ::foo() ; // call ::foo
    }
};

int main()
{
    veld v ;
    v.foo() ; // call veld::foo()
}



> i mean a program cant have more than one main function right? or can it?

A program can have only one main() function at global scope (and it must return an int).

However, the name main itself is not reserved; it can be freely used for other purposes.
Last edited on
I realize your example may be for demonstration purposes only, but please consider structuring your code in such a way that you'll never have to implicitly call the main execution method. There's really no reason for doing so and it makes your code cringe-worthy.
Topic archived. No new replies allowed.