|
What about
(1) Writing A class without fun function.
(2) Writing a (say) AA class that inherits from A and provides fun function.
(3) Writing a B class that inherits from A class and therefore is not able to call fun function?
Of course A class consumers should become AA class ones.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
yeah, that's the better way to do,
but in this project others may dislike this implement, they didn't want their code update, @_@.
Thanks for all your help.
|
|
|
|
|
Hi,
I don't understand what you don't want to happen for the B class.
do you want to hide the functionaly from a b object or the declaration;
like this:
int main()
{
A a;
B b;
a.fun();
b.fun();
b.fun();
A* p = &b;
p->fun();
b.A::fun();
}
Maybe you could use the Facade pattern to hide the unwanted A functionaly.
Learn from the mistakes of others, you may not live long enough to make them all yourself.
|
|
|
|
|
Dear gurus,
I detected a runtime-bottleneck in my code at the following line
pY[j] = d1 * pY[j] + d2 * pX[j];
.
All variables are floating point. This line is excecuted millions of times.
Now I think about improving runtime by using inline-assembler.
Can somebody give me a hint how to start in the best way (e.g. using a library)
(by the way: I'm not a beginner at assembler-coding)
Kind regards
modified on Thursday, July 3, 2008 5:16 AM
|
|
|
|
|
Tomerland wrote: All variables are floating point. This line is excecuted millions of times.
Now I think about improving runtime by using inline-assembler.
I would recommend you to not use inline-asm for various reasons, but the primary reason is "there must be a better way".
A few things to reflect upon:
1. Floating point instructions are slow.
Does it have to be floating points or would integer values suffice?
2. Updating an array of floating points is rarely a "bottleneck" from a data administration point of view. You rarely take time critical actions based upon floating point calculation with e.g. 15-bit exponent and a 64-bit mantissa which is what you have when using long double .
If you're doing this from your GUI thread it will become unresponsive and the user will experience the UI as hung, but that's another problem.
Is it not possible to calculate the new values of the array in a worker thread and post a message to the GUI thread when the calculation has finished?
3. How about using integers that are multiplied by e.g. 100?
When you calculate with the values you have two "decimals", but when you present the value to the user, or store it, you can convert to a floating point value.
I used this technique in an embedded system that had no support for floating point values, but I multiplied with 16 instead which was a good enough approximation.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote "High speed never compensates for wrong direction!" - unknown
|
|
|
|
|
A few ideas:
1. Since the line is executed millions of times, you may be able to speed it up with loop unrolling http://en.wikipedia.org/wiki/Loop_unrolling[^].
2. Every time you do pY[j], it requires a multiplication and an addition. If you're processing the array sequentially, it would be faster to start with a pointer to pY[0], then increment it to get the next array element.
If you show some more surrounding code, we may be able to see some other optimizations.
|
|
|
|
|
How to solve that problem in C language?
//struct BB; ?? Is there something similar as declaring class BB; in C++ ??
struct AA {
struct BB bb;
int a;
};
struct BB {
struct AA aa;
int b;
};
AA has BB member and BB has AA member.
chesnokov
|
|
|
|
|
As far as I know it will not work with C++ either. You have to use pointers to struct's. Like this
struct BB;
struct AA
{
struct BB* bb;
int a;
};
struct BB
{
struct AA aa;
int b;
};
This will work for C as well.
-Saurabh
|
|
|
|
|
No that also fails in compilation:
typedef struct _AA {
BB* bb; <--- C2016, C2061
int a;
} AA, *PAA;
typedef struct _BB {
AA* aa; <--- C2016, C2061
int b;
} BB, *PBB;
chesnokov
|
|
|
|
|
You forgot the forward declaration for struct _BB, try this.
struct BB;
typedef struct _AA
{
struct BB* bb;
int a;
} AA, *PAA;
typedef struct _BB
{
struct AA* aa;
int b;
} BB, *PBB;
-Saurabh
|
|
|
|
|
But you can not do that???
AA aa;
aa.a = 1;
aa.bb->b = 1; //error???
chesnokov
|
|
|
|
|
Should be aa->bb->b = 1;
-Saurabh
|
|
|
|
|
???
AA aa; //it is allocated on stack
aa.a = 1; //there is no error here
aa.bb->b = 1; //the error is here stating
error C2037: left of 'b' specifies undefined struct/union 'BB'
You will get the same error with
AA* aa;
aa&gt;bb->b = 1;
chesnokov
|
|
|
|
|
Oh Yes that one compiled
struct BB;
struct AA {
struct BB* bb;
int a;
};
struct BB {
struct AA* aa;
int b;
};
And that one also.
struct BB;
typedef struct _AA {
struct BB* bb;
int a;
} AA;
typedef struct _BB {
AA* aa;
int b;
} BB;
chesnokov
|
|
|
|
|
See this[^] thread.
Steve
|
|
|
|
|
I use forwarding references in C++ but I need them in C with structs and it does not work:
struct _BB;
typedef struct _AA {
BB* bb; //error
int a;
} AA, *PAA;
typedef struct _BB {
AA* aa; //error
int b;
} BB, *PBB;
chesnokov
|
|
|
|
|
Try something like this:
"A.h":
#ifndef __A_H__INCLUDED__
#define __A_H__INCLUDED__
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
typedef struct tagB B;
typedef struct tagA
{
B *pB;
} A;
#endif // !__A_H__INCLUDED__
"B.h":
#ifndef __B_H__INCLUDED__
#define __B_H__INCLUDED__
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
typedef struct tagA A;
typedef struct tagB
{
A *pA;
} B;
#endif // !__B_H__INCLUDED__
Steve
|
|
|
|
|
I'll see if it will solve the next problem!
That one compiles:
struct BB;
typedef struct _AA {
struct BB* bb;
int a;
} AA, *PAA;
typedef struct _BB {
struct AA* aa;
int b;
} BB, *PBB;
But you can not access BBs contents using AA object??
AA aa;
aa.a = 1;
aa.bb->b = 1; //error???? Ahhhh
chesnokov
|
|
|
|
|
Chesnokov Yuriy wrote: aa.bb->b = 1; //error???? Ahhhh
Should be as follows:
aa->bb->b = 1;
Steve
|
|
|
|
|
You have a lot of patience, man. My 5 .
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
No way
AA aa;
aa->bb??? how can you access with -> object on stack not a pointer??
the glitch is
struct BB;
typedef struct _AA {
struct BB* bb;
int a;
} AA, *PAA;
typedef struct _BB {
struct AA* aa;
int b;
} BB, *PBB;
that one works but you can not access bbs constents from AA nor vice versa?
chesnokov
|
|
|
|
|
Yeah, you're right. So what's your problem exactly?
Steve
|
|
|
|
|
to compile and run that code:
struct BB;
typedef struct _AA {
struct BB* bb;
int a;
} AA, *PAA;
typedef struct _BB {
struct AA* aa;
int b;
} BB, *PBB;
main()
{
AA a;
BB b;
a.bb = &b;
b.aa = &a;
a.bb->b = 1;
b.aa->a = 3;
}
chesnokov
|
|
|
|
|
This works fine for me:
#include "stdafx.h"
struct BB;
struct AA
{
struct BB* bb;
int a;
};
struct BB
{
struct AA* aa;
int b;
};
void main()
{
struct AA a;
struct BB b;
a.bb = &b;
b.aa = &a;
a.bb->b = 1;
b.aa->a = 3;
}
Steve
|
|
|
|
|
You're having trouble with them being declared as pointers, and want them as real contained structs.
To borrow a phrase, "Imagine if you could do what you're asking".
AA would contain a BB which would contain an AA which would contain a BB which would contain an AA which would contain a BB which would contain an AA which would contain a BB which would contain an AA which would contain a BB which would contain an AA which would contain a BB which would contain an AA which would contain a BB which would contain an AA which would contain a BB...
I hope I'm getting the point across - I'm bored of typing. But your structure would end up infinitely large.
You can contain a *pointer* to your structures evil twin - a pointer only takes up a few bytes. But you need to give the pointer something to point to.
So:
struct BB;
struct AA
{
struct BB *bb;
int a;
};
struct BB
{
struct AA *aa;
int b;
};
main ()
{
AA a;
BB b;
a.bb->b = 1;
}
In the above example, C isn't psychic, so hasn't pointed A.bb at any BB structure. How can it? That's your job.
Try the below code.
AA a;
BB b;
a.bb = &b;
b.aa = &a;
a.bb->b = 1;
b.aa->a = 3;
Now your two structure can talk to each other to their heart's content.
I'd go back to your C book, and look up linked lists - it'll give you a light bulb moment about pointers.
Iain.
Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.
|
|
|
|