I
ICBM0926
Hi
I am studying about copy constructor in C++.I changed a part of a
text book example like follows:
========================================
#include <iostream>
#include <cstring>
using namespace std;
class product
{
int *number;
public:
char *name;
int get_number(){return *number;}
void set_number(int n){*number = n;}
product(char *text, int count);
product(const product &other_product);
};
product:
roduct(char *text, int count)
{
name = new char[40];
strcpy(name, text);
*number = count;
}
product:
roduct(const product & p)
{
name = new char[40];
strcpy(name, p.name);
*number = p.*number;
}
main()
{
product oranges("oranges", 200), also_oranges(oranges);
cout << "Number of " << also_oranges.name << ": " <<
also_oranges.get_number() << endl;
system("pause");
return 0;
}
==================================================
I make the number become a pointer to try how to copy a int pointer
instead of using strcpy to copy a string.But the compiler gives me some
erroe message like following:
`((product*)this)->product::number' cannot be used as a member pointer,
since it is of type `int*' in line 27.
May I ask What causes the problem?
I am studying about copy constructor in C++.I changed a part of a
text book example like follows:
========================================
#include <iostream>
#include <cstring>
using namespace std;
class product
{
int *number;
public:
char *name;
int get_number(){return *number;}
void set_number(int n){*number = n;}
product(char *text, int count);
product(const product &other_product);
};
product:
{
name = new char[40];
strcpy(name, text);
*number = count;
}
product:
{
name = new char[40];
strcpy(name, p.name);
*number = p.*number;
}
main()
{
product oranges("oranges", 200), also_oranges(oranges);
cout << "Number of " << also_oranges.name << ": " <<
also_oranges.get_number() << endl;
system("pause");
return 0;
}
==================================================
I make the number become a pointer to try how to copy a int pointer
instead of using strcpy to copy a string.But the compiler gives me some
erroe message like following:
`((product*)this)->product::number' cannot be used as a member pointer,
since it is of type `int*' in line 27.
May I ask What causes the problem?