confused with the declaration

Hello everyone,i have a simple problem which i canot understant.Here is a simple code which has a simple class ,i create an object of this class and then i call the function of this object and it runs just fine:

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

class one
{
    public:
        one(){}
        void show(){cout<<"i am one \n";};

    private:
};

int main()
{
    one ena;
    ena.show();

    return 0;
}



Well my problem apears when i try to declare "one" object like this:

1
2
3
4
5
6
int main()
{
    one ena();// void style,like ena overloaded constructor ena(5,6);

    return 0;
}
[/code]

In that case my compiler doesn't return me any error,but if a want to use this object ,for example:

1
2
3
4
5
6
7
8
9
10
11
[code]int main()
{
    one ena();
  
    ena.show();
   //or
  //  ena().show();


    return 0;
}
[/code]
[/code]

and i try to execute ,my compiler returns me "class one __cdecl ena(void)"

Can you please explain me why this is hapening? An overloaded constructor can be declared like <<one ena(5,6);>> but the default constructor not.Why is that? Thanks !
That's because one ena(); is a function declaration. You're declaring that there is a function ena that returns an one object and takes no parameters.
one ena(); is the prototype for a function called 'ena' which takes no arguments and returns an object of type 'one'.
To call the default constructor, avoid the parentheses

[edit] Slow :^(
Last edited on
ok ,thanks
Topic archived. No new replies allowed.