Saves you the skill of creating an array of objects

Source: Internet
Author: User

... No matter
Student Obj[10];//obj is the pointer's first address or
student* Pstudent=new student[10];
...
Delete pstudent; ...
In fact, in the end of the initialization of each object is called the default constructor (struct is no constructor), if there are overloaded constructors, then use this method is not a waste of time? The following are the workarounds:
Class student{
Public
Student ();
Student (char name[20],int age,float score);
~student ();
void SetName (char name[20]) {strcpy (this->name,name);} Inline, this refers to the first address of the current object
void setage (int age) {this->age=age;} This is also an inline function
void SetScore (float score) {This->score=score;} This is also an inline function
char* GetName () {return name;} This is also an inline function
int Getage () {return age;}
Float Getscore () {return score;}
Private
Char name[20];
int age;
Float score;
};

Student::student ()
{
cout<< "Stduent Constructor" <<endl;
}
Student::student (char name[20],int age,float score)
{
cout<< "Stduent Constructor" <<endl;
strcpy (This->name,name);
this->age=age;
this->score=score;
}
Student::~student ()
{
cout<< "Stduent Destroy" <<endl;
}

void Fun44 ()
{
typedef student* PSTU;
student** Ppstu;
ppstu= New PSTU[10];
ppstu[0]->age=20; Wrong notation, because Ppstu[0] is just a pointer, but does not point to any student object
for (int i=0;i<10;i++)
{
* (ppstu+i) =new Student ("CCC", i,i/2.0f);//Call the overloaded constructor here to pass the first address of new to * (Pstu+i)
}
Release each pointer in the pointer array before releasing the pointer array
for (int i=0;i<10;i++)
{
Delete Ppstu[i];
}
delete [] ppstu;
}
================
Inline functions in the 1.c++ can actually be implemented without inline, directly defining the implementation in class, but be aware that the code is short, and Java and C # compilers actually have similar processing mechanisms, but are transparent to programmers
2. Skillfully use the multi-star pointer, first open the pointer to student (then the pointer is a null pointer, can not be used), and then a new
3. The release of the multi-Star pointer, the release of the pointer is indeed troublesome. NET and Java remove pointers are justified, but at the expense of performance

Saves you the skill of creating an array of objects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.