0

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!

3 Answers3

3

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

Foo a;   // note, if you are using c++11, you can do Foo a{};

As opposed to:

Foo b();

Which actually does something totally unexpected*, and declares a function named b that returns a Foo 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 prototype getMyInt that returns an integer.

swalog
  • 4,198
  • 3
  • 28
  • 59
2

b is interpreted as a declaration of a function taking no arguments and returning an object of type class.

This is known as the most vexing parse. Edit: This is not the most vexing parse.

Community
  • 1
  • 1
emlai
  • 39,703
  • 9
  • 98
  • 145
-4

you meant something like this? NULL represents 0, you know. void means no data.

class Cl_Test
{
private:
    int m_a;
public:
    Cl_Test(int in_a= -1) { m_a= in_a; }
};

int main(int argc, char** argv) {
    Cl_Test a;
    Cl_Test b();
    Cl_Test c(void);
    return 0; }

edit:

my mistakes:

  • the "variable" b: It's not a variable, it's a function declaration
  • one should not pass a void as an argument in C/C++
iNyuu
  • 62
  • 5
  • 1
    Did you try to compile that? – emlai Mar 22 '15 at 12:49
  • I compiled it before posting it here. why? is something wrong with my answer? if so, do share.. I am always learning. – iNyuu Mar 22 '15 at 12:57
  • You aren't answering anything and are reproducing one of OP's mistakes. – juanchopanza Mar 22 '15 at 12:58
  • and what might my mistake be? – iNyuu Mar 22 '15 at 13:01
  • Here's an idea: read the other answers and the possible duplicate. – juanchopanza Mar 22 '15 at 13:05
  • they mostly didn't like how he named his class "class". my answer let him know NULL is not same as void (he did ask for difference between b and c). I feel needlessly attacked here, that's all (while I only tried to help like everyone else). if I did do something wrong with my code example, then tell me exactly what it is so I will learn. is default constructor meant as when no constructor is provided with the class declaration? and then you would for example create an array of some class elements. – iNyuu Mar 22 '15 at 13:15
  • I didn't know you can put void inside the brackets in a function call to signify no parameters. Thanks for teaching me something new. – emlai Mar 22 '15 at 22:59
  • "A *default constructor* is a constructor which can be called with no arguments" (from *cppreference.com*). So in your example that would be the `Cl_Test(int)` constructor because it has a default parameter. – emlai Mar 22 '15 at 23:06
  • 1
    The mistake you're reproducing from OP's post is the "variable" `b`. It's not a variable, it's a function declaration actually. – emlai Mar 22 '15 at 23:25
  • @zenith: _"I didn't know you can put void inside the brackets in a function call to signify no parameters"_ It's a holdover from C, where `int foo()` and `int foo(void)` mean different things. It's totally redundant in C++, though. I typically remove the `void` in C++ programs. – Lightness Races in Orbit Mar 23 '15 at 01:17
  • @LightnessRacesinOrbit Yeah, I've only seen `void` used in function declarations so far. What does `int foo()` actually mean in C then? – emlai Mar 23 '15 at 01:25
  • 1
    @zenith http://stackoverflow.com/q/693788/560648 http://stackoverflow.com/a/5075577/560648 (tl;dr it's horrible) Note that this answer makes yet another mistake, which is that passing `void` as an _argument_ is illegal (as you'd expect). It only makes sense like this in a declaration. – Lightness Races in Orbit Mar 23 '15 at 01:26
  • 1
    _"I feel needlessly attacked here"_ It's not "needless", and it's not an "attack": your answer contains multiple serious mistakes as well as missing the point of the question, plain and simple. It's not personal! – Lightness Races in Orbit Mar 23 '15 at 01:29
  • @LightnessRacesinOrbit I did not know that (about passing void as an argument). reason why I felt attacked yesterday was not because I was being told I was wrong but because no one told me what I was doing wrong and I was just being reminded over and over again I was wrong. – iNyuu Mar 23 '15 at 04:32
  • 1
    You're wrong.​​ You were referred to the other answers, which explain [some of] the problems. Next time please (a) try your code, and (b) look-up what your code means, before suggesting to someone that they should use it. Remember that you are _teaching_ and therefore have a duty of care! Thanks. – Lightness Races in Orbit Mar 23 '15 at 11:56