通过基类引用分配 [英] Assignment through base class reference

查看:75
本文介绍了通过基类引用分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应该采取什么措施来避免这类事情?


等级基础

{

};


class Derived1:public Base

{

private:

int i,j,k; < br $>
};


类Derived2:公共基地

{

私人:

double l,m,n;

};


void BaseAssign(Base& lhs,Base& rhs)

{

lhs = rhs;

}

int main(int argc,char * argv [])

{

Derived1 d1;

Derived2 d2;


BaseAssign(d1,d2);

}


//结束代码片段


我能看到防止这种事情的唯一方法是

声明赋值运算符和非叶子类b / b类的复制构造函数受保护,并提供克隆/创建方法而不是

让运算符/构造函数为使用。


一般人只是不担心这种事情?我是

对这样的事情绝对偏执。

What measures should be taken to avoid this sort of thing?

class Base
{
};

class Derived1 : public Base
{
private:
int i, j, k;
};

class Derived2 : public Base
{
private:
double l, m, n;
};

void BaseAssign(Base& lhs, Base& rhs)
{
lhs = rhs;
}

int main(int argc, char *argv[])
{
Derived1 d1;
Derived2 d2;

BaseAssign(d1, d2);
}

// End code snippet

The only way I can see around preventing this sort of thing is
declaring the assignment operators and copy constructors of non-leaf
classes protected, and providing clone/create methods instead of
letting the operators/constructors be used.

Do people generally just not worry about this sort of thing? I''m
absolutely paranoid about stuff like this.

推荐答案

一个************** @ googlemail.com 写道:
应该采取什么措施来避免这类事情?


为什么?

类基地
{
};

类Derived1:public Base
{
私人:
int i,j,k;
};

类Derived2:public Base
{
私人:
双l,m,n;
};

void BaseAssign(Base& lhs,Base& rhs)
{
lhs = rhs;
}
int main(int argc,char * argv [])
{
Derived1 d1;
Derived2 d2;
BaseAssign(d1,d2);
}
//结束代码片段

我可以看到防止这类事情的唯一方法是


为什么要防止这种事情?你有任何问题吗?请

详细说明。

声明赋值操作符和非叶子类的复制构造函数受保护,并提供克隆/创建方法而不是
letting使用操作员/构造者。

人们通常不担心这类事情吗?我对这样的事情非常偏执。
What measures should be taken to avoid this sort of thing?
WHY?
class Base
{
};

class Derived1 : public Base
{
private:
int i, j, k;
};

class Derived2 : public Base
{
private:
double l, m, n;
};

void BaseAssign(Base& lhs, Base& rhs)
{
lhs = rhs;
}

int main(int argc, char *argv[])
{
Derived1 d1;
Derived2 d2;

BaseAssign(d1, d2);
}

// End code snippet

The only way I can see around preventing this sort of thing is
Why prevent this sort of thing? Do you experience any problem? Please
elaborate.
declaring the assignment operators and copy constructors of non-leaf
classes protected, and providing clone/create methods instead of
letting the operators/constructors be used.

Do people generally just not worry about this sort of thing? I''m
absolutely paranoid about stuff like this.




好​​吧,你能分享一下吗?也许我需要变得偏执,因为

很好......


V



Well, could you share? Maybe I need to become paranoid about it as
well...

V




Victor Bazarov写道:

Victor Bazarov wrote:
** ************@googlemail.com 写道:
应采取哪些措施来避免此类事情?
What measures should be taken to avoid this sort of thing?


为什么?



WHY?

class Base
{
};

类Derived1:public Base
{<私人:
int i,j,k;
};

类Derived2:public Base
{
私人:
double l,m,n;
};

void BaseAssign(Base& lhs,Base& rhs)
{
lhs = rhs;
}

int main(int argc,char * argv [])
{
Derived1 d1;
Derived2 d2;

BaseAssign(d1 ,d2);
}
//结束代码片段

我能看到的唯一方法可以防止这种情况发生有点像
class Base
{
};

class Derived1 : public Base
{
private:
int i, j, k;
};

class Derived2 : public Base
{
private:
double l, m, n;
};

void BaseAssign(Base& lhs, Base& rhs)
{
lhs = rhs;
}

int main(int argc, char *argv[])
{
Derived1 d1;
Derived2 d2;

BaseAssign(d1, d2);
}

// End code snippet

The only way I can see around preventing this sort of thing is



为什么要阻止这种事情?你有任何问题吗?请详细说明。



Why prevent this sort of thing? Do you experience any problem? Please
elaborate.

声明赋值操作符和非叶子类的复制构造函数受保护,并提供克隆/创建方法而不是
letting使用操作员/构造者。

人们通常不担心这类事情吗?我对这样的事情非常偏执。
declaring the assignment operators and copy constructors of non-leaf
classes protected, and providing clone/create methods instead of
letting the operators/constructors be used.

Do people generally just not worry about this sort of thing? I''m
absolutely paranoid about stuff like this.



嗯,你能分享一下吗?也许我需要变得偏执,因为它很好......

V



Well, could you share? Maybe I need to become paranoid about it as
well...

V




这些课程必须要切片......我的意思是,Derived2如何分配给Derived1?



The classes must be getting sliced... I mean, how can a Derived2 be
assigned to a Derived1?


************** @ googlemail.com 写道:
Victor Bazarov写道:
Victor Bazarov wrote:
一个************** @ googlemail.com写道:
an**************@googlemail.com wrote:
应该采取什么措施来避免这类事情?
What measures should be taken to avoid this sort of thing?



为什么?



WHY?

class Base
{
};

类Derived1:public Base
{
私人:
int i,j,k;
};

类Derived2:公共基地
{
私人:
双l,m,n;
};

void BaseAssign(Base& amp; ; lhs,Base& rhs)
{
lhs = rhs;
}
int main(int argc,char * argv [])
{
Derived1 d1;
Derived2 d2;

BaseAssign(d1,d2);
}

//结束代码片段

我能看到周围防止这种事情的唯一方法是
class Base
{
};

class Derived1 : public Base
{
private:
int i, j, k;
};

class Derived2 : public Base
{
private:
double l, m, n;
};

void BaseAssign(Base& lhs, Base& rhs)
{
lhs = rhs;
}

int main(int argc, char *argv[])
{
Derived1 d1;
Derived2 d2;

BaseAssign(d1, d2);
}

// End code snippet

The only way I can see around preventing this sort of thing is



为什么要阻止这类事情?你有任何问题吗?请详细说明。



Why prevent this sort of thing? Do you experience any problem? Please
elaborate.

声明赋值运算符和非叶子类的复制构造函数受保护,并提供克隆/创建方法而不是<让操作员/施工人员使用。

人们通常不担心这类事情吗?我对这样的事情非常偏执。
declaring the assignment operators and copy constructors of non-leaf
classes protected, and providing clone/create methods instead of
letting the operators/constructors be used.

Do people generally just not worry about this sort of thing? I''m
absolutely paranoid about stuff like this.



嗯,你能分享一下吗?也许我需要变得偏执,因为它很好......

V



Well, could you share? Maybe I need to become paranoid about it as
well...

V



课程必须要切片...我的意思是,如何将Derived2分配给Derived1?


The classes must be getting sliced... I mean, how can a Derived2 be
assigned to a Derived1?




你在哪里得到关于切片的想法? ''d1'的Base子对象是

简单地将_the_same_作为''d2'的Base子对象。至少那个'

对我来说意味着什么* *语义*。没有更多,也没有更少。


无论什么''d1''_above_and_beyond_''Base''子对象,都保持不变。

无论'd2' ''_above_and_beyond_''Base''子对象,在该操作中不用于

。我想以下对你来说更可怕:


class Derived3:public Derived2 {};

class Derived4:public Derived3 {std :: string name; };


...

Derived4 d4;

BaseAssign(d1,d4);

不应该。放松。深吸一口气。没有*坏*正在发生

在这里。如果您的问题域禁止(无论出于何种原因,您确实没有对问题域进行任何说明),您可以通过

禁用它来使''Base'受保护基类,但LSP无法应用...

也许这就是你想要的......然后说出来。


V



Where did you get that idea, about slicing? The Base subobject of ''d1'' is
simply made _the_same_ as the Base subobject of ''d2''. At least that''s
what it means [to me] *semantically*. Nothing more and nothing less.

Whatever ''d1'' has _above_and_beyond_ its ''Base'' subobject, is kept intact.
Whatever ''d2'' has _above_and_beyond_ its ''Base'' subobject, is not used at
all in that operation. I suppose the following is even scarier to you:

class Derived3 : public Derived2 {};
class Derived4 : public Derived3 { std::string name; };

...
Derived4 d4;
BaseAssign(d1, d4);

It shouldn''t be. Relax. Take a deep breath. Nothing *bad* is happening
here. If your problem domain prohibits that (for whatever reason, you did
not say anything about the problem domain), you _could_ disable it by
making ''Base'' protected base class, but then LSP cannot be applied...
Maybe that''s what you want... Speak up, then.

V


这篇关于通过基类引用分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆