Re: Copy a Base Class to a Derived Class
iakko wrote:
On 12 Gen, 21:06, Daniel Pitts
<newsgroup.spamfil...@virtualinfinity.net> wrote:
There is a difference between classes and instances! Make sure not to
confuse the two. When you have "new Derived()", you have actually
created a new Base along with a new Derived, so you don't need to have
another new Base();
--
That's not what I'm looking for. Suppose that the Base class is
produced by another class that I cannot modify. Suppose that I can
only use instances of a Derived class of Base. Thus, I can only make
an instance of Derived class and handle an already instanced Base
class. That's the scenario:
Is inheritance really what you want to do? I'm going to guess that the
class that you're inheriting from doesn't have a virtual dtor and might
need one.
int main()
{
/* Cannot touch from here ... */
Base * b = new Base();
b->setK(2);
printf("k: %d\n", b->getK());
/* prints 0 -- 2 */
/* ... to here! */
Derived * d = new Derived();
d->setI(10);
printf("i: %d\n", d->getI());
/* prints 10 -- 0 */
b = d; /* ??? Doesn't work */
d = b; /* ??? It's the same, doesn't work */
You want to assign pointers of different types to each other?
printf("i: %d -- k: %d\n", d->getI(), d->getK());
/* prints 10 -- 0 or 0 -- 2, it depends on b = d or d = b */
d and b point to different objects.
return 0;
}
Doesn't work.
Seems like the c++ language does not allow this behavior, but it's
logically plausible.
If you want to modify the int in Base that is inherited by derived, have
you considered something like this?
#include <iostream>
class Base {
int i;
public:
void SetI(const int ii) { i = ii; }
int GetI() const { return i; }
};
class Derived : public Base {
public:
void SetI(const int ii) { Base::SetI(ii); }
int GetI() const { return Base::GetI(); }
};
int main() {
Derived d;
d.SetI(34);
std::cout << d.GetI() << std::endl;
}
Suppose you have a Server class with a method that produces a Socket
class needed to handle connections. Suppose you want to inherit the
Socket class to generate Connection classes.
/* Pseudo code here, no implementations of methods */
class Server {
public:
Server(); ~Server();
Socket * incomingConnection();
};
class Connection : public Socket
{
public:
Socket(); ~Socket();
I don't understand this, you have a ctor and a dtor for Socket in the
Connection class?
};
int main () {
Server srv;
Socket * mySck = new Socket();
for(;;) {
mySck = reinterpret_cast<Connection *>
(srv.incomingConnection());
}
/* mySck loses al the informations, takes the entire data of
the Socket class returned by srv.incomingConnection(); */
}
In this scenario, deriving a Connection class is extremely useful! The
only way should be that srv.incomingConnection() returns a Connection
derived class filled whit data. I cannot do that because it belongs to
the untouchable library.
Maybe my english is not good enough to explain what I mean, but I hope
that the example can make it clearer.
Thanks anyway for your time :)
I'm pretty sure that I don't follow all of that, but maybe what you want
is something like this:
class SocketWeCantTouch {};
class ServerWeCantTouch {
public:
SocketWeCantTouch *socket() const {
// who owns this?
// many details are missing, so I don't know
// but we might not want to delete this thing
// ourselves but let the classes we can't touch
// do it for us.
return new SocketWeCantTouch;
}
};
class Connection {
SocketWeCantTouch *p_;
public:
Connection(const ServerWeCantTouch &s)
:
p_ (s.socket())
{}
~Connection() {
delete p_; // wait a minute, who owns this?
}
SocketWeCantTouch *p() const { return p_; }
};
int main() {
const ServerWeCantTouch server;
for(;;) {
const Connection c(server);
}
}
LR