constructor initialization

what mean by 4th line of this code

1
2
3
4
5
6
7
class cnn{
int x=0;
public:
cnn() :x(5)
{
}
};
Last edited on
is it equal to
class cnn{
int x=0;
public:
cnn()
{
x=5;
}
};
Last edited on
and why constructor don't have any return value?
Yes, it's very similar, but it is not quite the same.

1
2
3
4
5
6
7
8
class cnn{
  int x;
public:
  cnn()
    : x(5)  // <- this is the "initializer list"
  {
  }
};


The initializer list lets you call constructors for parent classes and members.

Not using the initializer list... and just doing a x = 5; in the ctor body will default construct the object, then reassign it. For basic types like int... this will normally work out to be the same thing... but for complex types like strings... it might be less efficient than constructing the string directly. And for parent classes/references/const members it simply isn't possible to do this in the ctor body and you MUST use the initializer list.

Example:

1
2
3
4
5
6
7
8
9
10
class cnn{
  const int x;
  const int y;
public:
  cnn()
    : x(5)  // <- OK
  {
    y = 6; // <- error.. 'y' is const and cannot be assigned.
  }
};



EDIT:

and why constructor don't have any return value?


Because the object itself is the implied return value:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class foo
{
public:
    foo(int x) { ... }
};

void func( foo someobj );

int main()
{
    func( foo(4) ); // <- legal because we're constructing a 'foo' object and
      // passing that object to our 'func' function.  If the ctor returned another
      // value, this wouldn't make sense.
}
Last edited on
The initializer list lets you call constructors for parent classes and members.
what your mean by this?
and why we cannot initialize const without initialization list?
Last edited on
You are reading a book, yes?

Have you had a chapter about constructors yet? Parent classes? Class member variables? You ask "means what?" like you had not.

Please, explain those concepts to us as like you understand them now. It helps us to focus our terminology more appropriately.
and why constructor don't have any return value?

Constructors never have a return value. They are called automatically when you create an object. You never call them directly, so you could never receive a return value from them.

and why we cannot initialize const without initialization list?

Because you can never assign a value to a const variable. You can only initialise them.

i mean why we MUST have to use initializer list -for parent classes/references/const members it simply isn't possible to do this in the ctor body and you MUST use the initializer list.
@hellcoder I think you're viewing this from the wrong angle. Constructor initializer list is how constructors are written in C++, for the same reason the character '+' performs addition: that's how the language was designed.

The questions you should be asking are
1) when can a base or member be omitted from the constructor initializer list (the answer is: when it can be default-initialized (which is what makes it impossible to omit const and reference members) or when it has an in-class brace-or-equal initializer)

2) and when can the value of a member be changed in the constructor body (the answer to that is: when it is assignable, as is the case with your 'x').
Last edited on
(1) for parent classes to initialize its data member here in this example if create object of A then compiler showing error why?
(2) and suppose if i create any object of A then will it data member that is initialize through b have same value for all objects crated for A?

#include <iostream>
using namespace std;

class A {
int i;
public:
A(int );
};

A::A(int arg) {
i = arg;
cout << "A's Constructor called: Value of i: " << i << endl;
}

// Class B is derived from A
class B: A {
public:
B(int );
};

B::B(int x):A(x) { //Initializer list must be used
cout << "B's Constructor called";
}

int main() {
B obj(10);
A ob;
return 0;
}
Last edited on
1) What error?
2) What do you mean by "for all objects"?
3) Code tags would still be nice.

You don't call constructor explicitly. Therefore, it cannot be called within body of function. Initializer list allows selecting a constructor and passing parameters to base class.

Remember that initialization occurs in the order that the base classes and members are declared in the definition of the class. Compiler should warn, if the initializer list does not have the same order. Within body of constructor you can modify members in any order you wish, but they have all been initialized before that.

Consider T * p = new T;. new allocates memory, initializes it to look like an object of type T, and returns the address of that memory location. No "value" is returned.
error is
28|error: no matching function for call to ‘A::A()’|
EDIT:
for all objects means if i create more than one object
Last edited on
error is
28|error: no matching function for call to ‘A::A()’|

Are you sure you've shown us all your code? I don't think I can see anywhere in the code you've posted that would call the default constructor of A.

Of course, this would be easier if you'd use code tags, as you've already been asked to do previously in this thread.

Oh, and where you've written:

class B: A {

you haven't specified an access specifier for inheritance. The default is private inheritence. Is that really what you intended?
edited!
check again the code!
code tags..?
Well, clearly you know why you're getting the error, because you specifically added a line to generate it! So what's your question?
my question is that
if i do like that B obj(10);
then b's constructor will be called and 10 will be passed to constructor of A like A(x)
i want to ask if i create a obj to A in main then for that obj value will be already assigned to it(for i)
or what?
No, objects do not magically become homogenous after you've inherited from a class.

http://ideone.com/l0R47N

Consider writing code to test your assumptions.
No.

Consider this: int foo = 42;
Does it assign a value 42 to every int variable that you will create in your program?

Same with class types. When you create one instance of B, you do have one instance of A too, because B IS-A A. That A is part of B. You can then create more B's, each with different value in A::i, and as many plain A objects as well, all with unique A::i.
1
2
3
4
B bf( 4 );  // bf.i == 4
B bt( 10 ); // bt.i == 10
A au( 79 ); // au.i == 79
A ti( 22 ); // ti.i == 22 


Your A ob; is an error because A does have only the A::A(int) constructor.
Last edited on
Topic archived. No new replies allowed.