English 中文(简体)
参考通过母体反对
原标题:Passing parent object by reference

我在图书馆里有这一守则:

class Parent
{
    //some data and functions
};

void myfunc(Parent& ref);

在我的申请中,我还要这样做:

class Child : public Parent
{
   // some other data and functions
   void dostuff()
   {
       myfunc(*this);
   }
};

Is it safe to pass *this? (no slicing, no copying, ...) Is it better to call myfunc like this:

myfunc( * ((Parent*)this) )

请注意,我没有掌握我方言中发生的情况,在某些情况下,我甚至不知道那里发生的情况。

我多次使用过亲人的双人,我曾经使用过,但以前从未使用过亲人的参照。

最佳回答

www.un.org/chinese/ga/president

This will not copy the object. It will pass a reference to the original object. Furthermore, it will not slice the object. The reference will be of type Base&, but the object to which it refers will be unchanged.

就在你所知,如果你在<代码>Base&上使用多形态(例如virtual)方法,那么多形态仍将是正确的,你会做些什么。 换言之:

Base& b = *derived;
b.SomeVirtualFoo();

...will have the same effect as:

Base* b = derived;
b->SomeVirtualFoo();
问题回答

第1版不正确:

 myfunc(*this);

第二版很可能在该案中发挥作用,但我并不相信,如果涉及多重继承,它将在所有案件中发挥作用(正如你正在使用一种C-Style )。 我必须删除我的标准,看看C-Style的准确行为。

如果采用同一技术,如果你改用参考书,该方法就会发挥作用。

Update:

现在,我读过标准一,C-Cast将做正确的事(作为静态的预测和预测;并且可以用来从儿童到父母的等级等级划分)。

5.4 Explicit type conversion (cast notation)

4 The conversions performed by
— a const_cast (5.2.11),
— a static_cast (5.2.9),
— a static_cast followed by a const_cast,
— a reinterpret_cast (5.2.10), or
— a reinterpret_cast followed by a const_cast,

可以通过投放明确类型转换的标签来实现。 同样的语种限制和同种异构体也适用,但以下情况下在静态播种时,即使基类无法进入,转换也是有效的:

— a pointer to an object of derived class type or an lvalue or rvalue of derived class type may be explicitly converted to a pointer or reference to an unambiguous base class type, respectively;
— a pointer to member of derived class type may be explicitly converted to a pointer to member of an unambiguous non-virtual base class type;
— a pointer to an object of an unambiguous non-virtual base class type, a glvalue of an unambiguous non-virtual base class type, or a pointer to member of an unambiguous non-virtual base class type may be explicitly converted to a pointer, a reference, or a pointer to member of a derived class type, respectively.

如果能够以上述不止一种方式解释转换,则使用清单中首先出现的解释,即使由于这种解释而产生的解释不知情。 如果转换可以不止一种方式解释为静态预测,然后是星体预测,转换就不知情。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签