copy constructor c++

P

penny336

dear all,
just a little confused in c++ oop , in the below class, i set x,y as
private, but it can be accessed directly in copy constructor e.g x=p.x
is it problems in c++?
//=== file Point.h =============================================
class Point {
public:
Point(const Point& p); // copy constructor private: int x,
y;};//=== file Point.cpp ==========================================
.. . .
Point::point(const Point& p) {
x = p.x;
y = p.y;
}
 
J

John Harrison

penny336 said:
dear all,
just a little confused in c++ oop , in the below class, i set x,y as
private, but it can be accessed directly in copy constructor e.g x=p.x
is it problems in c++?
//=== file Point.h =============================================
class Point {
public:
Point(const Point& p); // copy constructor private: int x,
y;};//=== file Point.cpp ==========================================
. . .
Point::point(const Point& p) {
x = p.x;
y = p.y;
}

Yes, that is correct. Access is class based, not object based.

If you think about the purpose of public/private it makes perfect sense.
Basically you are writing the whole class, including the copy constructor
and therefore you can make sensible decisions about accessing private
variables. The point is hiding the internals of a class from users, not
hiding the internals of objects from each other.

john
 
J

Jonathan Turkanis

penny336 said:
dear all,
just a little confused in c++ oop , in the below class, i set x,y as
private, but it can be accessed directly in copy constructor e.g x=p.x
is it problems in c++?
//=== file Point.h =============================================
class Point {
public:
Point(const Point& p); // copy constructor private: int x,
y;};//=== file Point.cpp ==========================================
. . .
Point::point(const Point& p) {
x = p.x;
y = p.y;
}

Member functions of a class have access to its private data.

BTW, while it may be good programming practice to put function
definitions in a separate file, when posting code here it's easier to
read if you put the definition in class:

class Point {
public:
Point(const Point& P)
{
// ...
}
// ...
};

Also BTW, in your case the compiler-generated copy constuctor would be
fine.

Jonathan
 
P

penny336

class Point {
Member functions of a class have access to its private data.

BTW, while it may be good programming practice to put function
definitions in a separate file, when posting code here it's easier to
read if you put the definition in class:

class Point {
public:
Point(const Point& P)
{
// ...
}
// ...
};

Also BTW, in your case the compiler-generated copy constuctor would be
fine.

Jonathan
Hi Jonathan,
the main pt is that
Point::point(const Point& p) {object p is not the class , it is just a object and p.x and p.y is trying to
access its private member.
also thx ur advice, i separate declaration and implement in two files.
 
R

Rolf Magnus

penny336 said:
Hi Jonathan,
the main pt is that
Point::point(const Point& p) {
object p is not the class ,

Yes, it is. It's of class Point, of which the above is a constructor.
it is just a object and p.x and p.y is trying to access its private
member.

A constructor of Point is trying to access a private member of class
Point, i.e. of the same class. And that's allowed. It doesn't matter
what object p refers to. That object is of the same class, and that's
all that counts.
 

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

Forum statistics

Threads
474,163
Messages
2,570,897
Members
47,435
Latest member
PhilipBelm

Latest Threads

Top