Crazy C++ question - is this possible?
by Mike Stoddart · in Technical Issues · 10/11/2002 (7:02 pm) · 4 replies
Ok, this is going to be tough for me to explain, so bear with me.
Say I have a class:
Apart from using class inheritance, is there any way of doing the following.
Basically, at runtime I want the option of being able to create an instance of someClass that contains either:
- Data 1
- Data 2
- Data 1 & Data 2
I don't want to use inheritance as I want only one class per "object".
Is this possible, if it even makes sense! If I haven't explained this fully, then please let me know and I'll try to expand it further.
Thanks
Say I have a class:
Quote:
class someClass
{
// Data 1
public:
setA(int a) { a_ = a; };
setB(int b) { b_ = b; };
private:
int a_;
int b_;
// Data 2
public:
setC(int c) { c_ = c; };
setD(int d) { d_ = d; };
private:
int c_;
int d_;
};
Apart from using class inheritance, is there any way of doing the following.
Basically, at runtime I want the option of being able to create an instance of someClass that contains either:
- Data 1
- Data 2
- Data 1 & Data 2
I don't want to use inheritance as I want only one class per "object".
Is this possible, if it even makes sense! If I haven't explained this fully, then please let me know and I'll try to expand it further.
Thanks
#2
Thanks
10/11/2002 (7:40 pm)
It's tough to explain, but an object will be created on the server and then duplicated onto the client. Except the client doesn't need access to all of the methods and data, only a subset. Does that clarify the reasoning? I know the more I think about this the more I realise I should just create derived classes from a base class, but I'm just trying to see if there's another way to do this.Thanks
#3
That way, the client only has the data that it needs, and none of the data that it has no business touching.
Alternatively, if you absolutely cannot stand using inheritance:
Then, you could set them up as follows:
Is this more what you were looking for? Be warned that this code I spat out has no safety checking.
But, I'm curious to know, why do you want only 1 class for 2 sets of data with only some possible overlap?
10/11/2002 (10:50 pm)
Well, if the client only needs a subset of the data and functions, why not do the following:class ClientData
{
public:
setA(int a) { a_ = a; }
setB(int b) { b_ = b; }
protected:
int a_;
int b_;
};
class ServerData : public ClientData
{
public:
setC(int c) { c_ = c; }
setD(int d) { d_ = d; }
private:
int c_;
int d_;
};That way, the client only has the data that it needs, and none of the data that it has no business touching.
Alternatively, if you absolutely cannot stand using inheritance:
class DataSet
{
public:
DataSet(int a=0, int b=0) { setA(a); setB(b); }
setA(int a) { a_ = a; }
setB(int b) { b_ = b; }
private:
int a_;
int b_;
};
class DataHolder
{
public:
addData(int a, int b) { data_.append(DataSet(a,b)); }
addData(DataSet d) { data_.append(d); }
setData(int setNumber, int a, int b) { data_[setNumber].setA(a); data_[setNumber].setB(b); }
DataSet getData(int setNumber) { return data_[setNumber]; }
private:
std::vector<DataSet> data_;
};Then, you could set them up as follows:
SERVER: DataHolder serverData; serverData.addData(1,1000); // first one is data for client serverData.addData(255,10); // data only for server DataHolder clientData; clientData.addData(serverData.getData(0)); // give client client-only data
Is this more what you were looking for? Be warned that this code I spat out has no safety checking.
But, I'm curious to know, why do you want only 1 class for 2 sets of data with only some possible overlap?
#4
10/11/2002 (11:11 pm)
Didn't you ask this on the GD.net forums? You'll probably find a more comprehensive answer there.
Torque Owner James \"Corvidae\" Williams
union {
int a;
int c;
};
Unions are like structures, although only one of its members can be used at any time. I'm not sure if you can do things with the functions, though.
Why would you want this, anyhow? It'd probably just be easier to include BOTH sets of data in the class and only call the ones you want to use. Well, either that or inheritance, but you already said you don't want that (why not, BTW? That's what it's there for).