class constructor

Let's say now i have a class named "app", if i want to invoke the default constructor, i would use app a; or if I want to omit the variable name, i can use app();. but if i use app a();, a compilation error occurs. can anyone explain why?
thanks.
If the constructor does not take arguments, the parentheses should not be used.

"app a();" forward-declares a function returning an app.
If you really want to explicitly specify that you want the constructor taking 0 arguments, you can do it like this:
app a = app();

This creates an app object and then initializes a to that object.

This is also useful in initialization lists like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
class Foo
{
public:
  Foo() {}
};

class Bar
{
public:
  Bar() : f( Foo() ) {}
private:
  Foo f;
};
Last edited on
Stewbond wrote:
This is also useful in initialization lists like so: [...]

You can also:
1
2
3
Bar() : f() {}
// or
Bar() {}
I like explicitly writing it out so that there is absolutely no guessing which constructor is called.

I guess it's sort of like people that like to specify that they are using a function in the global namespace by using the :: prefix:
::abs(x); instead of abs(x);.

It's not required, but it makes things a little clearer when someone else is trying to read through what is going on.
Yeah, I'm not saying you can't use them. By the way I also use the '::' prefix, but mainly because MSVS then shows a list of the current functions, structures, typedefs and variables in the global namespace, and I just have to enter a part of the name and press Enter. A bit OffTopic, Sorry.
Topic archived. No new replies allowed.