C++ 简单多态性

C++ 简单多态性,c++,polymorphism,C++,Polymorphism,所以,很明显我做错了什么 #include <iostream> using namespace std; class thing1 { public: void thingTest() { cout << "I AM THING 1\n"; } }; class thing2: public thing1 { public: void thingTest() { cout << "I

所以,很明显我做错了什么

#include <iostream>
using namespace std;

class thing1
{
public:
    void thingTest()
    {
        cout << "I AM THING 1\n";
    }
};

class thing2: public thing1
{
public:
    void thingTest()
    {
        cout << "I AM THING 2\n";
    }
};

void DoStuff( thing1 temp )
{
    temp.thingTest();
}


void main()
{
    DoStuff( thing2() );
}
#包括
使用名称空间std;
课堂教学1
{
公众:
空物测试()
{

cout您正在按值获取它们,这是对它们的切片。您必须使用引用或指针(在本例中,引用常量或右值引用,因为它们是右值)

请注意
virtual
关键字的使用。它仅在基类中的方法声明中需要-默认情况下,继承的
virtual
成员是
virtual


上的固定版本。

您正在按值获取它们,这是对它们进行切片。您必须使用引用或指针(引用常量或右值引用,在本例中,它们是右值)

请注意
virtual
关键字的使用。它仅在基类中的方法声明中需要-默认情况下,继承的
virtual
成员是
virtual


上的固定版本。

virtual
是您的朋友

#include <iostream>
using namespace std;

class thing1
{
public:
    // Make this virtual, so that when called by pointer or 
    // by reference, the most derived version is called.
    virtual void thingTest()
    {
        cout << "I AM THING 1\n";
    }
};

class thing2: public thing1
{
public:
    // This is virtual by default, but it's best to be explicit,
    // especially when the next guy derives from thing2.
    virtual void thingTest()
    {
        cout << "I AM THING 2\n";
    }
};

// Pass by value to prevent object slicing
void DoStuff(thing1& temp )
{
    temp.thingTest();
}

// main always returns int
int main()
{
    thing2 x; // Create a thing2
    DoStuff(x); // Pass the rvalue
}
#包括
使用名称空间std;
课堂教学1
{
公众:
//将此设置为虚拟,以便在通过指针或
//通过引用,最派生的版本称为。
虚空thingTest()
{

cout
virtual
是你的朋友

#include <iostream>
using namespace std;

class thing1
{
public:
    // Make this virtual, so that when called by pointer or 
    // by reference, the most derived version is called.
    virtual void thingTest()
    {
        cout << "I AM THING 1\n";
    }
};

class thing2: public thing1
{
public:
    // This is virtual by default, but it's best to be explicit,
    // especially when the next guy derives from thing2.
    virtual void thingTest()
    {
        cout << "I AM THING 2\n";
    }
};

// Pass by value to prevent object slicing
void DoStuff(thing1& temp )
{
    temp.thingTest();
}

// main always returns int
int main()
{
    thing2 x; // Create a thing2
    DoStuff(x); // Pass the rvalue
}
#包括
使用名称空间std;
课堂教学1
{
公众:
//将此设置为虚拟,以便在通过指针或
//通过引用,最派生的版本称为。
虚空thingTest()
{

CUT

你应该阅读C++中多态性的语法。

首先,
thingTest
必须在基类中声明为
virtual
,然后编译器将执行动态查找,以在引用或指针上调用正确的运行时函数。在当前代码中,编译器已在编译时将函数固定为
thing1::thingTest
,因为它没有标记d是虚拟的


第二,您将一个派生类按值传递给基类类型。这会导致对象切片,即派生类的成员被切断。您需要通过引用传递对象或通过指向“代码> DoStuff < /COD>”的指针。

< P>您应该阅读C++中多态性的语法。

首先,
thingTest
必须在基类中声明为
virtual
,然后编译器将执行动态查找,以在引用或指针上调用正确的运行时函数。在当前代码中,编译器已在编译时将函数固定为
thing1::thingTest
,因为它没有标记d是虚拟的


第二,您正在通过值将派生类传递给基类类型。这可能会导致对象切片,即,派生类的成员被切断。您需要通过引用或通过指向
DoStuff

@sehe:Sod的指针来传递对象。我在另一个屏幕上忙着根据您的请求合并^pmr。现在我有了e使其与队列一起工作是一个新问题。@sehe:Sod。我在另一个屏幕上忙着^^@pmr根据您的请求合并。现在我有一个新问题,使其与队列一起工作。+1用于将虚拟放在派生方法上,尽管不必要。我认为这样更清楚。+1用于将虚拟放在派生方法上,尽管不需要我想这样更清楚。