473,378 Members | 1,592 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,378 software developers and data experts.

Private members and copy constructors

aclass A, B;

Can A access B's privates all the time, or only in a copy constructor?

-------and---------

This code:

#include <iostream>
class A
{
int my;
public:
A():my(0){}
void print(){std::cout<<my<<std::endl;}
void test(A x){x.my++;}
};

int main()
{
A a, b;
a.test(b);
a.print();
b.print();
}

Produces "0 0" when run.

Expected "0 1" or compile error.

--------------------

Please tell me this is one of those staying-up-late mistakes :-)

Thanks,
Will

Aug 6 '05 #1
7 2443
Full Decent wrote:
aclass A, B;

Can A access B's privates all the time, or only in a copy constructor?
All the time.
-------and---------

This code:

#include <iostream>
class A
{
int my;
public:
A():my(0){}
void print(){std::cout<<my<<std::endl;}
I strongly recommend rewriting it at least as

void print(ostream& os) const { os << my; }
void test(A x){x.my++;}
Passing an object by value makes a _copy_. Changing that _copy_
inside the function has no effect (in your case) on the object
from which the copy was made. Pass it by reference:

void test(A& x) { x.my++; }
};

int main()
{
A a, b;
a.test(b);
a.print();
b.print();
And doing here

a.print(std::cout); std::cout << std::endl;
b.print(std::cout); std::cout << std::endl;
}

Produces "0 0" when run.
Rightfully so.
Expected "0 1" or compile error.
I wouldn't.
--------------------

Please tell me this is one of those staying-up-late mistakes :-)


Or one of those I-don't-understand-the-difference-between-passing-
by-value-and-passing-by-reference mistakes...

V
Aug 6 '05 #2
I can't believe I made this mistake in the original code and still
passed by value in this dumb example.

Luckily you saved me and I can stay up later to make dumber mistakes.

Aug 6 '05 #3

"Full Decent" <fu********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
aclass A, B;

Can A access B's privates all the time, or only in a copy constructor?
What B?

-------and---------

This code:

#include <iostream>
class A
{
int my;
public:
A():my(0){}
void print(){std::cout<<my<<std::endl;}
void test(A x){x.my++;}
};
test() creates a local instance of class A called x using a pass_by_value /
copy ctor.

int main()
{
A a, b;
a.test(b);
a.print();
b.print();
}

Produces "0 0" when run.

Expected "0 1" or compile error.
No, instance b was never modified in any way. Only a local copy in test was
modified.

--------------------

Please tell me this is one of those staying-up-late mistakes :-)

Thanks,
Will


Try:

#include <iostream>

class A
{
int n;
public:
A():n(0) {}
void print() const {std::cout << n << std::endl;}
void test() {n++;}
};

int main()
{
A a, b;
a.test();
b.test();
a.print();
b.print();
}

Aug 6 '05 #4
Peter, the idea was to do this:

#include <iostream>
class A
{
int my;
public:
A():my(0){}
void print(){std::cout<<my<<std::endl;}
void test(A& x){x.my++;}

};

int main()
{
A a, b;
a.test(b);
a.print();
b.print();
}

This code proves that a can modify b's private parts. I wasn't sure if
this was allowed.

Aug 6 '05 #5
Ram
> This code proves that a can modify b's private parts. I wasn't sure if
this was allowed.


The access restrictions apply on a per class basis and not on a per
object one. So objects belonging to the same class can always access
each other's members whether public, protected or private..

-Ramashish

Aug 6 '05 #6
I wouldn't think all the time. Consider the following code:

template<class T>
class Foo
{
int x;
public:
template<class C> Foo(const Foo<C>& src)
{
x = src.x;
}
};

I don't think you will be allowed to access src's privates. I guess
that is because a generic class is really a standin for a new type?

Aug 6 '05 #7
Vijai Kalyan wrote:
I wouldn't think all the time. Consider the following code:

template<class T>
class Foo
{
int x;
public:
template<class C> Foo(const Foo<C>& src)
{
x = src.x;
}
};

I don't think you will be allowed to access src's privates. I guess
that is because a generic class is really a standin for a new type?


That's correct. Unless 'C' is the same as 'T', 'Foo<C>' is not the
same type as 'Foo<T>'. Another twist here: the templated "copy-
constructor" cannot replace or even overload the "true" copy-c-tor,
generated in this case by the compiler for the Foo<T> class when it
is instantiated. So, no matter what, inside the constructor template
'C' will simply _never_ be the same as 'T'. Weird, eh?

V
Aug 7 '05 #8

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...
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: rKrishna | last post by:
I was trying to understand the real need for copy constructors. From literature, the main reason for redfinition of copy constructor in a program is to allow deep copying; meaning ability to make...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.