By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,445 Members | 1,329 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,445 IT Pros & Developers. It's quick & easy.

A text book example question

P: n/a
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?

Dec 20 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Dec 20, 7:32 am, "ICBM0926" <ICBM0...@gmail.comwrote:
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);

--
Erik Wikström

Dec 20 '06 #2

P: n/a

ICBM0926 wrote:
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.
>
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?
Dec 20 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.