Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Initializer and comma seperated lists

Reply
Thread Tools

Initializer and comma seperated lists

 
 
Pmb
Guest
Posts: n/a
 
      05-26-2004
I'm trying to learn the syntax for initializing objects in a comma separated
list. Below is an example program I wrote to learn how to do this (among
other things). While I understand how to initialize a primitive data type
with a comma separated list, e.g.

int a[3] = { 11, 23, 43 };

I know how to initialize the example object's "student1" and "student2"
below, e.g.

Student student1( "Isaac", "Newton" ), student2( "Albert", "Einstein" );

What I don't know is how to initialize an array of objects of class Student.

Suppose I had the following

int a1 = 11, a2 = 23, a3 = 43;

and I then decided to make my program work a bit differently and changed to

int a[3] = { 11, 23, 43 };

In this sense how do I go from

Student student1( "Isaac", "Newton" ), student2( "Albert", "Einstein" );

to

Student students[2] = ????

I tried what seemed like the logical thing to do, i,.e

Student students[2] = { { "Isaac", "Newton"}, { {"Albert", "Einstein"} };

That won't compile. Is there a way to create an array of objects with a
comma seperated list for initialization?

Thanks

Pmb

______________________________________

#include <iostream.h>

class Student{
public:
Student( char* = "", char* = "");
~Student();
void print();
static int getCount();
private:
char *firstName;
char *lastName;
static int count;
};

Student::Student( char *first, char *last )
{
firstName = new char[ strlen( first ) + 1 ];
strcpy( firstName, first );
lastName = new char[ strlen( last ) + 1 ];
strcpy( lastName, last );
++count;
}

Student::~Student()
{
delete firstName;
delete lastName;
--count;
}

void Student:rint()
{
cout << "Student Name: " << firstName << " " << lastName << endl;
cout << endl;
}

int Student::getCount(){ return count;}

int Student::count = 0;

int main()
{
Student student1( "Isaac", "Newton" ), student2( "Albert", "Einstein" );

student1.print();
student2.print();
cout << "Number of students: " << Student::count << endl;

return 0;

}
______________________________________


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      05-26-2004

"Pmb" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I'm trying to learn the syntax for initializing objects in a comma

separated
> list. Below is an example program I wrote to learn how to do this (among
> other things). While I understand how to initialize a primitive data type
> with a comma separated list, e.g.
>
> int a[3] = { 11, 23, 43 };
>
> I know how to initialize the example object's "student1" and "student2"
> below, e.g.
>
> Student student1( "Isaac", "Newton" ), student2( "Albert",

"Einstein" );
>
> What I don't know is how to initialize an array of objects of class

Student.
>
> Suppose I had the following
>
> int a1 = 11, a2 = 23, a3 = 43;
>
> and I then decided to make my program work a bit differently and changed

to
>
> int a[3] = { 11, 23, 43 };
>
> In this sense how do I go from
>
> Student student1( "Isaac", "Newton" ), student2( "Albert",

"Einstein" );
>
> to
>
> Student students[2] = ????
>
> I tried what seemed like the logical thing to do, i,.e
>
> Student students[2] = { { "Isaac", "Newton"}, { {"Albert", "Einstein"} };
>
> That won't compile. Is there a way to create an array of objects with a
> comma seperated list for initialization?
>


Yes

Student students[2] = { Student( "Isaac", "Newton"), Student("Albert",
"Einstein") };

Curly brackets at the beginning and end, round brackets elsewhere.

john


 
Reply With Quote
 
 
 
 
Pmb
Guest
Posts: n/a
 
      05-26-2004
> Student students[2] = { Student( "Isaac", "Newton"), Student("Albert",
> "Einstein") };
>
> Curly brackets at the beginning and end, round brackets elsewhere.
>
> john


Excellent! Muchus Gracias!

Pmb


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Splitting comma-seperated items MSNEWS ASP .Net 3 04-21-2007 01:35 PM
XML and keeping things seperated? Stan R. XML 6 06-04-2006 11:39 PM
Problem with text seperated by lines Kamyk HTML 0 10-07-2004 08:34 PM
comma at end of initializer allowed? Serve Laurijssen C Programming 12 05-13-2004 06:08 AM
WAR and EJB-JAR in Seperated Server steven Java 2 10-26-2003 02:54 PM



Advertisments