范围问题 [英] issue with scope

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

问题描述

我熟悉C和Java,我想使用一种风格,我已经习惯于使用C ++在Java中习惯了但是每次我这样做我都有

名称冲突。在过去,我刚刚使用

来解决它。不同的名字可以告诉我如何让以下工作:


我想拥有一个类名为v3d(向量3d),它有以下

私有实例变量......

浮动x,y,z;


它有以下公共获取和设置方法:

//设置方法

void x(浮动x);

void y (浮动y);

void z(浮动z);

//获取方法

浮动x(无效);

float y(void);

flaot z(void);


set方法中的代码...

void x(浮动x){

this-> x = x;

}


代码get方法......

浮动x(无效){

返回x;

}


为什么我的编译器似乎有问题v3d :: x,v3d :: x()和

x用作函数中的参数?

I am familiar with C and Java, I would like to use a style that I have
become accustomed to in Java with C++ but each time I do this I have
name conflict. In the past I have just worked around it by using
different names can anyone tell me how to get the following to work:

I want to have a class named v3d (vector 3d), it has the following
private instance variables...
float x, y, z;

It has the following public get and set methods:
//set methods
void x(float x);
void y(float y);
void z(float z);
//get methods
float x(void);
float y(void);
flaot z(void);

The code in a set method...
void x(float x){
this->x = x;
}

The code in a get method...
float x(void){
return x;
}

Why does my compiler seem to have an issue with v3d::x, v3d::x() and
the x used as an argument in the function?

推荐答案

不可能能够在c ++中的

相同范围内声明两个不同类型的标识符。这是java语法的一个特性,用于识别要声明函数和变量的
,c ++不支持
支持这个。


函数


void x(浮动x){

this-> x = x;

}

如果要重命名函数,
应该可以正常工作。在这种情况下,参数x

隐藏成员变量x,因此赋值可以工作。


- peter

it is not possible to declare two identifiers of differing type in the
same scope in c++. it is a feature of the java grammar to recognize
that you want to declare both a function and a variable, c++ does not
support this.

the function

void x(float x){
this->x = x;
}

should work properly if you would rename the function. the argument x
shadows the member variable x in this case, thus the assignment works.

-- peter


如果是这种情况我有点失望。在过去,我有

在实例变量中加上前缀''its''来自

Jesse Liberty的建议。例如itsx,itsy,itsz或前缀成员名称

with get and set ...我喜欢在矢量

成员上没有get / set前缀因为它会添加大量打字。


谢谢彼得。

If this is the case I am a bit disappointed. In the past I have either
added the prefix ''its'' onto the instance variables a suggestion from
Jesse Liberty. Such as itsx, itsy, itsz or prefixed the member names
with get and set... I like not having the get/set prefix on vector
members because it will add a lot of typing.

Thank you peter.


Ken写道:
我熟悉C和Java,我想使用我已经习惯于使用C ++的Java习惯,但每次我这样做都会产生名称冲突。


嗯,那么你只需要学习编写C ++,就像C ++程序员那样做b $ b,而不是尝试用C ++编写Java。


我熟悉Common Lisp,其他一切看起来都很糟糕。


我把它搞砸了!

In过去我刚刚通过使用不同的名称来解决它,任何人都可以告诉我如何让以下工作:

我想要一个名为v3d(向量3d)的类,它具有以下<私有实例变量......
浮动x,y,z;

它有以下公共get和set方法:
//设置方法
void x(float x);
void y(float y);
void z(float z);
// get methods
float x(void);
float y(void);
flaot z(void);


有一种迂回的方式可以做得更好。您可以直接创建模板类,而不是直接使用

浮点类型。

类的实例表现得像模板参数类型的对象。他们

自动转换为类型,你可以将它分配给

他们。


但模板类有两个附加参数:即指针

到成员函数来获取和设置值。该模板还将一个

指针指向该对象作为参数,因此它有一个对象来调用

这些函数。


使用此SmartMember模板类可能如下所示:


class MyClass {

public:

SmartMember< MyClass,int> x;

public:

MyClass():x(this,& MyClass :: get_x,& MyClass :: set_x){}

private:

int get_x();

void set_x(const int&);

};


SmartMember< int> object x存储一个指向MyClass实例的指针,

以及指向两个成员函数的指针,用于设置和获取。它已经重载了运营商的b
。您可以将int值分配给x,

或可以隐式转换为int的值。这些值通过

到set_x()函数。你也可以显式地将x转换为int,

这也会在许多情况下隐式发生,比如函数

参数表达式。


MyClass obj;


obj.x = 3; //调用obj.set_x(3);

int y = obj.x; //调用obj.get_x();


我将离开SmartMember< int>作为读者的练习。这是非常简单的
。它是这样的:


模板< class CLASS,类TYPE>

类SmartMember {

private:

void(CLASS :: * set_func)(const TYPE&);

TYPE(CLASS :: * get_func)();

CLASS * obj;

public:

SmartMember(CLASS * o,void(CLASS :: * sf)(const TYPE&),TYPE

(CLASS :: * gf)())

:set_func(sf),get_func(gf),obj(o){}

SmartMember& operator =(const TYPE& rhs){(obj-> * set_func)(rhs);

返回* this; }


// ...等等

};


如果get_func和set_func可以改成

模板参数,这会减少对象的大小。

get方法中的代码...
float x( void){
^^^^^


(void)是C兼容性的过时语法。当你编写C ++类成员函数时,C兼容性是无关紧要的,因为你将无法通过C编译器成功地获得它。


在C ++中,你写()。

返回x;
}
为什么我的编译器似乎与v3d :: x有问题,v3d :: x()和
x用作函数中的参数?
I am familiar with C and Java, I would like to use a style that I have
become accustomed to in Java with C++ but each time I do this I have
name conflict.
Well, then you just have to learn to write C++ the way C++ programmers
do it and not try to write Java in C++.

I''m familiar with Common Lisp, and everything else looks like crap.

I suck it up!
In the past I have just worked around it by using different names can anyone tell me how to get the following to work:

I want to have a class named v3d (vector 3d), it has the following
private instance variables...
float x, y, z;

It has the following public get and set methods:
//set methods
void x(float x);
void y(float y);
void z(float z);
//get methods
float x(void);
float y(void);
flaot z(void);
There is a roundabout way to do something better. Instead of using the
float type directly, you can create a template class. Instances of that
class behave like objects of the template argument type. they
automatically convert to the type, and you can assign that type to
them.

But the template class has two additional parameters: namely, pointers
to member functions to get and set the value. The template also takes a
pointer to the object as a parameter, so it has an object to invoke
these functions against.

Use of this "SmartMember" template class might look like this:

class MyClass {
public:
SmartMember<MyClass, int> x;
public:
MyClass() : x(this, &MyClass::get_x, &MyClass::set_x) { }
private:
int get_x();
void set_x(const int &);
};

The SmartMember<int> object x stores a pointer to the MyClass instance,
and pointers to two member functions for setting and getting. It has
overloaded operators to do the magic. You can assign int values to x,
or values that can implicitly convert to int. These values get passed
to the set_x() function. You can also explicitly convert x to int,
which will also happen implicitly in many contexts, like function
argument expressions.

MyClass obj;

obj.x = 3; // calls obj.set_x(3);
int y = obj.x; // calls obj.get_x();

I will leave SmartMember<int> as an exercise for the reader. It''s
pretty easy. It goes something like this:

template <class CLASS, class TYPE>
class SmartMember {
private:
void (CLASS::* set_func)(const TYPE &);
TYPE (CLASS::* get_func)();
CLASS *obj;
public:
SmartMember(CLASS *o, void (CLASS::* sf)(const TYPE &), TYPE
(CLASS::*gf)())
: set_func(sf), get_func(gf), obj(o) { }
SmartMember &operator = (const TYPE &rhs) { (obj->*set_func)(rhs);
return *this; }

// ... et cetera
};

It would be nice if the get_func and set_func could be made into
template parameters instead, which would cut down the size of object.
The code in a get method...
float x(void){ ^^^^^

(void) is obsolete syntax for C compatibility. C compatibility is
irrelevant when you are writing a C++ class member function, since you
will never successfully get that through a C compiler.

In C++, you write ().
return x;
}

Why does my compiler seem to have an issue with v3d::x, v3d::x() and
the x used as an argument in the function?




你不能重载数据成员和函数会员。你只能使用
过载功能。所以基本上你必须重命名你的x。一个名为m_x的
成员将很乐意与ax()和x(浮动)

函数共存。


但它''仍然很难看它是一个功能:语言应该能够抽象访问看起来像对象属性x的东西。该类的

用户不应该关心x是否是一个简单的数据成员,或者

是否访问x实际上是函数调用执行

计算。


SmartMember模板是一种方法。



You can''t overload a data member and a function member. You can
overload functions only. So basically you have to rename your x. A
member called m_x will happily coexist with a x() and x(float)
function.

But it''s still ugly that it''s a function: the language ought to be able
to abstract accesses to what looks like a property x of the object. The
user of the class shouldn''t care whether x is a simple data member, or
whether accesses to x are actually function calls that perform
computation.

The SmartMember template thing is a way to do that.


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

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