www.digitalmars.com         C & C++   DMDScript  

c++ - array of pointer to classes...

reply Michael Comperchio <mcmprch adelphia.net> writes:
I'm confused by declarations. I'm trying to dynamically create an array 
of pointers to classes. so:


#include 
<stdio.h>
#include <string.h>

class emails
{
public:
     emails(){};
     emails(char *FName, char *LName, char *EMail, int ID){
	    strcpy(fname, FName);
	    strcpy(lname, LName);
	    strcpy(email , EMail);
	    id = ID;
     };

	char * GetName() const { return strcat(fname,lname);};
	void SetName(char * Fname, char *Lname){
	    strcpy(fname, Fname);
	    strcpy(lname, Lname);
	}
	char * GetEmail() const { return email;};
	void SetEmail( char * EMail) { strcpy(email,EMail);};

private:

	char fname[50];
	char lname[50];
     char email[50];
	int id;
};


void main(int argc, char *argv[])

{
    {
     /* Print a prompt prior to exiting so that you can view output */
     emails *local_emails[];
// I read this as: local_mails is an array of pointers to 
emails...however... the compiler tells me size of array is not 
known...ok so
     emails **local_emails;

//it doesn't like this either...can I do something like this in C++????

     local_emails = new emails[10];

     printf("Press a character....\n");
     getch();
    }
}
Sep 23 2002
next sibling parent reply Jan Knepper <jan smartsoft.cc> writes:
Michael Comperchio wrote:

     {
      /* Print a prompt prior to exiting so that you can view output */
      emails *local_emails[];
 // I read this as: local_mails is an array of pointers to
 emails...however... the compiler tells me size of array is not
 known...ok so
      emails **local_emails;
Ok, than: const int LOCAL_EMAILS = 10; local_emails = new ( emails * ) [ LOCAL_EMAILS ]; for ( int i = 0 ; i < LOCAL_EMAILS ; i++ ) *( local_emails + i ) = new emails ();
 //it doesn't like this either...can I do something like this in C++????
emails *local_emails; // This should do for new emails [ 10 ];
      local_emails = new emails[10];

      printf("Press a character....\n");
      getch();
     }
 }
Sep 23 2002
parent Michael Comperchio <mcmprch adelphia.net> writes:
Jan Knepper wrote:
 Michael Comperchio wrote:
 
 
    {
     /* Print a prompt prior to exiting so that you can view output */
     emails *local_emails[];
// I read this as: local_mails is an array of pointers to
emails...however... the compiler tells me size of array is not
known...ok so
     emails **local_emails;
Ok, than: const int LOCAL_EMAILS = 10; local_emails = new ( emails * ) [ LOCAL_EMAILS ]; for ( int i = 0 ; i < LOCAL_EMAILS ; i++ ) *( local_emails + i ) = new emails ();
//it doesn't like this either...can I do something like this in C++????
emails *local_emails; // This should do for new emails [ 10 ];
     local_emails = new emails[10];

     printf("Press a character....\n");
     getch();
    }
}
the actual number of emails is not known until runtime (the number of rows returned from a DB call)...but that's seems to be ok as this works... emails **local_email; .... local_email = new (emails * [res.size() * sizeof(emails *)]); the key was (emails *...) I was stuck in my stubborn way on using ...new emails[res.size() * sizeof(emails *)]
Sep 23 2002
prev sibling parent Al Bowers <abowers combase.com> writes:
Michael Comperchio wrote:

 I'm confused by declarations. I'm trying to dynamically create an array 
 of pointers to classes. so:
..............code snipped.............. Since you are using C code, you could use function malloc. You need to watch the char arrays. The way you are copying to them is very dangerous. Ex. #include <stdio.h> #include <string.h> #include <stdlib.h> class emails { public: emails(){}; emails(char *FName, char *LName, char *EMail, int ID){ strcpy(fname, FName); strcpy(lname, LName); strcpy(email , EMail); id = ID; }; char * GetName() { return strcat(fname,lname);}; void SetName(char * Fname, char *Lname){ strncpy(fname, Fname,48); fname[48]='\0'; strcat(fname," "); strncpy(lname, Lname,49); lname[49]='\0'; } char * GetEmail() { return email;}; void SetEmail( char * EMail) { strncpy(email,EMail,49); email[49]='\0'; }; private: char fname[100]; char lname[50]; char email[50]; int id; }; void main(int argc, char *argv[]) { emails *local_emails; if((local_emails=(emails *)malloc(10*sizeof(emails)))==NULL) exit(EXIT_FAILURE); int i; for(i = 0;i <10;i++) local_emails[i].SetName("George","Washington"); for(i = 0;i <10;i++) printf("local_emails[%i] name: %s\n", i,local_emails[i].GetName()); free(local_emails); printf("\nDa size of emails * = %u\n" "Da size of emails = %u\n\n",sizeof(emails *), sizeof(emails)); printf("Da End....\n"); }
Sep 26 2002