C++ related question
-
Hi friends ! Consider scenario
class A
{
public:
A()
{
cout << "In zero argument constructor";
}A(int val) { cout << "In one argument constructor"; }
};
void main()
{A *obj;
obj= new A[20];// Blah
}As you can see above, in main i used statement
obj= new A[20];
. As such the zero argument constructor gets called. What i want is that this statement cause one argument constructor to get called. How is it possible ?? ThankX. Bye Bye -
Hi friends ! Consider scenario
class A
{
public:
A()
{
cout << "In zero argument constructor";
}A(int val) { cout << "In one argument constructor"; }
};
void main()
{A *obj;
obj= new A[20];// Blah
}As you can see above, in main i used statement
obj= new A[20];
. As such the zero argument constructor gets called. What i want is that this statement cause one argument constructor to get called. How is it possible ?? ThankX. Bye ByeYou can't. What would it call each one with?
Software is everything. It also sucks. Charles Fishman [^] Awasu 1.0.2 (beta)[^]: A free RSS reader with support for Code Project.
-
Hi friends ! Consider scenario
class A
{
public:
A()
{
cout << "In zero argument constructor";
}A(int val) { cout << "In one argument constructor"; }
};
void main()
{A *obj;
obj= new A[20];// Blah
}As you can see above, in main i used statement
obj= new A[20];
. As such the zero argument constructor gets called. What i want is that this statement cause one argument constructor to get called. How is it possible ?? ThankX. Bye ByeTechnically it's not possible, but you can certainly work around it. The easiest way is to get rid of your default constructor and give your other constructor a default value for its parameter: A(int val = 17) { ... } You can also go with more complex solutions, such as global or static member variables which you initialize before creating the array, and then perhaps increment inside the constructor as each element is created. No matter what you do, all elements of the array will call the same constructor. There's no way out of it. Regards, Alvaro
When birds fly in the right formation, they need only exert half the effort. Even in nature, teamwork results in collective laziness. -- despair.com