困惑......这里发生了什么 [英] Confused .. What is happenning here

查看:87
本文介绍了困惑......这里发生了什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我是C ++的新手,只是阅读有关多态的内容。我试着用
写一个非常简单的程序。然后一个奇怪的想法进入我的脑海。

而不是在多态中使用指针,我使用了一个引用。并且

他们两个都打印了相同的东西。

我想知道引擎盖下发生了什么。

#include< iostream> ;


使用命名空间std;


班级基础

{

public:

虚拟空白打印()

{

cout<< 在基类中<< endl;

}

};


派生类:公共基础

{

public:

virtual void print()

{

cout<< ;In Derived Class<< endl;

}


};


int main ()

{

//使用指针

base * b = new derived();

派生d ;

b-> print();


//使用参考

base& c = d;

c.print();


返回0;

}


问候,

vb

解决方案

3月28日,12:25,vb.h ... @ gmail .com写道:


大家好,

我是C ++的新手,只是阅读有关多态的内容。我试着用
写一个非常简单的程序。然后一个奇怪的想法进入我的脑海。

而不是在多态中使用指针,我使用了一个引用。并且

他们两个都打印了相同的东西。

我想知道幕后发生了什么。



多态性。多态性与指针或引用无关

(但必须使用它们才能使其工作)。并且正如你所看到的那样只有

以及带有指针的引用。因此,当您在指针上调用print()时,就会发生相同的

事件,就像

参考一样。


-

Erik Wikstr?m


3月28日凌晨2:25,vb.h ... @ gmail .com写道:


大家好,

我是C ++的新手,只是阅读有关多态的内容。我试着用
写一个非常简单的程序。然后一个奇怪的想法进入我的脑海。

而不是在多态中使用指针,我使用了一个引用。并且

他们两个都打印了相同的东西。

我想知道引擎盖下发生了什么。


#包括< iostream>


使用命名空间std;


类别基础

{

public:

虚拟空白打印()

{

cout<< 在基类中<< endl;

}


};


派生类:公共基地

{

public:

虚拟空白打印()

{

cout<<" In Derived Class"<< endl;

}


};


int main()

{

//使用指针

base * b = new derived();

派生d;

b-> print();


//使用参考

base& c = d;

c.print();


返回0;


}


问候,

vb



你的意思是内存泄漏?

3月28日下午3:48,Mathematician < mathemtician1234567 ... @ yahoo.com>

写道:


3月28日凌晨2:25,vb.h。 .. @ gmail.com写道:


大家好,

我是C ++的新手,只是阅读有关多态的内容。我试着用
写一个非常简单的程序。然后一个奇怪的想法进入我的脑海。

而不是在多态中使用指针,我使用了一个引用。并且

他们两个都打印了相同的东西。

我想知道幕后发生了什么。


#include< iostream>


using namespace std;


class base

{

public:

virtual void print()

{

cout<< 在基类中<< endl;

}


};


类派生:公共基础

{

public:

virtual void print()

{

cout<<" In Derived Class"<< endl;

}


};


int main()

{

//使用指针

base * b = new derived();

派生d;

b-> print();


//使用参考

base& c = d;

c.print ();


返回0;


}


问候,

vb



你的意思是内存泄漏?



好​​..禁止内存泄漏.. :-)


Hi all,
I am new to C++ and was just reading about polymorphism. I tried to
write a very simple program. Then a curious thought came into my mind.
And instead of using pointer in polymorphism, i used a reference. And
both of them printed the same thing.
I want to know what is going on under the hood.
#include <iostream>

using namespace std;

class base
{
public:
virtual void print()
{
cout<< "In Base Class"<<endl;
}
};

class derived : public base
{
public:
virtual void print()
{
cout<<"In Derived Class"<<endl;
}

};

int main()
{
//using pointer
base *b = new derived();
derived d;
b->print();

//using reference
base &c = d;
c.print();

return 0;
}

Regards,
vb

解决方案

On 28 Mar, 12:25, vb.h...@gmail.com wrote:

Hi all,
I am new to C++ and was just reading about polymorphism. I tried to
write a very simple program. Then a curious thought came into my mind.
And instead of using pointer in polymorphism, i used a reference. And
both of them printed the same thing.
I want to know what is going on under the hood.

Polymorphism. Polymorphism is independent of pointers or references
(but you must use them to make it work). And as you see it works just
as well with references as with pointers. So under the hood the same
thing happens when you call print() on the pointer as on the
reference.

--
Erik Wikstr?m


On Mar 28, 2:25 am, vb.h...@gmail.com wrote:

Hi all,
I am new to C++ and was just reading about polymorphism. I tried to
write a very simple program. Then a curious thought came into my mind.
And instead of using pointer in polymorphism, i used a reference. And
both of them printed the same thing.
I want to know what is going on under the hood.

#include <iostream>

using namespace std;

class base
{
public:
virtual void print()
{
cout<< "In Base Class"<<endl;
}

};

class derived : public base
{
public:
virtual void print()
{
cout<<"In Derived Class"<<endl;
}

};

int main()
{
//using pointer
base *b = new derived();
derived d;
b->print();

//using reference
base &c = d;
c.print();

return 0;

}

Regards,
vb

You mean the memory leak ?


On Mar 28, 3:48 pm, "Mathematician" <mathemtician1234567...@yahoo.com>
wrote:

On Mar 28, 2:25 am, vb.h...@gmail.com wrote:

Hi all,
I am new to C++ and was just reading about polymorphism. I tried to
write a very simple program. Then a curious thought came into my mind.
And instead of using pointer in polymorphism, i used a reference. And
both of them printed the same thing.
I want to know what is going on under the hood.

#include <iostream>

using namespace std;

class base
{
public:
virtual void print()
{
cout<< "In Base Class"<<endl;
}

};

class derived : public base
{
public:
virtual void print()
{
cout<<"In Derived Class"<<endl;
}

};

int main()
{
//using pointer
base *b = new derived();
derived d;
b->print();

//using reference
base &c = d;
c.print();

return 0;

}

Regards,
vb


You mean the memory leak ?

Ok .. Barring the memory leak .. :-)


这篇关于困惑......这里发生了什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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