hi,
i just read that you can initialize a variable
int num = 10;
like this
int num(10)
does any one ever do this ? are there any differences at all however subtle between the 2?
Thanks.
hi,
i just read that you can initialize a variable
int num = 10;
like this
int num(10)
does any one ever do this ? are there any differences at all however subtle between the 2?
Thanks.
I do this as well.
int num = 10; <-- creates the variable, initializes with the default value, then assigns 10 to it.
int num(10); <-- creates the variable and initializes it with whatever's between the parentheses, using the constructor.
That's (parantheses) the standard way to initialize a programmer defined class object, at least as far as I've learned .Originally posted by The Gweech
hi,
i just read that you can initialize a variable
int num = 10;
like this
int num(10)
does any one ever do this ? are there any differences at all however subtle between the 2?
Thanks.
E.g., you create a class called Employee that contains first & last names & id #. When you create an object of class Employee, you can initialize it it like this:
A class is like a built in type (int, double, char, etc). There are probably differences, but that's what I've learned so far.Code:Employee peon("Joe", "Sixpack", 666);
There is actually no real difference in the two different syntaxes, but the parenthetical syntax is required in the initialization lists for class constructors:
class some_class
{
public:
some_class() : varone(1), // Parentheses used here
vartwo(2),
varthree(3)
{
// Initialize...
}
private:
int varone, vartwo, varthree;
};