access private member variables in copy constructor
In the copy constructor, why can the
Tricycle reference
copied directly access its private member variables
speed and
price?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
class Tricycle
{
public:
Tricycle();
Tricycle(const Tricycle&);
~Tricycle() {}
int getSpeed() const { return speed; }
int getPrice() const { return price; }
private:
int speed;
const int price;
};
Tricycle::Tricycle():
speed(5),
price(100)
{
}
Tricycle::Tricycle(const Tricycle &copied):
speed(copied.speed),
price(copied.price)
{
}
|
Because it's an instance of the same class.
When can an instance access its private member, and when cannot?
Always.
Topic archived. No new replies allowed.