473,402 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

issue with scope

Ken
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?

Nov 5 '05 #1
12 1554
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

Nov 5 '05 #2
Ken
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.

Nov 5 '05 #3
Ken wrote:
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?


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.

Nov 5 '05 #4
Ian
Ken wrote:
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;

for a simple container like this where you want to directly access the
data members, why not simply use:

struct v3d
{
double x;
double y;
double z;
};

Ian
Nov 5 '05 #5
In article <11**********************@g14g2000cwa.googlegroups .com>,
Peter Steiner <pn*******@gmail.com> wrote:
it is not possible to declare two identifiers of differing type in the
same scope in c++.
That's not always so...
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.


....although this is.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Nov 5 '05 #6
In article <11**********************@f14g2000cwb.googlegroups .com>,
Ken <ke*@kenmcwilliams.com> wrote:
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.


This makes me wonder if there is not too much going on too low level
about setting members?

Also, a problem with something like its is what you've shown:
itsy. That would mean something small to me.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Nov 5 '05 #7

"Ken" <ke*@kenmcwilliams.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
| 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?

You may think the issue you raise is a weakness in the language, but
its actually one of its powerful features. Ambiguity from the user's
perspective is not allowed. It grately enhances the code's readability.
This is specially true since you have the benefit of initialization
lists in C++. Something those other languages would *love* to have.

class N
{
int m_n;
public:
N() : m_n(0) { }
N( int n ) : m_n(n) { }
~N() { }
};

N n(3);

imagine a class that looks like this:

#include <iostream>
#include <ostream>

template< typename T >
class V3d
{
T x;
T y;
T z;
public:
V3d(T tx, T ty, T tz) : x(tx), y(ty), z(tz) { }
~V3d() { }
void set(T tx, T ty, T tz)
{
x = tx;
y = ty;
z = tz;
}
friend std::ostream& operator<<( std::ostream& os, V3d< T >& t )
{
os << t.x << ", ";
os << t.y << ", ";
os << t.z << "\n";
return os;
}
};

int main()
{
V3d<int> n3d( 0, 1, 2 );
std::cout << n3d;
n3d.set( 3, 4, 5 );
std::cout << n3d;

V3d<double> d3d( 0.0, 1.1, 2.2 );
std::cout << d3d;

V3d<char> c3d( 'a', 'b', 'c' );
std::cout << c3d;

return 0;
}

/*
0, 1, 2
3, 4, 5
0, 1.1, 2.2
a, b, c
*/
Nov 6 '05 #8

"Kaz Kylheku" <kk******@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
| Ken wrote:
| > 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();

int get_x() const;

| 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?
|
| 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.
|
Nov 6 '05 #9

Ken wrote:
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?


Why not just name the member variables x_, y_ and z_?

Greg

Nov 6 '05 #10
Ken
Because my vectors require some operations... addition, subtraction,
dot and cross products, projection and length.

Nov 6 '05 #11
Ken
I know I could have done it but I wanted to know if I was doing
something wrong and there was some magic way to make it work.

Nov 6 '05 #12
Ian
Ken wrote:
Because my vectors require some operations... addition, subtraction,
dot and cross products, projection and length.

So? Add them to the simple struct. If you are providing a full set of
accessors, why complicate things with private data members?

Also, please quote, this reply made no sense out of context.

Ian
Nov 6 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Pjotr Wedersteers | last post by:
I have a function foo () in a file called funcfoo.php called from my main program file main.php, which has an include for that file. Foo () contains a line: echo $bar; Now I have a (global)...
3
by: PeteMc | last post by:
Hi I'm new to c# and .net from a vb6 background and i'm having issues with the debugging of applications I am trying to create a watch on an expression aString.IndexOf(",",0).ToString() but...
11
by: VK | last post by:
Folks, An aspx page has some querystring variables in the URL. In the code beind file, I tried requesting these variables and setting as public variables, so it is not specific to a sub. ...
17
by: Danny J. Lesandrini | last post by:
The following code works with a standard MDB to navigate to a particluar record (with a DAO recordset, of course) but it's giving me problems in an ADP I'm working on. Dim rs As ADODB.Recordset...
12
by: Sunny | last post by:
Hi All, I have a serious issue regarding classes scope and visibility. In my application, i have a class name "TextFile", and also a few other classes like "TotalWords", "TotalLines" and etc..,...
8
by: Laphan | last post by:
Hi All I'm using the same style of form on a number of pages, but for some reason the exact same one liner on one my pages doesn't do anything. The one liner is: <INPUT TYPE="BUTTON"...
8
by: Mark | last post by:
Hi, I have two classes: template <class Resourceclass GuardBase { protected : Resource& fResource; bool fAcquired;
7
by: Victor | last post by:
I just tried a test comparing a Function to a Private Function with this code: <% Option Explicit dim X1 X1 = 9 Private Function RealTest(here) RealTest = here + X1 End Function
2
by: mr.mattyg | last post by:
I might as well start off like everyone else who posts problems they are having....So I'm new to JavaScript..... Anywho, I have a page that lists 15 or so thumbnails and then one big image of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.