This question already has an answer here:
- Default constructor with empty brackets 9 answers
I'm pretty new to c++, i'm now trying to learn all the basics, I know when default constructors are called, but when i tried different syntax it doesn't work like i expected.
Look at the following code:
class a;
class b();
class c(NULL);
'class' is a class i created with default constructor, for a and c everything works well, but for b it just won't recognize the variable as a class member.
As i see it b and c are basically the same, what's wrong than? Thanks!
Don't name your class "
class
", as it is a reserved name.As for C++, if the constructor takes no parameters, you instantiate it using
As opposed to:
Which actually does something totally unexpected*, and declares a function named
b
that returns aFoo
instance.As for
Foo c(null)
, it won't compile as there is no default constructor that takes an argument.* It is referred to as "the most vexing parse", though I find that to be an exaggeration. It can certainly catch you by surprise, but just knowing that you can declare a function prototype inside a function, should be enough to remove the "vexing" aspect.
In other words
int getMyInt();
is obviously a function prototype when placed outside any function definitions. However, since this is also the case when inside a function definition,int getMyInt();
doesn't do anything it wouldn't normally do... which is to define a function prototypegetMyInt
that returns an integer.b
is interpreted as a declaration of a function taking no arguments and returning an object of typeclass
.This is known as the most vexing parse.Edit: This is not the most vexing parse.you meant something like this? NULL represents 0, you know. void means no data.
edit:
my mistakes: