I'm not sure if "data hiding" is the correct term, but I'm trying to
emulate this object-oriented technique. I know C++ probably provides
much more than my example, but I'd just like some feedback to find out
if I've done anything wrong. Also, I am working on this for an
embedded environment, so if there are great inefficiencies with this
code, I'd like to know that also. I realize there is overhead with
using an "accessor" function.
I'm trying to:
1.) "hide" data (variables declared as static at file scope) within a
file (algorithm.c)
2.) manipulate the data using an algorithm function (Algorithm())
3.) provide external access to the data using an "accessor" function
(GetAlgorithmDa ta())
Here is my code:
main.c:
#include <stdio.h>
#include "algorithm. h"
int main(void)
{
const ALGORITHM_DATA *alg_ptr;
Algorithm();
GetAlgorithmDat a(&alg_ptr);
/*alg_ptr->data1 = 99.9;*/ /* This is not allowed */
printf("%f\n", alg_ptr->data1);
printf("%f\n", alg_ptr->data2);
printf("%f\n", alg_ptr->data3);
return 0;
}
algorithm.c:
#include "algorithm. h"
static ALGORITHM_DATA alg;
void GetAlgorithmDat a(const ALGORITHM_DATA **out)
{
*out = &alg;
}
void Algorithm(void)
{
alg.data1 = 1.0;
alg.data2 = 2.0;
alg.data2 = 3.0;
}
algorithm.h:
#ifndef ALGORITHM_H_
#define ALGORITHM_H_
typedef struct {
double data1;
double data2;
double data3;
} ALGORITHM_DATA;
void GetAlgorithmDat a(const ALGORITHM_DATA **out);
void Algorithm(void) ;
#endif /*ALGORITHM_H_*/
Apr 11 '07
11 5658
On Apr 11, 5:43 pm, "E. Robert Tisdale" <e...@netwood.n etwrote:
The C computer programming language supports encapsulation
but *not* data hiding (like the private label in C++).
C programmers have always used "opaque" data types
when they need to "hide" data.
This is accomplished by publishing a type declaration
in a public header file (algorithm.h in this case)
and defining the new type in a private implementation file
(algorithm.c in this case).
I'm sorry about my misunderstandin g of "opaque" data types in my
previous posts. I was not familiar with this concept. It is not my
goal to create "opaque" data types. I am writing application code and
not API code. My intention was to prevent external functions from
modifying my "hidden" (statically declared) data structures. I did not
want to "hide" the members of the structure. I realize this is
primitive compared to your code (which, I admit, I did not
understand). I am just trying to find a better alternative to using
all global variables which our legacy code currently uses. Note, our
project is relatively small at ~10,000 lines of code. Do you think my
method would be an improvement to using all global variables?
sofeng wrote:
On Apr 11, 5:43 pm, "E. Robert Tisdale" <e...@netwood.n etwrote:
>The C computer programming language supports encapsulation but *not* data hiding (like the private label in C++). C programmers have always used "opaque" data types when they need to "hide" data. This is accomplished by publishing a type declaration in a public header file (algorithm.h in this case) and defining the new type in a private implementation file (algorithm.c in this case).
I'm sorry about my misunderstandin g of "opaque" data types in my
previous posts. I was not familiar with this concept. It is not my
goal to create "opaque" data types. I am writing application code and
not API code. My intention was to prevent external functions from
modifying my "hidden" (statically declared) data structures. I did not
want to "hide" the members of the structure. I realize this is
primitive compared to your code (which, I admit, I did not
understand). I am just trying to find a better alternative to using
all global variables which our legacy code currently uses. Note, our
project is relatively small at ~10,000 lines of code. Do you think my
method would be an improvement to using all global variables?
No!
You should avoid global variables including static global variables.
Why can't you use global constants?
----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =---- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Picho |
last post by:
Hi all,
Lets say I have a "secret" I wish to "hide", lets say a database password.
For the more detailed problem, a web application/service that uses a
connection string.
all the solutions I came up with (embedding in code, encrypting-decrypting)
involve embedding the/another secret in the code. since my problem cannot
request a user intervention, I am at a stop.
|
by: Amir S. |
last post by:
Hi,
I'm a newbie to C++ (2 weeks into the course). We were given this
assignment to write some code that reads a set of integers (grades)
from a file (filename passed by console), outputs them in reverse
order, calculates and prints their average.
=> We have been told that we need to make sure that our code meets
"information hiding" guidelines. Could anyone help me get started with
this (any ideas?)
|
by: 21novembre |
last post by:
Hi all,
I made a question several days before to describe my strange trouble of
mysqldump. But I still can't figour it out. Well, I just want to ask
another question whether I could just backup my databases by copying
the data folder to some place? Then if I meet some disaster, could I
just copy the backup folder back to recover my databases?
Thank you.
Zh.y
|
by: Jerry |
last post by:
I have an off-the-shelf app that uses an Access database as its
backend. One of the tables contains a field with an "OLE Object"
datatype. I'm writing some reports against this database, and I
believe this field contains data I need. When I view the table in
datasheet view, all I can see in this field is the string "Long
binary data".
So, I've got the problem of needing to extract data from this
field, but I don't know what format...
|
by: FX |
last post by:
I wanna publish a script on my site which allows me to hide image
source.
i have rough idea abt it. i`ll point src to some php page like:
<img src="image.php">
& in tht php wat exactly shud be done so tht user doesnt come to know
the real source location of image file upon clicking its properties.
I've seen websites doing this. can somebody post the script for it?
Thanx in advance
| |
by: Nicolas Castagne |
last post by:
Hi all,
I have been wondering for a while why function hiding (in a derived
class) exists in C++, e.g. why when writing
class Base {
void foo( int ) {}
};
class Derived: public Base {
void foo( char const ) {}
};
|
by: Nick Gilbert |
last post by:
Hi,
I'm using the asp:Wizard control and on some of the steps, I would only
like the user to be able to progess to the next step by clicking an
image button. Therefore I would like to be able to hide the Next button
on these steps.
Is there any way to do this?
I know you can convert the navigation to a template, but this is a
|
by: Sanders Kaufman |
last post by:
I'm converting my table-based layouts to css-positioned divs - but the
ONLY reason I'm doing it is because it's *considered* a best practice.
I don't really see where anything goes hinky when tables are used - but
I'm doing it anyway because the HTML and CSS specs says to reserve
tables for tabular data.
So as I convert my login widgit to a css thing, I'm saying to myself -
hey, this form is most certainly "tabular data" - even if it...
|
by: BobRoyAce |
last post by:
I am using Visual Studio 2008 w/ VB.NET.
For the database, I am using SQL Server 2005, which is running on a
dedicated server box.
I am creating a WinForms application for a client. It is run on any of
several employees' desktop PCs.
Now, they want to be able to "push" some of the data from the SQL
Server database up to a database on a website (also SQL Server 2005).
|
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...
|
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,...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |