A text book example question

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::product(char *text, int count)
{
name = new char[40];
strcpy(name, text);
*number = count;
}

product::product(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?
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Hi
I am studying about copy constructor in C++.I changed a part of a
text book example like follows: [snip]
*number = p.*number; [snip]
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?

Use parenthesis like so:
*number = *(p.number);
 
S

Salt_Peter

ICBM0926 said:
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);
};

Using pointers only complicates your design. Take member pointer int
*number for instance. The parameter in the product constructor labelled
as "count" is a temporary copy. So pointer number is pointing to
unreserved space once the ctor has completed. Sooner or later: that
will byte you since the pointer is left dangling. You therefore have
undefined behaviour (yes - thats bad).

Pointers have no way to hold a value unless they point to a valid
object or you allocate a variable with them (new). Which implies a
corresponding delete to deallocate. In your ctor, name is allocating a
char array which is not recovered anywhere - thats a memory leak. Can
you see why pointers are not worth the trouble?
product::product(char *text, int count)
{
name = new char[40];
strcpy(name, text);
*number = count;
}

product::product(const product & p)
{
name = new char[40];
strcpy(name, p.name);
*number = p.*number;
}

Try designing classes with no pointers at all except where absolutely
required. And that should include all forms of char*, using a
std::string is a much better alternative. And main() always returns
int.

#include <iostream>
#include <ostream>
#include <string>

class product
{
std::string name;
int number;
public:
product(const std::string& s, int n);
product(const product& copy);
int get_number() const { return number; }
const std::string& get_name() const { return name; }
};

product::product(const std::string& s, int n)
: name(s), number(n)
{
}

product::product(const product& copy)
{
name = copy.name;
number = copy.number;
}

int main()
{
product oranges("oranges", 200);
product also_oranges(oranges);
std::cout << "Number of " << also_oranges.get_name() << ": ";
std::cout << also_oranges.get_number() << std::endl;

return 0;
}

The output above could be made a member function or better: overload
operator<< for the product class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top