I have the following situation. A pseudo-code is attached below. I have a class A which has an object c of type D or E and this varies (actually its randomly decided). It uses b as a message for communication with a remote computer.
Code:
class C
{
...
};
class D: public C
{
...
};
class E: public C
{
...
};
struct B
{
int a;
// If A->c is of type D
float b;
// If A->c is of type E
double b;
};
class A
{
B b;
C *c;
A()
{
c = (C*) new D;
//c = (C*) new E;
}
...
...
void transmit()
{
//b has some attributes depending on whether c is of type D or E
//Open a socket and send packets via UDP
//The remote host receives the packets
}
};
I hope this explains my problem. If it is not clear or ambiguous, please let me know. I will provide more details and explanation. Thanks in advance.
Either use a factory pattern to create the objects at runtime.
Or, use a template:
template <class T>
class A {
T a;
}
A<int> a = new A<int>();
A<double> b = new A<double>();
Second part is easy.
In your send-receive protocol keep 4 bytes at the beginning for sizeof (int) ... Then fill it with sizeof(a)
Since both D and E derive from C your implementation looks correct:
c = (C*) new D;
Though I would remove the C-Cast (it is not required, if you need casts you should use the C++ variants).
c = new D;
How you transmit the data will depend. But usually you will need to prefix that part of the information with type information so the destination understands how to decode the following stream.
send(a->a);
send("1") if a->c is D
send("2") if a->c is E
send(<Conditional Part>);
As a side note. Look up smart pointers. Using a raw pointer in your class is a bad idea (and not good C++).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With