operator new
-
See the sample code Example:
class MyClass
{
MyClass(int); // parameterized ctor
};int main()
{
MyClass* p = new MyClass(10); // ok fine!
MyClass* p1 = new MyClass[6];// error no default ctor available.
return 0;
}how can I dynamically allocate array of objects using this class? do I need to overload operator new? if yes how?
SaRath. "Before you can learn to recognize what's wrong, you must learn to recognize what's right" - Raymond Chen."
-
See the sample code Example:
class MyClass
{
MyClass(int); // parameterized ctor
};int main()
{
MyClass* p = new MyClass(10); // ok fine!
MyClass* p1 = new MyClass[6];// error no default ctor available.
return 0;
}how can I dynamically allocate array of objects using this class? do I need to overload operator new? if yes how?
SaRath. "Before you can learn to recognize what's wrong, you must learn to recognize what's right" - Raymond Chen."
You have to supply a default constructor. From MSDN: "No explicit per-element initialization can be done when allocating arrays using the new operator; only the default constructor, if present, is called. See Default Arguments for more information" (MSDN[^])
Cheers Steen. "Are you gonna check your makeup when you're done whining?" John Simmons, 05/31/2006
-
See the sample code Example:
class MyClass
{
MyClass(int); // parameterized ctor
};int main()
{
MyClass* p = new MyClass(10); // ok fine!
MyClass* p1 = new MyClass[6];// error no default ctor available.
return 0;
}how can I dynamically allocate array of objects using this class? do I need to overload operator new? if yes how?
SaRath. "Before you can learn to recognize what's wrong, you must learn to recognize what's right" - Raymond Chen."
This is what I just wrote on VC7.0.
#include class MyClass { public: MyClass(int a){} MyClass(){} }; int main() { MyClass* p1 = new MyClass(6); MyClass* p2 = new MyClass[6]; }
This works just fine. -- modified at 6:50 Tuesday 15th August, 2006Abhishek
-
You have to supply a default constructor. From MSDN: "No explicit per-element initialization can be done when allocating arrays using the new operator; only the default constructor, if present, is called. See Default Arguments for more information" (MSDN[^])
Cheers Steen. "Are you gonna check your makeup when you're done whining?" John Simmons, 05/31/2006
so we should have some "Set" function to do the task of parameterized constructor right?
SaRath.
_"Where I am from, there is no plan B. So, take advantage of today becuase tomorrow is not promised. - 50 Cent"
-
so we should have some "Set" function to do the task of parameterized constructor right?
SaRath.
_"Where I am from, there is no plan B. So, take advantage of today becuase tomorrow is not promised. - 50 Cent"
Right. If you wan't to be politically OOP correct you write accessor functions to your member variables. In this case, with the member being an int, I would just assign directly to it in a loop.
Cheers Steen. "Are you gonna check your makeup when you're done whining?" John Simmons, 05/31/2006
-
Right. If you wan't to be politically OOP correct you write accessor functions to your member variables. In this case, with the member being an int, I would just assign directly to it in a loop.
Cheers Steen. "Are you gonna check your makeup when you're done whining?" John Simmons, 05/31/2006
it would be helpful if you say how such a operation is restricted with arrays. but it can be used with single object. is it the difficulty for compiler in writing same code for more than one object? or any oothr purposeful use for that?
SaRath.
_"Where I am from, there is no plan B. So, take advantage of today becuase tomorrow is not promised. - 50 Cent"
-
it would be helpful if you say how such a operation is restricted with arrays. but it can be used with single object. is it the difficulty for compiler in writing same code for more than one object? or any oothr purposeful use for that?
SaRath.
_"Where I am from, there is no plan B. So, take advantage of today becuase tomorrow is not promised. - 50 Cent"
Honestly, I have no idea why the compiler doesn't support initializing arrays of classes during construction, but according to MSDN that's just the way it is. Perhaps there's some sort of breach of the standard or a problem with the syntax. I don't know. I don't know what's your use of this is, but maybe it will be easier for you to use an array of pointers instead of a pointer to an array. Then you can allocate and initialize each element in a loop instead.
Cheers Steen. "Are you gonna check your makeup when you're done whining?" John Simmons, 05/31/2006
-
See the sample code Example:
class MyClass
{
MyClass(int); // parameterized ctor
};int main()
{
MyClass* p = new MyClass(10); // ok fine!
MyClass* p1 = new MyClass[6];// error no default ctor available.
return 0;
}how can I dynamically allocate array of objects using this class? do I need to overload operator new? if yes how?
SaRath. "Before you can learn to recognize what's wrong, you must learn to recognize what's right" - Raymond Chen."
The following will do what you want.
class MyClass { public: MyClass(int a) : m_MyInt(a) { } MyClass() : m_MyInt(0) { } void SetInt(int a) { m_MyInt = a; } private: int m_MyInt; }; int main() { MyClass* pClass1 = new MyClass(6); MyClass* pClass2 = new MyClass[5]; for (int i = 0; i < 5; ++i) { pClass2[i].SetInt(i); } delete pClass1; delete [] pClass2; }
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac