473,513 Members | 2,401 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

more elegant solution

I have a struct, that has a lot of double variables. (only doubles,
assume). Now, each variable has a different "default" value, which is
set in Init function.

So, is there any better way of checking wheter mystruct.myvalue has
the default value set than this:
# # #
// a class for checking each member
struct SuperClass
{
virtual void Init() =0;

// that's the main thing
template <class Tbool IsStd(const T& type, const double* pD)
{
// create a new one and initialize with defaults
T t; t.Init();
// see if the double at the offset:
// &t + (pD-this) == *pD
return (*(double*) ((char*)&t + ((char*)pD - (char*)this)) == *pD);
}
};
// that's just a struct with members
struct test : public SuperWalls
{
test() {Init();}
void Init() {m_a=1; m_b=2; m_c=3;}
double m_a, m_b, m_c;
};
int main(int, char**)
{

test t;
t.m_c = 5;
// check for defualts
printf("%d, %d\n", t.IsStd(t, &t.m_b), t.IsStd(t, &t.m_c));

}
# # #
I wish I could just write:
t.IsStd(&t.m_b);
or even better:
IsStd(&t.m_b);

but without using a macro.
Thank you,
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
Jul 14 '06 #1
2 1180
Gernot Frisch wrote:
I have a struct, that has a lot of double variables. (only doubles,
assume). Now, each variable has a different "default" value, which is
set in Init function.

So, is there any better way of checking wheter mystruct.myvalue has
the default value set than this:
Sure:

1) get rid of the /init()/ member function;
2) write a constructor for your class;
3) initialize its members with their default values in the
constructor's initialization list;
4) instantiate and use your objects feeling certain that their
members have all been initialized with their default values.

Pardon me case I have misunderstood your question, but this is the
simplest solution.

Regards,

--
Ney André de Mello Zunino
Jul 14 '06 #2
1) get rid of the /init()/ member function;
2) write a constructor for your class;
3) initialize its members with their default values in the
constructor's initialization list;
4) instantiate and use your objects feeling certain that their
members have all been initialized with their default values.

Pardon me case I have misunderstood your question, but this is the
simplest solution.

Sorry, my question was not clear.
I know about c'tor instead of init. OK. But I wanted a faster/better
way of writing:
t.IsStd(t, &t.m_c); without a macro.

So, basically:
t.IsStd(&t.m_c); // only

I used a Macro:
#define IS_STD(a,b) (a.IsStd(a, a.b) )

which works fine, but I'd lkie to know if there's any better way of
seeing, if t.m_c contains the default value, given by the init
function.

Is my question clear?
Jul 17 '06 #3

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

Similar topics

6
5003
by: Kamilche | last post by:
Is there a more elegant way to change the working directory of Python to the directory of the currently executing script, and add a folder called 'Shared' to the Python search path? This is what...
17
7336
by: Fresh Air Rider | last post by:
Hello Could anyone please explain how I can pass more than one arguement/parameter value to a function using <asp:linkbutton> or is this a major shortfall of the language ? Consider the...
2
1702
by: Rosa | last post by:
Hi, I'm looking for an elegant solution on how to find the youngest file within a given directory. At the moment I'm storing all files in an array and loop through it comparing the creation date...
2
2085
by: Eric Newton | last post by:
VB's more declarative nature of handling events is golden. I'm hoping C# will acquire this type of deal, in addition to the anonymous delegates. could do same as vb (actually would be easier to...
8
2140
by: Braky Wacky | last post by:
Hello, I have an ASP.NET webpage that uses an instance of System.Web.UI.HtmlControls.HtmlInputFile for uploading files to our server. I came across the documentation at MSDN for upping the...
13
1186
by: Edward W. | last post by:
hello, I have this function below which is simple and easy to understand private function ListHeight (byval UserScreenHeight as int) as int if UserScreenHeight < 1024 return 30 else return 50...
4
1463
by: Iain King | last post by:
When I loop over one list I use: for item in items: print item but often I want to loop through two lists at once, and I've been doing this like I would in any other language - creating an...
32
1997
by: r.z. | last post by:
class vector3 { public: union { float data; struct { float x, y, z; };
35
4301
by: Bjoern Hoehrmann | last post by:
Hi, For a free software project, I had to write a routine that, given a Unicode scalar value U+0000 - U+10FFFF, returns an integer that holds the UTF-8 encoded form of it, for example, U+00F6...
5
3703
by: Helmut Jarausch | last post by:
Hi, I'm looking for an elegant solution to the following (quite common) problem: Given a string of substrings separated by white space, split this into tuple/list of elements. The problem...
0
7264
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
7166
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...
1
7106
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
5689
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,...
0
4749
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...
0
3236
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...
0
3226
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1601
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 ...
0
459
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...

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.