作为函数参数的类数组...多态性问题。 [英] Array of classes as a function parameter...polymorphism question.

查看:70
本文介绍了作为函数参数的类数组...多态性问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Base的类。这个类有一个受保护的成员

变量" m_base"可以使用公共成员检索

function" GetBaseMember"。 " m_base"被初始化为1。并且永远不会改变



我有另一个类,它是Base的子类。名为

" Derived"的课程。这个派生类有一个名为

" m_derived"的成员变量。 " m_derived"被初始化为2。并且永远不会改变。


我传递了一个Base数组。类作为函数的参数。

此数组中的单个项可能是也可能不是

" Base"的子类。类。在这个函数中,成员函数

" GetBaseMember"被称为,并且m_base的值被称为m_base。显示为

屏幕。


直观地说,输出应始终为1,但不幸的是它是

不是。输出在1和1之间交替。和2。我不明白为什么

这是。我怎么能通过这个子类呢?类数组到

函数并检索预期的基类成员变量?什么

概念我在这里错过了吗?


#include< iostream>

使用命名空间std;


等级基础

{

受保护:

int m_base;


public:

Base():m_base(1){}

int GetBaseMember(){return m_base; }

};


类派生:公共基地

{

受保护:

int m_derived;


public:

Derived():Base(),m_derived(2){}

};


void Foo(int cItems,Base b [])

{

int i = 0;

for(i = 0; i< cItems; i ++)

{

//我希望输出始终为1,

//但它在1和2之间交替

cout<< " m_base =" << b [i] .GetBaseMember()<<结束;

}

}


int main()

{

const int NUM_ITEMS = 4;

派生d [NUM_ITEMS];


Foo(NUM_ITEMS,d);


返回0;

}

I have a class called "Base". This class has a protected member
variable "m_base" which can be retrieved using the public member
function "GetBaseMember". "m_base" is initialized to "1" and is never
changed.

I have another class which is a subclass of the "Base" class called
"Derived". This derived class has a member variable called
"m_derived". "m_derived" is initialized to "2" and is never changed.

I pass an array of "Base" classes as a parameter to a function.The
individual items in this array may or may not be subclasses of the
"Base" class. Within this function, the member function
"GetBaseMember" is called, and the value of "m_base" is displayed to
the screen.

Intuitively, the output should always be "1", but unfortunately it''s
not. The output alternates between "1" and "2". I don''t understand why
this is. How else can I pass this "subclassable" class array to a
function and retrieve the expected base class member variable? What
concept am I missing here?

#include <iostream>
using namespace std;

class Base
{
protected:
int m_base;

public:
Base() : m_base( 1 ){}
int GetBaseMember() { return m_base; }
};

class Derived : public Base
{
protected:
int m_derived;

public:
Derived() : Base(), m_derived( 2 ){}
};

void Foo( int cItems, Base b[] )
{
int i = 0;
for ( i = 0; i < cItems; i++ )
{
// I want the output to always be 1,
// but it alternates between 1 and 2
cout << "m_base = " << b[i].GetBaseMember() << endl;
}
}

int main()
{
const int NUM_ITEMS = 4;
Derived d[NUM_ITEMS];

Foo( NUM_ITEMS, d );

return 0;
}

推荐答案

Jack写道:
Jack wrote:

我有一个名为Base的类。这个类有一个受保护的成员

变量" m_base"可以使用公共成员检索

function" GetBaseMember"。 " m_base"被初始化为1。并且永远不会改变



我有另一个类,它是Base的子类。名为

" Derived"的课程。这个派生类有一个名为

" m_derived"的成员变量。 " m_derived"被初始化为2。并且永远不会改变。


我传递了一个Base数组。类作为函数的参数。
I have a class called "Base". This class has a protected member
variable "m_base" which can be retrieved using the public member
function "GetBaseMember". "m_base" is initialized to "1" and is never
changed.

I have another class which is a subclass of the "Base" class called
"Derived". This derived class has a member variable called
"m_derived". "m_derived" is initialized to "2" and is never changed.

I pass an array of "Base" classes as a parameter to a function.



实际上,你不是。你的函数*需要*一个Base对象数组。

你*传递*它是一个* Derived * s数组。

Actually, you don''t. Your function *expects* an array of Base objects.
You *pass* it an array of *Derived*s.




此数组中的单个项可能是也可能不是

" Base"的子类。类。
The
individual items in this array may or may not be subclasses of the
"Base" class.



如果它是''Base''的数组,其中的项目总是*'Base'的对象。

它们都不是任何东西的子对象。它们都是独立的

对象,基本上。

If it''s an array of ''Base'', items in it are *always* objects of ''Base''.
None of them can be subobjects of anything. They are all stand-alone
objects, essentially.


在这个函数中,成员函数

" ; GetBaseMember"被称为,并且m_base的值被称为m_base。显示为

屏幕。


直观地说,输出应始终为1,但不幸的是它是

不是。
Within this function, the member function
"GetBaseMember" is called, and the value of "m_base" is displayed to
the screen.

Intuitively, the output should always be "1", but unfortunately it''s
not.



程序的行为未定义。您传递了一个

派生对象的数组,其中包含一个Base数组。两者之间没有

转换。并且由于您将函数声明为接收*'指针*到''Base'',因此编译器不会抱怨。

索引指针时会发生未定义的行为除了0之外的任何

表达式。

The behaviour of your program is undefined. You pass an array of
Derived objects where an array of Base is expected. There is no
conversion between the two. And since you declare your function as
receiving a *pointer* to ''Base'', the compiler does not complain.
The undefined behaviour occurs when you index that pointer with any
expression other than 0.


输出在1和1之间交替。和2。我不明白为什么

这是。
The output alternates between "1" and "2". I don''t understand why
this is.



原因并不重要。行为未定义;任何可能发生的事情都会发生。

The reason is immaterial. The behaviour is undefined; anything may
happen.


我还能通过这个子类⊂类数组到

函数并检索预期的基类成员变量?什么

概念我在这里不见了?
How else can I pass this "subclassable" class array to a
function and retrieve the expected base class member variable? What
concept am I missing here?



你想使用多态。可能是编译时间,通过

模板。你的''Foo''函数应定义为


模板<类BDvoid Foo(int cItems,BD b [])

{

//保持在里面就像你拥有它一样。

}

You want to use polymorphism. Probably compile-time one, through
templates. Your ''Foo'' function should be defined as

template<class BDvoid Foo(int cItems, BD b[])
{
// keep inside just like you have it.
}


>

#包括< iostream>

使用命名空间std;


class base

{

protected:

int m_base;


public:

Base():m_base(1){}

int GetBaseMember(){return m_base; }

};


类派生:公共基地

{

受保护:

int m_derived;


public:

Derived():Base(),m_derived(2){}

};


void Foo(int cItems,Base b [])

{

int i = 0;

for(i = 0; i< cItems; i ++)

{

//我希望输出始终为1,

//但它在1和2之间交替

cout<< " m_base =" << b [i] .GetBaseMember()<<结束;

}

}


int main()

{

const int NUM_ITEMS = 4;

派生d [NUM_ITEMS];


Foo(NUM_ITEMS,d);


返回0;

}
>
#include <iostream>
using namespace std;

class Base
{
protected:
int m_base;

public:
Base() : m_base( 1 ){}
int GetBaseMember() { return m_base; }
};

class Derived : public Base
{
protected:
int m_derived;

public:
Derived() : Base(), m_derived( 2 ){}
};

void Foo( int cItems, Base b[] )
{
int i = 0;
for ( i = 0; i < cItems; i++ )
{
// I want the output to always be 1,
// but it alternates between 1 and 2
cout << "m_base = " << b[i].GetBaseMember() << endl;
}
}

int main()
{
const int NUM_ITEMS = 4;
Derived d[NUM_ITEMS];

Foo( NUM_ITEMS, d );

return 0;
}



V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Victor Bazarov写道:
Victor Bazarov wrote:

Jack写道:
Jack wrote:

>我还能怎么办?通过这个子类类数组到
函数并检索预期的基类成员变量?我错过了什么概念?
>How else can I pass this "subclassable" class array to a
function and retrieve the expected base class member variable? What
concept am I missing here?



你想使用多态。可能是编译时间,通过

模板。你的''Foo''函数应定义为


模板<类BDvoid Foo(int cItems,BD b [])

{

//保持在里面,就像你拥有它一样。

}


You want to use polymorphism. Probably compile-time one, through
templates. Your ''Foo'' function should be defined as

template<class BDvoid Foo(int cItems, BD b[])
{
// keep inside just like you have it.
}



更好的是:


模板< typename它>

无效Foo(它开始,结束)

{

for(It i = begin; i!= end; ++ i)

cout<< " m_base =" << i-> GetBaseMember()<<结束;

}


int main()

{

const int NUM_ITEMS = 4;

派生d [NUM_ITEMS];


Foo(d,d + NUM_ITEMS);


返回0;

}


这允许除了数组之外还使用STL容器,或者

子范围。

Even better would be:

template <typename It>
void Foo(It begin,It end)
{
for (It i = begin; i != end; ++i)
cout << "m_base = " << i->GetBaseMember() << endl;
}

int main()
{
const int NUM_ITEMS = 4;
Derived d[NUM_ITEMS];

Foo(d,d + NUM_ITEMS);

return 0;
}

This allows the use of STL containers in addition to arrays, or
subranges of either.


Jack写道:
Jack wrote:

我有一个名为Base的类。这个类有一个受保护的成员

变量" m_base"可以使用公共成员检索

function" GetBaseMember"。 " m_base"被初始化为1。永远不会改变b $ b。
I have a class called "Base". This class has a protected member
variable "m_base" which can be retrieved using the public member
function "GetBaseMember". "m_base" is initialized to "1" and is never
changed.



[snip]

[snip]


class Base

{

受保护:

int m_base;


public:

Base():m_base(1){}

int GetBaseMember(){return m_base; }

};
class Base
{
protected:
int m_base;

public:
Base() : m_base( 1 ){}
int GetBaseMember() { return m_base; }
};



我知道这只是一个例子,但是如果m_base永远不会改变,为什么不用

声明它是const? GetBaseMember也是如此。


Nate

I know this is just an example, but if m_base never changes, why not
declare it const? Same goes for GetBaseMember.

Nate


这篇关于作为函数参数的类数组...多态性问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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