473,804 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Too many datamembers

I have a ordinary C++ class with lot of datamembers used internally
between methods of the same class.

Is there any other elegant way to maintain the private datamembers.

Please give your suggestions.
Nov 12 '08 #1
7 1973
sreeni wrote:
I have a ordinary C++ class with lot of datamembers used internally
between methods of the same class.

Is there any other elegant way to maintain the private datamembers.

Please give your suggestions.
Not knowing your requirements or your design, we can only guess.

My suggestion? Refactor.
Nov 12 '08 #2
On Nov 12, 6:11*am, sreeni <sreeni.h...@gm ail.comwrote:
I have a ordinary C++ class with lot of datamembers used internally
between methods of the same class.

Is there any other elegant way to maintain the private datamembers.

Please give your suggestions.
Hi

It is difficult to suggest about your problem unless we see a concise
code, but consider
the following suggestions:
1. If there are a lot of class members (data members and function
members) in a class,
it is the sign of big or fat class, so split it to two or more
classes.
2. May be the new smaller classes will be belong to a class hierarchy.
3. You can arrange and categorize data members as a nested class
inside original one.
4. Think about do: you really need to those data members? Sometimes it
occurs to me. some data members are really the formal parameters for
member functions. I use a simple rule: Does the data member have role
in object construction ( I mean in constructor)?

Regards
Saeed Amrollahi
Nov 12 '08 #3
On Nov 12, 3:11*am, sreeni <sreeni.h...@gm ail.comwrote:
I have a ordinary C++ class with lot of datamembers used internally
between methods of the same class.

Is there any other elegant way to maintain the private datamembers.
The most elegant way is to hide the implementation details of your
class. All non-public members are an implementation detail. You hide
these by providing an abstract class / interface and a factory
function that creates an instance of that interface.

--
Max
Nov 12 '08 #4
On Nov 12, 11:42 am, Maxim Yegorushkin <maxim.yegorush ...@gmail.com>
wrote:
On Nov 12, 3:11 am, sreeni <sreeni.h...@gm ail.comwrote:
I have a ordinary C++ class with lot of datamembers used
internally between methods of the same class.
Is there any other elegant way to maintain the private
datamembers.
The most elegant way is to hide the implementation details of
your class. All non-public members are an implementation
detail. You hide these by providing an abstract class /
interface and a factory function that creates an instance of
that interface.
That's one way. The more idiomatic way in C++ is the
compilation firewall idiom. Both work, but neither is totally
without drawbacks. The abstract class/factory function, for
example, doesn't work if users have to be able to derive from
the interface.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 12 '08 #5
On Nov 12, 3:04*pm, James Kanze <james.ka...@gm ail.comwrote:
On Nov 12, 11:42 am, Maxim Yegorushkin <maxim.yegorush ...@gmail.com>
wrote:
On Nov 12, 3:11 am, sreeni <sreeni.h...@gm ail.comwrote:
I have a ordinary C++ class with lot of datamembers used
internally between methods of the same class.
Is there any other elegant way to maintain the private
datamembers.
The most elegant way is to hide the implementation details of
your class. All non-public members are an implementation
detail. You hide these by providing an abstract class /
interface and a factory function that creates an instance of
that interface.

That's one way. *The more idiomatic way in C++ is the
compilation firewall idiom.
How do you tell which one is more idiomatic?
Both work, but neither is totally
without drawbacks. *The abstract class/factory function, for
example, doesn't work if users have to be able to derive from
the interface.
Hm... It works for me - the venerable decorator design pattern:

#include <memory>

struct Interface
{
virtual ~Interface() = 0;
// idiomatic functions ;)))
virtual void foo() = 0;
virtual void bar() = 0;
};
std::auto_ptr<I nterfacecreateI nterfaceImpleme ntation();

class DeriveFromInter face : public Interface
{
private:
std::auto_ptr<I nterfacebase_im plementation_;

// disable these for simplicity
DeriveFromInter face(DeriveFrom Interface const&);
DeriveFromInter face& operator=(Deriv eFromInterface const&);

public:
void foo() { base_implementa tion_->foo(); }
void bar() { base_implementa tion_->bar(); }

DeriveFromInter face()
: base_implementa tion_(createInt erfaceImplement ation())
{}
};

--
Max

Nov 12 '08 #6
On Nov 13, 8:22*am, Michael DOUBEZ <michael.dou... @free.frwrote:
Maxim Yegorushkin a écrit :
On Nov 12, 3:04 pm, James Kanze <james.ka...@gm ail.comwrote:
On Nov 12, 11:42 am, Maxim Yegorushkin <maxim.yegorush ...@gmail.com>
wrote:
>On Nov 12, 3:11 am, sreeni <sreeni.h...@gm ail.comwrote:
I have a ordinary C++ class with lot of datamembers used
internally between methods of the same class.
Is there any other elegant way to maintain the private
datamembers .
The most elegant way is to hide the implementation details of
your class. All non-public members are an implementation
detail. You hide these by providing an abstract class /
interface and a factory function that creates an instance of
that interface.
That's one way. *The more idiomatic way in C++ is the
compilation firewall idiom.
How do you tell which one is more idiomatic?

The factory system requires that everything be put on the heap and use
mechanisms to dispose of it and ensure exception safety; whereas the
firewall idiom let you instantiate the class as any other and dynamic
allocation is handled internally/locally.

In this regard, the firewall idiom is a far better tradeoff for the
intended usage here.
True. In this case you just modify the class, but not users.

--
Max

Nov 13 '08 #7
On Nov 11, 10:11*pm, sreeni <sreeni.h...@gm ail.comwrote:
I have a ordinary C++ class with lot of datamembers used internally
between methods of the same class.

Is there any other elegant way to maintain the private datamembers.

Please give your suggestions.
The bulging forehead types around here have given you
the answers. But unless you already knew them, you may
not understand what they are talking about.

Internal data members are not necessarily a problem,
even if there are lots of them. What you want to try
to do is manage the complexity. Complexity is when
it starts to be a problem keeping track of what goes
where, what belongs to what code, and so on.

The drive is always to have manageable chunks of data
to think about at one time. If you can hold an entire
chunk of the program (code and data) in your head at
the same time, then that's probably a good sign that
you have got about the right size objects.

When you design a class, you need to have an idea of
what that class will do for you. Think of a class as
an exporter of some task. Usually, if the task is "hold
on to a bunch of data and offer it up when asked" or
something like that, then your task is pretty weak.
This is sometimes called a "thin abstraction." This is
one way that complexity gets away from control. If you
treat a class as just a big bag that you shovel in a
bunch of related stuff, then you can easily lose control
and get a large bag of hard to manage complexity.

If your abstraction is something like "model the
behaviour of a car" then you start to see the
power of a class. You want your abstraction to give
you clues as to how to design the interface.
And how to design the internal relationships.

So, to manage the complexity of modelling a car
in C++, you might have a car class. Then the car
class would have internal data members that were
themselves instances of classes. An engine class,
a transmission class, a driver interface class, etc.

Just as the larger class hides details from its
clients, so too the internal classes hide details
from eachother. So, the engine does not need to be
able to see the details of the internal workings
of the transmission. The driver only needs to see
the controls, not the details of how the inside of
the radio works, or where the wires go from the
turn signals, and so on.

So, you want to examine your abstraction and see if
you have things aligned with the problem you are
trying to solve with your program. Is it in fact
the case that the thing you are modelling has this
big set of data items that need to be visible to
eachother? If not, can you reasonably divide this
thing into smaller, more manageable, classes?

So, you could design a car class as one big bag.
Everything to do with the internal guts of a car
could be visible once you were inside the car class.
But that would get unworkable fast. When you wanted
to call the routine to inventory the glove
compartment, you'd have to sort through the data
members that told you about all the other stuff, the
throttle position, the trunk contents, the radio
station currently playing, the gear the car was in,
and so on.

But if you design the car class to contain instances
of other classes, you can hide those details in the
other classes. The car has a glove compartment. The
glove compartment details hide behind a class interface.
The car has an engine, those details hide behind a
class interface for the engine. And maybe the engine
has enough details that it would work to have *it*
divide its contents up into other classes.

So, not only do you hide details from clients of a car
class, you hide details of one part of the car from
the other parts of the car. That way, when the auto-
matic transmission decides to change gears, you don't
accidentally wind up changing the radio station also.

In addition to helping manage complexity, there are
gains in terms of testing, documentation, etc. If you
have a radio class for the car, then you can take
that radio class and test it by itself. You can
document that code by itself. And, in principle, you
can reuse that code in another project, because you
have built it with a clean, easy to use interface.

There are lots of good books on this subject. A good
place to start (just don't let it be your last book
on the subject) is _Code Complete (2nd edition)_
by Steve McConnel.
Socks
Nov 14 '08 #8

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

Similar topics

4
2841
by: Kate | last post by:
I have some 2-4D arrays that are filled by several different subs. Instead of maintaining a global or module level counter, I'd rather redim the array after each sub is finished, and start the next sub out at ubound(ary). The program is already getting sluggish and I'm looking for ways to improve performance--is redimming the arrays adding an unnecessary load? BTW, if anyone has some good ways to investigate which parts of the
2
1425
by: balasubramaniam | last post by:
I comile the code in unix where sizof(int)=4 #include<iostream> using namespace std; class A { int i; static int j; }; int A::j = 10;
4
1585
by: Ant | last post by:
Hi I'm quite new to C#. I've been playing around with variables declared outside the scope of a method with & without the static keyword. What difference does declaring variables in either fashion make? For that matter, why must you declare methods with the static keyword? Thanks for any ideas Ant
4
2875
by: FyinsFlip | last post by:
Microsoft Data Access Blocks (SqlHelper) you can do an insert and get the new row ID without using a stored procedure? DataMembers.Car_IDObject = SqlHelper.ExecuteScalar( _connectionString , "INSERT INTO tCar (name) VALUES ('Ford') SELECT CAST(@@Identity AS INTEGER) ", FillSqlParameter()); Is there a new line symbol I can add before the SELECT, like a ';' or something?
14
3487
by: Paul | last post by:
I want to set the page title and/or a form hidden field programatically through ASP.Net. I do not want to use something like... <% sTitle ="My Title" %> <html><title><%=sTitle%></title>..... I want to completely seperate the code from presentation. I would like to do the same thing for a value of a hidden form field but
9
2044
by: romayankin | last post by:
Here is the problem. There are two constructors of the same class ~~~~~~~~~~~~ CClass() : param1 (0.5), param2 (100000), param3 (NULL), szPath (NULL) { }
0
2372
by: Nick Caramello | last post by:
We are in the process of prototyping an architecture for a multi-tier application, and have run into the following problem: 1) We have a client application who calls a method sending over a test.client.testClass proxy object, attribributed as which is generated by svcutil 2) The server receives the testClass (as test.server.testClass) does some processing on that object, and then forwards it to another server also using WCF.
3
2053
by: tomPee | last post by:
Hi, I have the following problem: I am trying to make some sort of base class menu that i can then use to derive other menu's from. Those menu's should then be able to interact with each other. And, i have most of the idea figured out and I thought out how i want to do it. But when i started coding i found a slight... difficulty. It might be easy to overcome, but google let me down :( and my own imagination made one happy jump, but...
7
1596
Airslash
by: Airslash | last post by:
Hello, I'm currently working on a part of my project for sending and recieving messages over the Network. The problem here is that I have a class Message that resembles a message beeing sent. The client for transmitting uses 2 messagequeues. One for incoming messages and one for outgoing messages. I'm looking for a way to sort the messages already present in the queue. These messages first need to be sorted by Reponsetype. (1 = direct...
0
9716
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10604
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10101
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9177
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7643
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3837
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3005
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.