Structure/Class with optional data
-
In my previous post I realized I pretty messed up my project with unneeded templates. I thought "what about creating a structure or class with a pointer to another structure and putting that pointer just when needed?" So here's the question: I dislike personally the idea of a pointer pointing to optional data, is there any better solution to store data which always has a common header but then may vary?
---
-
In my previous post I realized I pretty messed up my project with unneeded templates. I thought "what about creating a structure or class with a pointer to another structure and putting that pointer just when needed?" So here's the question: I dislike personally the idea of a pointer pointing to optional data, is there any better solution to store data which always has a common header but then may vary?
---
create a class to store the 'common header'. then, use that as a base class and derive objects from it to store the 'optional' data. ?
-
In my previous post I realized I pretty messed up my project with unneeded templates. I thought "what about creating a structure or class with a pointer to another structure and putting that pointer just when needed?" So here's the question: I dislike personally the idea of a pointer pointing to optional data, is there any better solution to store data which always has a common header but then may vary?
---
I don't really know about your requirements, but a
union
may possibly be an option.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] -
In my previous post I realized I pretty messed up my project with unneeded templates. I thought "what about creating a structure or class with a pointer to another structure and putting that pointer just when needed?" So here's the question: I dislike personally the idea of a pointer pointing to optional data, is there any better solution to store data which always has a common header but then may vary?
---
:) ? :
class A
{
...
};class B
{
A* m_pA;
public:
B(A* pA = NULL) { m_pA = pA; };
};int main()
{
A a;
B b1; // just because it (the passing of a) is not needed
B b2(&a); // just because it (the passing of a) is needed
...
return 0;
}virtual void BeHappy() = 0;
-
In my previous post I realized I pretty messed up my project with unneeded templates. I thought "what about creating a structure or class with a pointer to another structure and putting that pointer just when needed?" So here's the question: I dislike personally the idea of a pointer pointing to optional data, is there any better solution to store data which always has a common header but then may vary?
---
-
The hierarchy proposal is good I have to admit it. But I need to use data stored in this class in the easiest way possible, something like
MYclass a;
a.GetPoints();
CalculateOtherData( a.RetrievePoints() );and the hierarchy isn't that simple
---
4288 wrote:
But I need to use data stored in this class in the easiest way possible, something like MYclass a; a.GetPoints(); CalculateOtherData( a.RetrievePoints() ); and the hierarchy isn't that simple
So what does any of this have to do with, "...is there any better solution to store data which always has a common header but then may vary?"
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
4288 wrote:
But I need to use data stored in this class in the easiest way possible, something like MYclass a; a.GetPoints(); CalculateOtherData( a.RetrievePoints() ); and the hierarchy isn't that simple
So what does any of this have to do with, "...is there any better solution to store data which always has a common header but then may vary?"
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius