课程和缺点参考... [英] classes and cons reference...

查看:52
本文介绍了课程和缺点参考...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在处理,可能是轻松的问题,不过我不知道该怎么做,以及我的思维方式是正确的..

任务是实现一个ListOfElements类的对象列表

然后实现一个将颠倒
$的顺序的函数b $ b列表中的元素,但函数应如下所示:

ListOfElements reverse(const ListOfElements&)


至于我知道我应该创建一个这些对象的列表,这是我知道怎么做的。我对反转功能有困难。什么

它真的意味着它作为参数需要ListOfElements& ?

这是对第一个对象的引用,即。指向第一个

对象的指针?


说我们有以下内容:


class ListOfElements
{

private:

int data;

ListOfElements * next; //指向

列表中下一个元素的指针

public:

ListOfElements();

~ListOfElements ();

addNewElement(int new_data);

}


ListOfElements * head = NULL;指向第一个元素的指针


我现在如何实现反向?逆转的算法不是问题,我不知道如何处理常量引用和

返回一个对象...

解决方案

1337-chixor;)写道:


大家好,

我正在处理,可能是一个简单的问题,但是我不确定要做什么,以及我的思维方式是否正确..

任务是实现类ListOfElements

的对象列表,然后实现一个函数,该函数将颠倒列表中

元素的顺序,但函数应如下所示:


ListOfElements reverse(const ListOfElements&)



为什么不使用已排序的容器( std:map或std :: set)并使用反向

迭代器迭代它们,或者将它们复制到另一个容器中?


-

Ian Collins。


< BLOCKQUOTE>" 1337-chixor;)" < ne ********* @ gmail.com写信息

新闻:11 ********************* *@o5g2000hsb.googlegro ups.com ...


大家好,

我正在处理可能很容易的问题,但是我不确定要做什么以及我的思维方式是否正确..

任务是实现类ListOfElements的对象列表

然后实现一个函数,它将颠倒列表中

元素的顺序,但函数应如下所示:


ListOfElements reverse(const ListOfElements&)



您的参数是对ListOfElements的引用,它是常量。

表示您不要更改传入的列表。你需要创建

a副本。


听起来像一个简单的方法就是创建一个ListOfObjects

在这个函数里面。然后从传入的列表中开始插入

向后。 I.E.从后面开始阅读并插入到最后。然后

返回你创建的ListOfObjects。


很可能你可以使用标准算法。复制什么的。

我还没用它们,所以我只想用一个for循环。


这个问题听起来像是家庭作业(不是确定它是不是)

所以我不愿意显示代码。


据我所知,我应该创建一个这些对象的列表,

我知道该怎么做。我对反转功能有困难。什么

它真的意味着它作为参数需要ListOfElements& ?

这是对第一个对象的引用,即。指向第一个

对象的指针?


说我们有以下内容:


class ListOfElements
{

private:

int data;

ListOfElements * next; //指向

列表中下一个元素的指针

public:

ListOfElements();

~ListOfElements ();

addNewElement(int new_data);

}


ListOfElements * head = NULL;指向第一个元素的指针


我现在如何实现反向?逆转的算法不是问题,我不知道如何处理常量引用和

返回一个对象...



常量引用意味着你可以将参数视为实例,你不能改变它(或调用非常量的方法)我不认为的课程。


我看到的问题是返回列表的头部必须是

在堆栈上分配,其余的节点分配在

堆上,如果析构函数调用delete,那么分配就是

不可能。按值返回将调用默认的副本

构造函数,因此两个列表头将指向

元素的相同尾部列表,然后当本地副本退出时范围它将摧毁

所有。


Hi guys,
I''m dealing with, probably, easy problem, however I''m not sure what to
do and whether my way of thinking is correct..
The task is to implement a list of object of a class ListOfElements
and then implement a function that will reverse the order of the
elements on the list, but the function should look like this:

ListOfElements reverse (const ListOfElements&)

As far as I understand I should create a list of these objects, which
I know how to do. I have difficulties with the function reverse. What
does it realy mean that as parameter it takes ListOfElements& ? Is
this a reference to the first object, ie. a pointer to the first
object?

say we have the following:

class ListOfElements
{
private:
int data;
ListOfElements * next; //pointer to the next element on the
list
public:
ListOfElements();
~ListOfElements();
addNewElement (int new_data);
}

ListOfElements * head = NULL; pointer to the first element

How can I now implement reverse? The algorithm of the reversing is not
the problem, I don''t know what to do with the constant reference and
returning of an object...

解决方案

1337-chixor;) wrote:

Hi guys,
I''m dealing with, probably, easy problem, however I''m not sure what to
do and whether my way of thinking is correct..
The task is to implement a list of object of a class ListOfElements
and then implement a function that will reverse the order of the
elements on the list, but the function should look like this:

ListOfElements reverse (const ListOfElements&)

Why not use a sorted container (std:map or std::set) and use a reverse
iterator to iterate through them, or copy them to another container?

--
Ian Collins.


"1337-chixor;)" <ne*********@gmail.comwrote in message
news:11**********************@o5g2000hsb.googlegro ups.com...

Hi guys,
I''m dealing with, probably, easy problem, however I''m not sure what to
do and whether my way of thinking is correct..
The task is to implement a list of object of a class ListOfElements
and then implement a function that will reverse the order of the
elements on the list, but the function should look like this:

ListOfElements reverse (const ListOfElements&)

Your parameter is a reference to a ListOfElements which is constant. Which
means you are not to change the passed in list. You''ll need to then create
a copy.

It sounds to me like an easy way would simply be to create a ListOfObjects
inside this function. Then start inserting from the passed in list
backwards. I.E. Start reading from the back and insert to the end. Then
return the ListOfObjects you created.

There is most likely a standard algorithm you can use. Copy or something.
I don''t use them yet, so I would just use a for loop.

This question sounds suspiciously like homework (not sure if it is or not)
so I''m reluctant to show code.

As far as I understand I should create a list of these objects, which
I know how to do. I have difficulties with the function reverse. What
does it realy mean that as parameter it takes ListOfElements& ? Is
this a reference to the first object, ie. a pointer to the first
object?

say we have the following:

class ListOfElements
{
private:
int data;
ListOfElements * next; //pointer to the next element on the
list
public:
ListOfElements();
~ListOfElements();
addNewElement (int new_data);
}

ListOfElements * head = NULL; pointer to the first element

How can I now implement reverse? The algorithm of the reversing is not
the problem, I don''t know what to do with the constant reference and
returning of an object...

The constant refernece means you can treat the paramter as the instance, you
just can''t change it (or call non-const methods of the class I don''t think).


The problem I see is that the head of the returned list must be
allocated on the stack, with the rest of the nodes allocated on the
heap, and if the destructor calls delete next then the assignment is
impossible. The return by value will invoke the default copy
constructor, so both list heads will point to the same tail list of
elements, and then as the local copy goes out of scope it will destroy
them all.


这篇关于课程和缺点参考...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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