由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 子类的assignment operator 怎么访问父类的private member
相关主题
C++中怎么传递std::hex这样的参数啊为什么在overloading中,friend <<不能读取private值呢?
学scala发现一个有趣现象Why should i include .cpp instead of .h
C++子类中调用父类指针的问题请问关于overloading <<
C++: friend function能否通过父类指针来复制子类对象?
请教一个C++问题c++ 设计问题求助
为什么java要用setter和getter请教C#里property的意义
大家都用那跟手指敲 - + = [ ] ;问一个简单的:setter 和getter有什么用处?
老年工程师转行遇到下马威goodbug vs neverlearn
相关话题的讨论汇总
话题: operator话题: class话题: oo话题: private话题: base
进入Programming版参与讨论
1 (共1页)
k****y
发帖数: 781
1
要实现子类的assignment operator 怎么访问父类部分的的private member从而实现
copy
c*****t
发帖数: 1879
2
You can't. The parent classes should have their own assignment
operator and you just need to call them, and take care of the new
members introduced by this class.

【在 k****y 的大作中提到】
: 要实现子类的assignment operator 怎么访问父类部分的的private member从而实现
: copy

t****t
发帖数: 6806
3
我靠, 刚问了copy ctor,就不能举一反三一下...

【在 k****y 的大作中提到】
: 要实现子类的assignment operator 怎么访问父类部分的的private member从而实现
: copy

k****y
发帖数: 781
4
笨,没办法
不过两个实现不一样啊

【在 t****t 的大作中提到】
: 我靠, 刚问了copy ctor,就不能举一反三一下...
q*****g
发帖数: 72
5
要 thick road

【在 t****t 的大作中提到】
: 我靠, 刚问了copy ctor,就不能举一反三一下...
t****t
发帖数: 6806
6
Base::operator=(r);

【在 k****y 的大作中提到】
: 笨,没办法
: 不过两个实现不一样啊

g*****g
发帖数: 34805
7
Why would someone try to do this? This is against OOD.
Make a getter in the parents class and call it in child.

【在 k****y 的大作中提到】
: 要实现子类的assignment operator 怎么访问父类部分的的private member从而实现
: copy

k****y
发帖数: 781
8
Are you sure about this?
I think calling Base::operator=(r) is right

【在 g*****g 的大作中提到】
: Why would someone try to do this? This is against OOD.
: Make a getter in the parents class and call it in child.

k****z
发帖数: 550
9
This is right as long as the parent class provides a good operator=()
In other words, you are not touching the private members of parent class,
but just assume operator=() will do that.
This is not guaranteed to work, if the parent class disables operator=, then
there's no way to do it. In this situation, trying to access those members
are against OO.

【在 k****y 的大作中提到】
: Are you sure about this?
: I think calling Base::operator=(r) is right

L*********r
发帖数: 92
10
如果需要重用父类的function实现, 在子类的virtual或special function中调用父类
是一个rule.
normally, 子类不应该care, 也无法访问控制父类的private member, 无论调用什么
method.
相关主题
为什么java要用setter和getter为什么在overloading中,friend <<不能读取private值呢?
大家都用那跟手指敲 - + = [ ] ;Why should i include .cpp instead of .h
老年工程师转行遇到下马威请问关于overloading <<
进入Programming版参与讨论
g*****g
发帖数: 34805
11
If it's private memeber for parents, just use a getter/setter in
parents to access it, I don't understand why we need any trick
for this.

【在 L*********r 的大作中提到】
: 如果需要重用父类的function实现, 在子类的virtual或special function中调用父类
: 是一个rule.
: normally, 子类不应该care, 也无法访问控制父类的private member, 无论调用什么
: method.

L*********r
发帖数: 92
12
public getter/setter is always used to expose accessing a data member to a
class which is not in the class hierarchy tree.

【在 g*****g 的大作中提到】
: If it's private memeber for parents, just use a getter/setter in
: parents to access it, I don't understand why we need any trick
: for this.

k*k
发帖数: 508
13
what if the parent class doesn't provide the getter and setter?
it's not always the case that you have the access to the base class source

【在 g*****g 的大作中提到】
: If it's private memeber for parents, just use a getter/setter in
: parents to access it, I don't understand why we need any trick
: for this.

S*****n
发帖数: 227
14
program to the interface, not the implementation.

source

【在 k*k 的大作中提到】
: what if the parent class doesn't provide the getter and setter?
: it's not always the case that you have the access to the base class source

g*****g
发帖数: 34805
15
Then either you should make the field protected, or
if the parents' field should be private, what's the point
to hack into it, it's against OO.
It's like this friendly keyword in C++, I don't see why
it's needed.

source

【在 k*k 的大作中提到】
: what if the parent class doesn't provide the getter and setter?
: it's not always the case that you have the access to the base class source

q*****g
发帖数: 72
16
the friend keyword is need when u want to overload operator<<
if the operator << is a member function, to output the class, we need
to write this:
Widget << cout; // Widget is a object of someclass
This is against common practise.
if use friend function, the function declaratoin will like this:
friend ostream& operator<< (ostream&, Widget&);
so we can write this:
cout << widget;
which is much clearer.

【在 g*****g 的大作中提到】
: Then either you should make the field protected, or
: if the parents' field should be private, what's the point
: to hack into it, it's against OO.
: It's like this friendly keyword in C++, I don't see why
: it's needed.
:
: source

g*****g
发帖数: 34805
17
Let every object which want to be printable extends a Printable
object, and in it it has a virtual print() function,
so cout << widget becomes widget.print().
This is a better design borrowed from interface concept.

【在 q*****g 的大作中提到】
: the friend keyword is need when u want to overload operator<<
: if the operator << is a member function, to output the class, we need
: to write this:
: Widget << cout; // Widget is a object of someclass
: This is against common practise.
: if use friend function, the function declaratoin will like this:
: friend ostream& operator<< (ostream&, Widget&);
: so we can write this:
: cout << widget;
: which is much clearer.

i******l
发帖数: 270
18
what if this object need to be used in template?
just like integer, string, etc.

【在 g*****g 的大作中提到】
: Let every object which want to be printable extends a Printable
: object, and in it it has a virtual print() function,
: so cout << widget becomes widget.print().
: This is a better design borrowed from interface concept.

N********n
发帖数: 8363
19
Adding assignment operator to sub-class is messy if the base class
doesn't have one already. I suggest you drop it unless you really
wanna re-code the base class.

【在 k****y 的大作中提到】
: 要实现子类的assignment operator 怎么访问父类部分的的private member从而实现
: copy

p***o
发帖数: 1252
20
You can write a 'print' traits for that purpose such that
print_traits::print will call T::print while
print_traits::print will output the int directly.

【在 i******l 的大作中提到】
: what if this object need to be used in template?
: just like integer, string, etc.

相关主题
能否通过父类指针来复制子类对象?问一个简单的:setter 和getter有什么用处?
c++ 设计问题求助goodbug vs neverlearn
请教C#里property的意义有没有根据 model 类自动生成 html form的工具?
进入Programming版参与讨论
i******l
发帖数: 270
21
a little ugly a
不自然

【在 p***o 的大作中提到】
: You can write a 'print' traits for that purpose such that
: print_traits::print will call T::print while
: print_traits::print will output the int directly.

p***o
发帖数: 1252
22
Any suggestion?

【在 i******l 的大作中提到】
: a little ugly a
: 不自然

i******l
发帖数: 270
23
结论是还得要friend,:D

【在 p***o 的大作中提到】
: Any suggestion?
p***o
发帖数: 1252
24
I don't get it ... I thought you need some way to call
different functions based on types. How's that related
to friend?

【在 i******l 的大作中提到】
: 结论是还得要friend,:D
i******l
发帖数: 270
25
哦,这样。
我的意思是用friend function实现<<,用在模板中显得更加自然干净
用不着再去写traits。

【在 p***o 的大作中提到】
: I don't get it ... I thought you need some way to call
: different functions based on types. How's that related
: to friend?

k*k
发帖数: 508
26
that's not the point.
problem we discussed here is how to manipulate the private
member in an indirect way, i.e., with *existing* method or
override operator in the base class. Your way to do it is
one possible solution. You called it easy, yes, it's easy.
But if the base class doesn't provide such an "easy" way,
we need to use those "tricky" method. I am not blaming you
for anything. I just want to point the fact that what you
want may not always be there for you.

【在 g*****g 的大作中提到】
: Then either you should make the field protected, or
: if the parents' field should be private, what's the point
: to hack into it, it's against OO.
: It's like this friendly keyword in C++, I don't see why
: it's needed.
:
: source

g*****g
发帖数: 34805
27
You miss the point, hack the class to access its private memeber is
against OO. Many languages do not even have a way to do it at all.
So instead of doing it that way, check which part is wrong, if the parents
class is having a bad design, can you change it. And if you cannot,
can you rewrite/wrap it. And more frequently, do you really need to access
a private field at all?
Doing it this way, you are encouraged to think OO, instead of going
quick and dirty.

【在 k*k 的大作中提到】
: that's not the point.
: problem we discussed here is how to manipulate the private
: member in an indirect way, i.e., with *existing* method or
: override operator in the base class. Your way to do it is
: one possible solution. You called it easy, yes, it's easy.
: But if the base class doesn't provide such an "easy" way,
: we need to use those "tricky" method. I am not blaming you
: for anything. I just want to point the fact that what you
: want may not always be there for you.

k*k
发帖数: 508
28
sigh. I am not saying "access", but "manipulate indirectly"
you are still doing it in OO way but an indirect way. you don't
access the private member in base class directly, you access them
through what base class provided (e.g., the operator =()). of coz
i agree with your points of thinking in OO. But honestly speaking,
OO itself is not that perfect.

【在 g*****g 的大作中提到】
: You miss the point, hack the class to access its private memeber is
: against OO. Many languages do not even have a way to do it at all.
: So instead of doing it that way, check which part is wrong, if the parents
: class is having a bad design, can you change it. And if you cannot,
: can you rewrite/wrap it. And more frequently, do you really need to access
: a private field at all?
: Doing it this way, you are encouraged to think OO, instead of going
: quick and dirty.

1 (共1页)
进入Programming版参与讨论
相关主题
goodbug vs neverlearn请教一个C++问题
有没有根据 model 类自动生成 html form的工具?为什么java要用setter和getter
Who can help explain setter and getter?大家都用那跟手指敲 - + = [ ] ;
scala的ide版本号已经上3冲4了老年工程师转行遇到下马威
C++中怎么传递std::hex这样的参数啊为什么在overloading中,friend <<不能读取private值呢?
学scala发现一个有趣现象Why should i include .cpp instead of .h
C++子类中调用父类指针的问题请问关于overloading <<
C++: friend function能否通过父类指针来复制子类对象?
相关话题的讨论汇总
话题: operator话题: class话题: oo话题: private话题: base