-
Constructor for a class
I am current trying to learn C++ and I'm a little confused about a constructor I found in a book I am reading and was wondering if anyone can explain to me what something means
Code:
//this is the constructor
objectc::objectc(int x, int y):
itsx(x), //Book says these are initializations
itsy(y)
{} //empty body
Ok... for the class objectc, we have this constructor, right? What are those initializations? I look in the book everywhere and wasn't able to find anything about them. It appears its taking the value in x and assigning it to itsx which is declared as a private variable in the class but the syntax looks like its calling a function, kinda... hence my confused state.
Can someone explained to me what these initializations are and what is the syntax to use them and what else can be used in this area?
Between the constructor
objectc::objectc(int x, int y):
and the body
{}
Thanks in Advance.
-
They are used to initialize members of the class They don't get real important until after you have classes that contain other classes as instances. In short, they prevent you from constucting an object and then using assignment, in favor of just directly constructing an object.
Code:
className::className(type1 arg1, type2 arg2, type3 arg3) :
member1(arg1, arg2),
member2(arg3)
{ }
-
well .........
The class apparently has two members of type integer, itsx and itsy. The class is using a constructor known as an initializer list (or sometimes other names like member-initialization list according to one book I have). The value of the arguments x and y, are assigned to the members itsx and itsy respectively. It's just not occurring in the body of the constructor.
Most books I have read say that this is the preferred method because when using this type of constructor, the compiler actually assigns the values to the members before the constructor begins to execute (EDIT* IS WHAT I MEANT TO SAY). That's about the best explanation I can give.
Are itsx and itsy const member data? Because that would be a reason to use this type of constructor.
-
Thanks for the explaination, guys. I think I have an understanding of what they are enough to get me pass this bit of code.
They aren't const data members, DISGUISED. They are only private members.
I'm not sure why this book decided to throw me a curve ball like that. Using something in example code that they never explained.
-
Here is something you can try that is kind of interesting, and along the same lines as Constructor Initializer Lists...
float f = 3.14159; // usual way
and...
float f(3.14159); // unusual !
mean exactly the same thing, assign the value 3.14159 to the float primative type. The C++ compiler allows primative types to be initialized as though they were a user defined type with a constructor call.
-
In some sense it is just a syntax that you can use when declaring a constructor in a header .h:
Otherwise you would do this in a source class implementation file .c
Code:
Your implementation source file:
objectc::objectc(int x, int y)
{
itsx = x;
itxy = y;
}
And have this in your header:
objectc::objectc(int, int);
I think this is true anyway. To be honest I sort of forget the answer Mr. H. I will come across this in my books in about six weeks from now though. It will refresh my memory.
-
Quote:
Originally posted by SilentStrike
They are used to initialize members of the class They don't get real important until after you have classes that contain other classes as instances. In short, they prevent you from constucting an object and then using assignment, in favor of just directly constructing an object.
Code:
className::className(type1 arg1, type2 arg2, type3 arg3) :
member1(arg1, arg2),
member2(arg3)
{ }
This format for initialisation has to be used for constant data members of the class. Assignment is not permitted like normal data members.