473,403 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

copy constructor c++

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;
}
Jul 22 '05 #1
4 1662

"penny336" <pe******@hotmail.com> wrote in message
news:c2**********@www.csis.hku.hk...
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
Jul 22 '05 #2
"penny336" <pe******@hotmail.com> wrote in message
news:c2**********@www.csis.hku.hk...
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
Jul 22 '05 #3
> > 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

Hi Jonathan,
the main pt is that
Point::Point(const Point& p) {
x = p.x;
y = p.y;
}

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.
Jul 22 '05 #4
penny336 wrote:
> 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

Hi Jonathan,
the main pt is that
Point::Point(const Point& p) {
> x = p.x;
> y = p.y;
> }

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.

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
8
by: Jesper | last post by:
Hi, Does the concept "copy constructor" from c++ excist in c#. What is the syntax. best regards Jesper.
10
by: utab | last post by:
Dear all, So passing and returning a class object is the time when to include the definition of the copy constructor into the class definition. But if we don't call by value or return by value, ...
8
by: shuisheng | last post by:
Dear All, I am wondering how the default copy constructor of a derived class looks like. Does it look like class B : public A { B(const B& right) : A(right) {}
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
9
by: puzzlecracker | last post by:
From my understanding, if you declare any sort of constructors, (excluding copy ctor), the default will not be included by default. Is this correct? class Foo{ public: Foo(int); // no...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.