473,407 Members | 2,320 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,407 software developers and data experts.

How to hide class member variable

Hi,

I have a class with some member-functions. I should like to have a way
to add a non-static-member-variabile to the class that be visible by
only one member function.

TIA
Marco.
Sep 8 '05 #1
6 2776
* Marco Segurini:
I have a class with some member-functions. I should like to have a way
to add a non-static-member-variabile to the class that be visible by
only one member function.


C++ doesn't directly support that. You can make that member function its
own class that you inherit from, but passing all necessary information up
the chain might entail some run-time inefficiency. Also, it's a lot of
complication for very little gain.

On the other hand, a solution somewhat like that can (just can) be good
medicine for 'mutable'.

And also for resolving e.g. order-of-initialization. It's a very common,
what should I say, meta-pattern. Try it, and if you don't like the result
(I'm sure that I wouldn't, for your case), then just accept the language as
it is, and/or consider _redesigning_ your class.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 8 '05 #2
Marco Segurini wrote:
I have a class with some member-functions. I should like to have a way
to add a non-static-member-variabile to the class that be visible by
only one member function.


Write a class with that variable as private member, put the function in it,
and inherit form him.

--
Salu2
Sep 8 '05 #3
You can also create a variable private to the function.

class X
{
void fx();
}

void X::fx()
{
int i;
}

if you want to keep its value among calls you should create it as
static.

Sep 8 '05 #4
"Isaque" <ig******@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com
You can also create a variable private to the function.

class X
{
void fx();
}

void X::fx()
{
int i;
}

if you want to keep its value among calls you should create it as
static.


The problem with this is that there is only one instance of the variable per
class. Presumably the OP wants one instance per object.
--
John Carson

Sep 8 '05 #5
John Carson wrote:
"Isaque" <ig******@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com
You can also create a variable private to the function.

class X
{
void fx();
}

void X::fx()
{
int i;
}

if you want to keep its value among calls you should create it as
static.

The problem with this is that there is only one instance of the variable
per class. Presumably the OP wants one instance per object.

How about:

class X
{
private:
class Y
{
private:
int i;
friend void X::foo();
} y;

public:
void foo();
void bar();
};

void X::foo()
{
// use y.i here safely;

}

void X::bar()
{
// can't use y.i here
}
Sep 8 '05 #6
"red floyd" <no*****@here.dude> wrote in message
news:uX*****************@newssvr12.news.prodigy.co m

How about:

class X
{
private:
class Y
{
private:
int i;
friend void X::foo();
} y;

public:
void foo();
void bar();
};

void X::foo()
{
// use y.i here safely;

}

void X::bar()
{
// can't use y.i here
}


Looks good to me except that the declaration of foo() needs to precede that
of class Y in order for the code to compile.

--
John Carson

Sep 8 '05 #7

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

Similar topics

13
by: genetic.error | last post by:
I'm moving from Vb6 to VB.Net. I have a feeling this has come up before... The VS.Net MSDN file seems to state that the following should work: Form1.Show Form1.Visible = True Form1.Hide...
2
by: owingruters | last post by:
Hi all, I'm trying to hide a member from a base class, like so : public class class1 { public class1() { } public int i; }
5
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but...
5
by: ahaupt | last post by:
Hi all, Do you know if it is possible to hide class methods when the class has not been "properly" instantiated? Say in the following example, I'd like to hide or disable Drive() when the...
19
by: Jamey Shuemaker | last post by:
I'm in the process of expanding my knowledge and use of Class Modules. I've perused MSDN and this and other sites, and I'm pretty comfortable with my understanding of Class Modules with the...
4
by: Henrik Goldman | last post by:
Hi, Lets say I have a class a which looks like the following: class a { public: a(int i) { m_nI = i;
9
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
6
by: Sourabh Daptardar | last post by:
Why is register storage specifier not allowed for class member variables? <snip> ISO standard : Section 9.2 ; point 6 : -6- A member shall not be auto, extern, or register. </snip> I...
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?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.