| | |
Help : Pointers to array of class objects .
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2005
Posts: 21
Reputation:
Solved Threads: 0
Hi All
I am relatively new to the C++ world . Facing some problem , while initializing an array of objects .
Suppose i have a class Rectangle . I want to have a pointer to an array of objects to the class . How do i invoke the Class Constructor , for a paricular object in the arraylist.
<< moderator edit: added [code][/code] tags >>
Pplz Help
Best Regards
Varun
I am relatively new to the C++ world . Facing some problem , while initializing an array of objects .
Suppose i have a class Rectangle . I want to have a pointer to an array of objects to the class . How do i invoke the Class Constructor , for a paricular object in the arraylist.
C++ Syntax (Toggle Plain Text)
class Rectangle { public : int side1 , side2; Rectangle(int,int); Rectangle(); void get_area(); private : protected : }; Rectangle::Rectangle(int x, int y) { side1=x; side2=y; } void Rectangle::get_area() { cout<<"Area :"<< side1*side2<<endl; } Rectangle *ptr_arr; ptr_arr=new Rectangle[2] ; // pointer to an array of objects to the class // Now how do i initialize the dimensions for the ptr_arr[1] object , using the constructors ???
Pplz Help
Best Regards
Varun
•
•
Join Date: May 2005
Posts: 48
Reputation:
Solved Threads: 3
Hi Varun,
Just try this:
Just try this:
C++ Syntax (Toggle Plain Text)
Rectangle *ptr_arr[2]; ptr_arr[0] = new Rectangle(10,20); ptr_arr[1] = new Rectangle(15,5);
•
•
Join Date: May 2005
Posts: 48
Reputation:
Solved Threads: 3
C++ Syntax (Toggle Plain Text)
Rectangle * ptr_arr; ptr_arr=new Rectangle[2];
Rectangle::Rectangle();
But if u want to call the constructor:
Rectangle::Rectangle(int x, int y)
u should use
C++ Syntax (Toggle Plain Text)
ptr_arr[0] = new Rectangle(10,20); ptr_arr[1] = new Rectangle(15,5);
•
•
Join Date: May 2005
Posts: 21
Reputation:
Solved Threads: 0
Actually what i meant was , that is there any diff b//n the memory allocation / meathod scheme of the two ..leave alone the first query of constructors
The first approach shows an array of objects being pointed to ..by the ptr_arr pointer .
Does the second one , signify a pointer array or an object array initialization ....
Wat's the difference . (just suppose that , no constructor is involved0
I hope i am clear .....
Best Regards
Varun
The first approach shows an array of objects being pointed to ..by the ptr_arr pointer .
Does the second one , signify a pointer array or an object array initialization ....
Wat's the difference . (just suppose that , no constructor is involved0
I hope i am clear .....
Best Regards
Varun
•
•
Join Date: May 2005
Posts: 48
Reputation:
Solved Threads: 3
Look Varun, the main difference between these 2 is that the 1st one allocates the memory at the time of running (dynamic memory allocation). But the 2nd one does the same at both compile and run time. First it creates memory at the time of compilation (ie, static memory allocation) for the line :
Rectangle * ptr_arr[2];
and after that it again creates the memory at run time for :
ptr_arr[0]=new Rectangle;
ptr_arr[1]=new Rectangle;
So I think if there is no specific reason, the 2nd approach should be avoided.
Amit
Rectangle * ptr_arr[2];
and after that it again creates the memory at run time for :
ptr_arr[0]=new Rectangle;
ptr_arr[1]=new Rectangle;
So I think if there is no specific reason, the 2nd approach should be avoided.
Amit
![]() |
Similar Threads
- Array of Class Objects? (C++)
- Passing Class Objects, Instances (C++)
- Initializing an array as a class mamber (C++)
Other Threads in the C++ Forum
- Previous Thread: WriteLine Specifyng what line to write in a textfile
- Next Thread: Printing a Queue to file
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





