-2

I have this chuck of code and I'd really appreciate if someone could help me out by giving some more information about what it is. I know a bit of coding, but not too much so please bear with me if this seems silly.

class A
{
public:
    A();
};
int x, y, z;

A::A():
x(1),
y(2),
z(3)

{
    //some code here basically more variable definition
}

What I don't get is this,

  1. a function which is also the name of a class? Even if it works, but why would I want to do that?

  2. what is up with the variable definition of x, y, z. I assume some constructors are being called, but what's wrong with defining them the normal way? And what does do they being after the colon : but before the function definition signify?

Thank you. Much appreciated.

Blacktempel
  • 3,935
  • 3
  • 29
  • 53
  • 6
    It will take you forever to learn C++ a question at a time. Better read [a good book or two](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) first. – juanchopanza Nov 19 '13 at 07:17
  • 4
    You should read a tutorial on C++. Stack Overflow isn't for every possible combination of "what does this completely ordinary piece of syntax do?" – user229044 Nov 19 '13 at 07:18
  • As much as I'd hate to admit, I agree. I had learnt all this a couple of years ago and I am trying to relearn it. There are some gaps in my knowledge but I know just enough, so that when reading my old book, it all seemed trivial to me. I am quite aware of constructors but I somehow forgot the fact that they can also be used with classes. Hadn't used them much back then either. Would you'll have any personal recommendations for learning C++ online? – user2994497 Nov 19 '13 at 08:14

2 Answers2

1

Really, a C++ tutorial is what you need, but here goes:

A "function" with the same name as the class is a constructor. There can be several of these, each taking different arguments. In this case, the constructor takes no arguments at all (and so is called a default constructor). Not all classes have a default constructor, but if you don't write any constructors of your own, then the compiler will create a default constructor for you.

The bit after the colon is a special bit of syntax used in constructors. It's called an initializer list. What happens is that before you get to the body of the constructor, the member variables listed are initialized with those values (or, in the case of objects, constructors are called with those arguments). You should always use an initializer list to set the values of variables if you can.

In this case then, the default constructor specifies that the member variables x, y and z should be initialized to 1, 2 and 3 respectively (at least I assume they're meant to be member variables -- as written, they're actually globals).

Tristan Brindle
  • 16,281
  • 4
  • 39
  • 82
1

What you have in your question isn't valid C++.

class A
{
public:
    A();
};

int x,y,z; // x,y,z are out here in the wild

// This is supposed to be the definition of the constructor of "A"
// outside the "A" class
//
// The "A::A() : " sequence introduces a list of member initializers.
A::A() :
x(1),    // <- These are not members of "A", so they cannot be initialized as members!
y(2),    // <-
z(3)     // <-
{
}

What I think you meant was:

class A
{
public:

    int x,y,z;

    A();
};

A::A() :
x(1),    // <- Now members of "A", so they can be initialized here
y(2),    // <-
z(3)     // <-
{
}
defube
  • 2,395
  • 1
  • 22
  • 34