Hi,
I think my concept has a bug that I cannot handle the pointer array passing throughout function very well. Take the following code as an example:
#include <iostream.h>
#include <string.h>
class books
{
private:
char author[30];
char title[30];
public:
books() //constructor1
{
strcpy(author, "none");
strcpy(title, "none");
};
books(char auth[30], char tit[30]) //constructor2
{
strcpy(author, auth);
strcpy(title, tit);
};
~books()
{
cout << "Destructor: " << author << endl << endl;
};
void set_author(char s[30]);
void set_title(char s[30]);
void* pointer(const char* a[30]); //Am I right or wrong?
void* pointer1(const char* t[30]);
void display();
};
void books::set_author(char s[30])
{
strcpy(author, s);
}
void books::set_title(char s[30])
{
strcpy(title, s);
}
void* books:ointer(const char* a[30]) // Am I right or wrong?
{
strcpy(author, *a);
return a; //How should I set the return value?
}
void* books:ointer1(const char* t[30])
{
strcpy(title, *t);
return t;
}
void books::display()
{
cout << author << " , " << title << endl << endl;
}
int main(int argc, char* argv[])
{
books bk1, bk2, bk3("Paul", "Story book");
bk1.set_author("Raymond"); //through base class public function to access the data members
bk1.set_title("Eye on HK");
bk1.display();
bk2.display(); //constructor 1 (call constructor 1)
bk3.display(); //constructor 2 (call constructor 2)
books *bk4 = new books("Peter", "Maths"); //Dynamic pointer and allocate a memory space
bk4->display();
delete bk4;
/*char x[] = "ABC";
books *bk5;*/ // Don't know how to pass char array "ABC" to the void pointer function above to set the author name???
return 0;
}
Also, is there no such thing: reference array? e.g. &x[10].
If so, reference will only deal with single int, char or string but not array??
There is pass by value - the local variables in main will not change.
pass by reference - the local variables in main will change.
How about pointer? Is it a kind of pass by reference? I messed up these stuff. Anyone could give me an example like the above how to pass pointer between main and function? and when should I use it?
Thank you very much.
gogo