Dynamic Aggregation

C

c.ginestet

Hello,

I want to create classes that I could aggregate with each others using
operators. This would therefore take the form of a dynamic
aggregation.

For instance, (This code does not compile):


// Also include a base class MAMMAL for instance.

class cat
{
public:
cat();
~cat();
void meow();

protected:
int catAge;
}
void cat::meow();
{
std::cout << "Meow.";
}

class dog
{
public:
dog();
~dog();
void waouf();

protected:
int dogAge;
}
void dog::waouf();
{
std::cout << "Waouf.";
}

int main()
{
dog Bono;
cat Frisky;
mamal dogcat;

// Need to create an operator here,
dogcat = Bono + cat;
}

// And the resulting MAMMAL would have the following attributes and
// characteristic;
class dogcat
{
public:
dog();
~dog(); void waouf();

void waouf();
void meow();

protected:
int dogAge;
int catAge;
}

////////////////////////

Any help is welcome,

Thank you very much for your help,


Cedric
 
T

tony_in_da_uk

I want to create classes that I could aggregate with each others using
operators. This would therefore take the form of a dynamic
aggregation.

You might find it useful to search on "multiple inheritance",
basically:
class dog_and_cat : public dog, public cat { };
though I think the dog/cat thing isn't a great example as it's clearly
impossible for something to be both dog and cat. Normally, the
relationship would be "class dog : public mammal" and "class cat :
public mammal".

Why do you specifically want to use "+" to do this? You can use
templates to do such things, but it quickly gets complex and given you
need to ask about these things you probably couldn't maintain it, so I
can't see any point in explaining how to unless you can convince me
that you need to.

Cheers,

Tony
 
V

Victor Bazarov

I believe "c.ginestet" has a rather unconventional idea of what
"dynamic" means. For whatever reason I've always thought that in
C++ "dynamic" means either "driven by some data in the run-time"
or "having the lifetime not limited by scope". Maybe it's just me...
You might find it useful to search on "multiple inheritance",
basically:
class dog_and_cat : public dog, public cat { };

How is that "dynamic"?
[..impossibility of dog_and_cat existense aside..]

Why do you specifically want to use "+" to do this? You can use
templates to do such things, [..]

Again, how would that be "dynamic"?

I don't question your suggestion of using templates. After all,
we can try writing

template<class T, class U> class from_two : public T, public U
{
// ...
};

template<class T, class U>
from_two<T,U>* operator+(T const& t, U const& u)
{
return new from_two();
}

...
mammal *dogcat = dog + cat; // requires 'mammal' to be
// a virtual base class in both

(or something similar). I am just questioning the use of the
term "dynamic" here. And I second the request to elaborate on the
problem the OP's solving.

V
 
T

tony_in_da_uk

I believe "c.ginestet" has a rather unconventional idea of what
"dynamic" means. For whatever reason I've always thought that in
C++ "dynamic" means either "driven by some data in the run-time"
or "having the lifetime not limited by scope". Maybe it's just me...

I know multiple inheritance isn't dynamic. I simply suspect that the
OP tried to guess that the solution to whatever his real problem is
required some "dynamic" solution, when in fact it doesn't, so I'm
trying to make sure he's properly considered the compile-time
approaches.

FWIW, I agree that in C++ dynamic normally means "driven by some data
in the run-time", but fail to see any connection to scope other than
the meaningless coincidence of "dynamic" heap-based memory allocation
being independent of scope.

Tony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

School Project 1
Help in hangman game 1
Dynamic programming 3
exception handling problem 3
Destructor 2
Not what I expected from some exception code (throw/try/catch) 7
Chatbot 0
delete pointer 12

Members online

No members online now.

Forum statistics

Threads
474,144
Messages
2,570,823
Members
47,369
Latest member
FTMZ

Latest Threads

Top