CAbaco *one=new CAbaco(1); delete one;
- Construct a newly allocated object with argument 1, getting its pointer.
- Assign the pointer to pointer one.
- delete the object using its pointer contained in one. No problems.
Construct variable two (it's not a pointer, it's an CAbaco,) using the pointer. Calls copy constructor two.CAbaco(const CAbaco& RHS) if it existed, but it doesn't. The pointer RHS has now been lost. Leak error.Your code Equivalent with a copy constructor CAbaco two=new CAbaco(2);
CAbaco* RHS = new CAbaco(2); CAbaco two(RHS); // RHS lost, leak
Your code Equivalent with a assignment operator CAbaco two=new CAbaco(2);
CAbaco two; { CAbaco* RHS = new CAbaco(2); two = RHS; } // RHS lost, leak
- Construct variable two (it's not a pointer, it's an CAbaco,) using the the default constructor (which does exist.)
- Construct a new allocated object with argument 2, getting its pointer (RHS.)
- Assign the RHS object to variable two. Calls two.operator=(RHS) or compiler generated one.
- The pointer RHS has now been lost. Leak error.
delete GetPointer(two);//does not delete two object why?
Variable two was not allocated dynamically, can't delete.
//+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ class CAbaco { public: int m_int; CAbaco() {} CAbaco(int num){m_int=num;} }; //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { CAbaco *one=new CAbaco(1); CAbaco *two=new CAbaco(2);//Without empty constructor doesn't compile why? //Call empty constructor and then the right one, why? delete one; delete GetPointer(two);//does not delete two object why? } //+------------------------------------------------------------------+
Please help me answer questions in the code posted above:
1-with CAbaco object if i don't declare empty constructor i get an error during compilation, why?
CAbaco two=new CAbaco(2);//Without empty constructor doesn't compile why?
As you have an explicit constructor, the compiler doesn't generate a default empty one. So it tries to call existing constructor and can't as you don't provide an int parameter.
Should be :
CAbaco two(2);
And with default constructor (posted code) you have other problems, as well described by @whroeder1
See below article
- 2010.03.01
- MetaQuotes Software Corp.
- www.mql5.com
I see the problem here. New operator create a pointer, i was trying to assign a pointer to a variable, to make that i need to declare a copyconstructor. Is that right?
So what's the advatage to use pointer over variable of object? When i should use pointer and when is not recommend?
Another question:
int someFunc(CObject *obj) {} int anotherFunc(CObject &obj) {} //Same effect??
Passing object by & or * has the same effect?
I see the problem here. New operator create a pointer, i was trying to assign a pointer to a variable, to make that i need to declare a copyconstructor. Is that right?
So what's the advatage to use pointer over variable of object? When i should use pointer and when is not recommend?
Passing object by & or * has the same effect?
- A copy constructor will not fix your lost pointer problem.
- I almost never use pointers, mostly an array of objects. MetaQuotes classes uses pointers to the cObject class, because of their broken template class. Choice depends on your preferences.
- I always pass by reference, (and save a pointer if required.) They are equivalent except you can pass a null pointer, there is no such thing as a null reference. Only if the argument is optional, should you pass the pointer and test it internally..
MetaQuotes classes uses pointers to the cObject class, because of their broken template class.
Incorrect. Stdlib collections make use of the polymorphic behavior inherited from the CObject virtual methods implemented by the subclass.
I see the problem here. New operator create a pointer
No, the new operator creates a new dynamically allocated object and assigns the address of this object to a pointer. A Pointer declaration is required at some point prior to creating a dynamic object. Look at Alain's example.
MyClass obj1; //static object MyClass obj2(constructor_args); //static object with parameterized constructor MyClass *obj3; //pointer declaration obj3 = new MyClass(constructor_args); //dynamic object allocation
//+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ class CNumber { public: int m_number; CNumber() {} CNumber(int number) {init(number);} void init(int number) {m_number=number;;} }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ class CMultiply { public: CNumber m_number; CMultiply() {m_number.init(50); Print((m_number.m_number)*2);} }; //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { CMultiply number; } //+------------------------------------------------------------------+
So in the above code, when i declare m_number static object, if i want to initialize it with a desired number i need to call a method, in this case init()?
I can't declare as a member something like this, CNumber m_number(50); !!!!!
whroeder1:
2.I almost never use pointers, mostly an array of objects. MetaQuotes classes uses pointers to the cObject class, because of their broken template class. Choice depends on your preferences.
So in the above code, when i declare m_number static object, if i want to initialize it with a desired number i need to call a method, in this case init()?
I can't declare as a member something like this, CNumber m_number(50); !!!!!
Sometimes i need to sue pointer to operate on the same istance of t he object accross multiple other object, is that a good practice?So in the above code, when i declare m_number static object, if i want to initialize it with a desired number i need to call a method, in this case init()?
I can't declare as a member something like this, CNumber m_number(50); !!!!!
Sometimes i need to sue pointer to operate on the same istance of t he object accross multiple other object, is that a good practice?class CNumber { public: int m_number; CNumber() {} CNumber(int number) {Set(number);} void Set(int number) {m_number=number;;} }; class CMultiply { public: CNumber m_number; CMultiply() : m_number(50) { Print((m_number.m_number)*2);} CMultiply(int n) : m_number(n) { Print((m_number.m_number)*2);} }; //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { CMultiply number50; CMultiply anyNumber(18); }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Please help me answer questions in the code posted above:
1-with CAbaco object if i don't declare empty constructor i get an error during compilation, why?
2- When compiling CAbaco object it call first the empty constructor and then the right one. Why? CAbaco* call the right one first try.
2- Why i get leak error if i try to delete CAbaco two object?