初学者问题 [英] Beginner questions

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

问题描述

大家好,


我有一些新手问题:


在函数声明中''const'的意思是什么

在参数列表中?它不会修改它的值?


void MemberFunction(const int x);

函数dec之前'const'的含义是什么。是什么意思?


const void MemberFunction(int x);


与''static'相同?

我的指针技巧并不完美,那么这些函数之间的区别是什么?


void MyFunction(int& x);

void MyFunction(int * x);

为什么ctors和dtors不能完全虚拟?


class MyClass {

虚拟MyClass()= 0;

虚拟~MyClass()= 0;

};


如何指定一个类应该从基类继承

公共和受保护成员?

使用朋友?


和迭代器有什么关系比较

到一个普通的for语句?

如:


for(vector< int> :: iterator等...)

vs.

for(int i = 0; i< myvector .size()等...)

谢谢。


- Pelle

解方案

Pelle Beckman写道:

大家好,

我有几个新手问题:

在函数声明中有什么参数列表中的常量是什么意思?它不会修改值吗?

void MemberFunction(const int x);


是的,但在这个例子中它没有意义 - 参数上的''const''

有意义的指针或参考:


void MemberFunction(const int& x);

void MemberFunction(const int * x);


在这些情况下,它不是真正的参数本身,函数

承诺它不会修改,而是指向/指向的对象。


因为参数(除非它是参考)总是一些

值的副本,改变它并不是真正的意义。

什么在函数dec之前''const''。是什么意思?

const void MemberFunction(int x);


这意味着返回的是一个const(无论如何)。再一次,这个

的例子有一个小问题 - 因为一个带有void返回类型的函数

没有返回任何东西,你不能声明什么都没有这是'

作为常量返回。


类似于:


const int MemberFunction(int x );


确实有一定道理,虽然回报中的常量更常见,但是当你返回类似完整对象的东西时。

与静态相同?


不,甚至不相似。 const影响返回的对象类型

。在同一个地方,静态会影响

函数本身的类型 - 假设(顾名思义)这是一个

成员函数,它会使它成为静态成员函数。静态

成员函数几乎完全不同于普通成员

函数,因为它与

的特定实例无关对象。

我的指针技巧并不完美,那么这些函数之间的区别是什么?

void MyFunction(int& x);
void MyFunction(int * x);


第一个通过引用接收参数,而第二个接收

指针。这两个通常会完成类似的事情,但是参考

总是在整个生命周期中引用同一个对象,而

指针可以改变,因此它指向不同的对象at不同的

次(或者根本没有,如果你愿意的话)。

为什么ctors and dtors不能完全虚拟?

类MyClass {
虚拟MyClass()= 0;
虚拟~MyClass()= 0;
};


这是'纯虚拟',以防你关心。在任何情况下,如果你想要的话,dtor _can_是
纯虚拟。一个ctor不能是虚拟的 - 或者总是

虚拟,取决于你的观点,但无论如何,你不能

指定它完全虚拟化,没有它,纯粹的虚拟完全不在图片中。

如何指定一个类应该继承
公共和受保护的成员来自基类吗?
使用朋友?


无论何时进行继承,你都可以从基类中获得公共和受保护的

成员。

什么是'非常好的迭代器比较普通的声明?
如:

for(vector< int> :: iterator等...)
vs.
for(int i = 0; i< myvector.size()etc ...)




在这种特殊情况下,迭代器提供使用迭代器的主要原因是将它们传递给算法。

算法使用它们的原因是为了迭代迭代,所以

算法可以使用相同的代码来处理不同种类的
容器。从链接列表中的一个节点到下一个节点很多

不同于从一个数组中的一个元素到另一个元素,但是

迭代器允许你编写你的以相同的方式编写代码(或者使用
与各种其他容器一起工作)。至少在理论上,这个

可以更容易地将一个容器替换为另一个容器,尽管这有一些限制 - 迭代器提供了一个通用语法,但

并没有改变基本面,所以迭代器不会(例如)

让你随机访问那些被安排只允许串行的数据

访问。


-

后来,

Jerry。

宇宙是自己想象的虚构。


Jerry Coffin skrev:

Pelle Beckman写道:

大家好,

我有一些新手问题:

在函数声明中''const'是什么意思
在参数列表里面?它不会修改值吗?

void MemberFunction(const int x);



是的,但在这个例子中它没有意义 - - 参数上的''const''对于指针或引用是有意义的:

void MemberFunction(const int& x);
void MemberFunction(const int * x );

在这些情况下,它本身并不是函数
承诺它不会修改的参数本身,而是指向/引用的对象。

由于参数(除非它是参考)始终是某些值的副本,因此更改它并不是真正的意义。

函数dec之前'const'的含义是什么。是什么意思?

const void MemberFunction(int x);



这意味着返回的是一个const(无论如何)。再一次,这个例子有一个小问题 - 因为一个具有void返回类型的函数
并没有返回任何东西,你不能声明没有那个'
作为const返回。

类似于:

const int MemberFunction(int x);

确实有道理,虽然是一个const当你返回类似完整对象的东西时,返回更常见。

与''static'相同?


不,甚至不相似。 const影响返回的对象类型。在同一个地方,静态会影响
函数本身的类型 - 假设(顾名思义)这是一个
成员函数,它会使它成为一个静态成员函数。静态
成员函数几乎与普通成员函数完全不同,因为它与
对象的特定实例无关。

我的指针技巧并不完美,那么这些函数之间的区别是什么?

void MyFunction(int& x);
void MyFunction (int * x);



第一个通过引用接收参数,而第二个接收
一个指针。这两个通常会完成类似的事情,但是引用
总是在整个生命周期中引用同一个对象,而
指针可以改变,因此它指向不同的对象在不同的​​时间(或者什么都没有,如果你愿意的话。

为什么ctors and dtors不能完全虚拟?

class MyClass {
virtual MyClass()= 0;
virtual~MyClass()= 0;
};



那是''纯虚拟,以防你关心。在任何情况下,如果你想要的话,dtor _can_可以是纯虚拟的。 ctor不能是虚拟的 - 或者总是虚拟的,取决于你的观点,但无论如何,你根本不能将它指定为虚拟的,没有它,纯虚拟完全脱离了图片。

如何指定一个类应该继承公共和受保护的成员基类?
使用''friend''?



无论何时进行继承,都可以从基类派生公共成员和受保护成员。

迭代器与普通的声明相比有什么好处?
如:

for(vector< int> :: iterator etc ...)
for
for(int i = 0; i< myvector.size()etc ...)



在这种特殊情况下,迭代器几乎没有什么优势 - 使用迭代器的主要原因是将它们传递给算法。
算法使用的原因它们是为了迭代迭代,因此
算法可以使用相同的代码来处理不同类型的容器。从链接列表中的一个节点到下一个节点有很多不同于从一个数组中的一个元素到另一个元素,但是
迭代器允许您以相同的方式编写代码(或者与其他各种容器一起工作。至少在理论上,这可以更容易地将一个容器替换为另一个容器,尽管这有一些限制 - 迭代器提供了一个通用语法,但
不会改变基础知识,所以迭代器不会(例如)
让你随机访问那些只允许连续访问的数据。

- <后来,杰瑞。

宇宙是一种自己想象的虚构。




谢谢Jerry。


后续:


为什么我要返回一个const对象?

(如'const MyClass MyFunction ()")

这个用法有什么好的例子吗?


告诉

编译器有什么好处函数承诺不会修改值(如void Method(const int * x))?


如果有的话价值我不能修改 - 我不应该只是

确保我我什么时候写这个蠢货?


Pelle Beckman写道:


大家好,

我有几个新手问题:

在函数声明中参数列表中的'const'是什么意思?它不会修改值吗?

void MemberFunction(const int x);


它告诉编译器,这个函数不会修改x。

在你的特定例子中,这是没用的,因为它真的是

对任何人来说都没关系,如果这个函数改变了x。


但是考虑你有一个函数,它需要一个参考:

void foo(int& a);


现在你调用那个函数:

int main()

{

int x = 5;

foo(x);

}


和这里有一个问题:函数foo()是否会改变

x,或者不是吗?由于foo通过引用传递x,因此foo()

可以做到这一点。在这种情况下,你没有其他选择,然后假设

它确实。


现在,如果函数想要向调用者发出信号:不要担心,我会

不要触摸你给我的东西,它可以这样做:


void foo(const int& a);


现在函数声明清楚地说:我将通过引用获取该int,

但我不会在函数内部改变它。

什么在函数dec之前'const''。是什么意思?

const void MemberFunction(int x);


''const void''之类的东西不存在。


const int MemberFunction(int x);


那个const是返回类型的一部分:它说这个函数

返回一个int,并且这个int是常量(不能改变)

由来电者)。再说一遍:对于一个int来说这是没用的,但是


const char * Fnct();


表示函数返回一个指针。指向字符的指针。并且

这些字符是不变的。

和''静态'一样?


静态告诉我们关于函数本身的一些信息。 const

只是返回类型的一部分。

我的指针技巧并不完美,那么这些函数之间的区别是什么?

void MyFunction(int& x);
void MyFunction(int * x);


第一个获取引用,而第二个获取指针。

在这种情况下,您可以将引用视为某种''隐藏指针''。

一个存在但你无法使用的指针,编译器会处理它。

你想这样做才能获得更好的效果访问时函数内部的语法

x。

为什么ctors和dtors不能完全虚拟?

$ b $嗯。一个dtor可以。并且在多态的情况下,它通常具有



至于一个ctor:你如何创建一个类的对象,当

你现在不是编译时的班级类型吗?

class MyClass {
virtual MyClass()= 0;
virtual~MyClass()= 0;
};

如何指定一个类应该从基类继承公共和受保护的成员?
使用朋友?


一个类从其基类继承* all *成员。

唯一的问题是:派生类可以访问这些成员

或者不能。

对于迭代器而言,与普通的for语句相比有什么好处?
如:

for(vector< int> :: iterator etc ...)
for
for(int i = 0; i< myvector.size()etc ...)



迭代器以相同的方式处理所有stl容器

(差不多,有一些小例外,但在上面

情况下,如果容器是矢量,则无关紧要,

a map,deque,multimap,...)。因此,您可以在不更改源代码的情况下使用

类型的容器。

以防您选择的容器表现不佳

你想尝试另一个。

另外:这为一般算法打开了大门。那些算法

与迭代器一起工作,这对于这个

迭代器所属的容器来说并不重要。算法总是一样的。


-

Karl Heinz Buchegger
kb ****** @ gascad.at


Hi all,

I have a few newbie questions:

In function declaration what does a ''const'' mean
inside the parameter list ? That it won''t modify the value?

void MemberFunction(const int x);
What does ''const'' before a function dec. mean?

const void MemberFunction(int x);

The same as ''static''?
My pointer skills aren''t perfect, so what is
the differece between these functions?

void MyFunction(int& x);
void MyFunction(int* x);
Why can''t ctors and dtors be "totally virtual"?

class MyClass {
virtual MyClass() = 0;
virtual ~MyClass() = 0;
};

How do I specify that one class should inherit
both public AND protected members from a base class?
Using ''friend''?

And what''s so very good about iterators compared
to an ordinary for-statement?
As in:

for(vector<int>::iterator etc ... )
vs.
for(int i = 0; i < myvector.size() etc ...)
Thanks.

-- Pelle

解决方案

Pelle Beckman wrote:

Hi all,

I have a few newbie questions:

In function declaration what does a ''const'' mean
inside the parameter list ? That it won''t modify the value?

void MemberFunction(const int x);
Yes, but in this example it''s meaningless -- ''const'' on a parameter is
meaningful wrt a pointer or reference:

void MemberFunction(const int &x);
void MemberFunction(const int *x);

in these cases it isn''t really the parameter itself that the function
promises it won''t modify, but the object that points at/refers to.

Since the parameter (unless it''s a reference) is always a copy of some
value, changing it doesn''t really mean much.
What does ''const'' before a function dec. mean?

const void MemberFunction(int x);
It means that what''s being returned is a const (whatever). Again, this
example has a minor problem -- since a function with a void return type
doesn''t return anything, you can can''t declare that nothing that''s
being returned as const.

Something like:

const int MemberFunction(int x);

does make some sense, though a const on the return is more common when
you''re returning something like a full-fledged object.
The same as ''static''?
No, not even similar. The const affects the type of object being
returned. In the same place, static would affect the type of the
function itself -- assuming (as the name implies) that this was a
member function, it would make it a static member function. A static
member function is almost comppletely different from a normal member
function because it''s not associated with a particular instance of the
object.
My pointer skills aren''t perfect, so what is
the differece between these functions?

void MyFunction(int& x);
void MyFunction(int* x);
The first receives a parameter by reference, while the second recieves
a pointer. The two often accomplish similar things, but a reference
always refers to the same object for its entire lifetime, while a
pointer can be changed so it points at different objects at different
times (or to nothing at all, if you so desire).
Why can''t ctors and dtors be "totally virtual"?

class MyClass {
virtual MyClass() = 0;
virtual ~MyClass() = 0;
};
That''s "pure virtual", in case you care. In any case, a dtor _can_ be
pure virtual if you want. A ctor can''t be virtual -- or is always
virtual, depending a bit on your viewpoint, but in any case, you can''t
specify it as virtual at all, and without that, pure virtual is
completely out of the picture.
How do I specify that one class should inherit
both public AND protected members from a base class?
Using ''friend''?
Anytime you do inheritance, you derive both public and protected
members from the base class.
And what''s so very good about iterators compared
to an ordinary for-statement?
As in:

for(vector<int>::iterator etc ... )
vs.
for(int i = 0; i < myvector.size() etc ...)



In this particular case, the iterator provides little real advantage --
the primary reason for using iterators is to pass them to algorithms.
The reason algorithms use them is to abtract iteration, so the
algorithm can use the same code to work with different kinds of
containers. Getting from one node in a linked list to the next is a lot
different from getting from one element in an array to the next, but
iterators allow you to write your code the same way to do either (or to
work with various other containers as well). At least in theory, this
makes it easier to substitute one container for another, though there
are some limitations on this -- the iterator gives a common syntax, but
doesn''t change the fundamentals, so an iterator won''t (for example)
give you random access to data that''s arranged to only allow serial
access.

--
Later,
Jerry.

The universe is a figment of its own imagination.


Jerry Coffin skrev:

Pelle Beckman wrote:

Hi all,

I have a few newbie questions:

In function declaration what does a ''const'' mean
inside the parameter list ? That it won''t modify the value?

void MemberFunction(const int x);


Yes, but in this example it''s meaningless -- ''const'' on a parameter is
meaningful wrt a pointer or reference:

void MemberFunction(const int &x);
void MemberFunction(const int *x);

in these cases it isn''t really the parameter itself that the function
promises it won''t modify, but the object that points at/refers to.

Since the parameter (unless it''s a reference) is always a copy of some
value, changing it doesn''t really mean much.

What does ''const'' before a function dec. mean?

const void MemberFunction(int x);


It means that what''s being returned is a const (whatever). Again, this
example has a minor problem -- since a function with a void return type
doesn''t return anything, you can can''t declare that nothing that''s
being returned as const.

Something like:

const int MemberFunction(int x);

does make some sense, though a const on the return is more common when
you''re returning something like a full-fledged object.

The same as ''static''?


No, not even similar. The const affects the type of object being
returned. In the same place, static would affect the type of the
function itself -- assuming (as the name implies) that this was a
member function, it would make it a static member function. A static
member function is almost comppletely different from a normal member
function because it''s not associated with a particular instance of the
object.

My pointer skills aren''t perfect, so what is
the differece between these functions?

void MyFunction(int& x);
void MyFunction(int* x);


The first receives a parameter by reference, while the second recieves
a pointer. The two often accomplish similar things, but a reference
always refers to the same object for its entire lifetime, while a
pointer can be changed so it points at different objects at different
times (or to nothing at all, if you so desire).

Why can''t ctors and dtors be "totally virtual"?

class MyClass {
virtual MyClass() = 0;
virtual ~MyClass() = 0;
};


That''s "pure virtual", in case you care. In any case, a dtor _can_ be
pure virtual if you want. A ctor can''t be virtual -- or is always
virtual, depending a bit on your viewpoint, but in any case, you can''t
specify it as virtual at all, and without that, pure virtual is
completely out of the picture.

How do I specify that one class should inherit
both public AND protected members from a base class?
Using ''friend''?


Anytime you do inheritance, you derive both public and protected
members from the base class.

And what''s so very good about iterators compared
to an ordinary for-statement?
As in:

for(vector<int>::iterator etc ... )
vs.
for(int i = 0; i < myvector.size() etc ...)


In this particular case, the iterator provides little real advantage --
the primary reason for using iterators is to pass them to algorithms.
The reason algorithms use them is to abtract iteration, so the
algorithm can use the same code to work with different kinds of
containers. Getting from one node in a linked list to the next is a lot
different from getting from one element in an array to the next, but
iterators allow you to write your code the same way to do either (or to
work with various other containers as well). At least in theory, this
makes it easier to substitute one container for another, though there
are some limitations on this -- the iterator gives a common syntax, but
doesn''t change the fundamentals, so an iterator won''t (for example)
give you random access to data that''s arranged to only allow serial
access.

--
Later,
Jerry.

The universe is a figment of its own imagination.



Thanks Jerry.

Followups:

Why would I want to return a const object?
(as in "const MyClass MyFunction()")
are there any good examples for this use?

What is the advantage of telling the
compiler that a function promises not
to modify the value (as in "void Method(const int* x)") ?

If there is a value I can''t modify - shouldn''t I just
make sure it doesn''t when I write the darn thing?


Pelle Beckman wrote:


Hi all,

I have a few newbie questions:

In function declaration what does a ''const'' mean
inside the parameter list ? That it won''t modify the value?

void MemberFunction(const int x);
It tells the compiler, that this function will not modify x.
In your particular example, this is useless, because it really
doesn''t matter to anybody,if this functions alters x or not.

But consider ouy have a function, which takes a reference:

void foo( int& a );

Now you call that function:
int main()
{
int x = 5;
foo( x );
}

And here comes the question: Does the function foo() alter
x, or does it not?. Since foo gets x passed by reference foo()
can do that. In this case you have no other choice then to assume
that it does.

Now if the function wants to signal to the caller: Don''t worry, I will
not touch what you have given me, it can do so:

void foo( const int& a );

Now the function declaration clearly says: I will take that int by reference,
but I will not alter it inside the function.

What does ''const'' before a function dec. mean?

const void MemberFunction(int x);
Something like ''const void'' does not exist.

const int MemberFunction( int x );

That const is part of the return type: It says that this function
returns an int, and that this int is constant (cannot be altered
by the caller). Again: This is pretty useless with an int, but

const char* Fnct();

says that the Function returns a pointer. A pointer to characters. And
those characters are constant.

The same as ''static''?
No. static tells us something about the function itself. The const
is simply part of the return type.

My pointer skills aren''t perfect, so what is
the differece between these functions?

void MyFunction(int& x);
void MyFunction(int* x);
The first one gets a reference, while the second one gets a pointer.
In this case, you can view a reference as some sort of ''hidden pointer''.
A pointer which exists but isn''t usable by you, the compiler takes care of it.
You want to do that to get a nicer syntax inside the function when accessing
x.

Why can''t ctors and dtors be "totally virtual"?

Well. A dtor can. And in case of polymorphism it often has
to be.
As for a ctor: How would you create an object of a class, when
you don''t now the class type during compile time?
class MyClass {
virtual MyClass() = 0;
virtual ~MyClass() = 0;
};

How do I specify that one class should inherit
both public AND protected members from a base class?
Using ''friend''?
A class inherits always *all* members from its base class.
The only question is: can a derived class access those members
or can it not.

And what''s so very good about iterators compared
to an ordinary for-statement?
As in:

for(vector<int>::iterator etc ... )
vs.
for(int i = 0; i < myvector.size() etc ...)



The iterators work on all stl containers the same way
(nearly, there are small exceptions, but in the above
case, it would not matter, if the container is a vector,
a map, a deque, a multimap, ...). Thus you can chance the
type of container without having to change your source code.
Just in case your choosen container turns out to behave poorly
and you want to try a different one.
Also: This opens the door for general algorithms. Those algorithms
work with iterators and it doesn''t matter to which container this
iterators belong. The algorithm is always the same.

--
Karl Heinz Buchegger
kb******@gascad.at


这篇关于初学者问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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